Class: DataMapper::Sweatshop

### Home Classes Methods

### In Files

- lib/dm-sweatshop/support/class_attributes.rb
- lib/dm-sweatshop/sweatshop.rb
- lib/dm-sweatshop/unique.rb

### Parent

Object

### Namespace

- MODULE DataMapper::Sweatshop::ClassAttributes
- MODULE DataMapper::Sweatshop::Unique
- CLASS DataMapper::Sweatshop::NoFixtureExist
- CLASS DataMapper::Sweatshop::UniqueWorker

### Methods

- ::add
- ::attributes
- ::create
- ::create!
- ::expand_callable_values
- ::make
- ::pick
- ::record

### Files

- LICENSE
- README.rdoc

### Class/Module Index

Quicksearch

- DataMapper
- DataMapper::Model
- DataMapper::Sweatshop
- DataMapper::Sweatshop::ClassAttributes
- DataMapper::Sweatshop::NoFixtureExist
- DataMapper::Sweatshop::Unique
- DataMapper::Sweatshop::Unique::TooManyTriesException
- DataMapper::Sweatshop::UniqueWorker

No matching classes.

# DataMapper::Sweatshop

### Attributes

model_map[RW]
record_map[RW]

### Public Class Methods

add(klass, name, &proc) click to toggle source

Adds a Proc to model map. Proc must return a Hash of attributes.

@param klass [Class, DataMapper::Resource] @param name [Symbol] @param instance [DataMapper::Resource]

@api private

@return [Array] model map

    # File lib/dm-sweatshop/sweatshop.rb, line 32
    def self.add(klass, name, &proc)
      self.model_map[klass][name.to_sym] << proc
    end

attributes(klass, name) click to toggle source

Returns a Hash of attributes from the model map

@param klass [Class, DataMapper::Resource] @param name [Symbol]

@return [Hash] existing instance of a model from the model map @raise NoFixtureExist when requested fixture does not exist in the model map

@api private

    # File lib/dm-sweatshop/sweatshop.rb, line 114
    def self.attributes(klass, name)
      proc = model_map[klass][name.to_sym].pick

      if proc
        expand_callable_values(proc.call)
      elsif klass.superclass.is_a?(DataMapper::Model)
        attributes(klass.superclass, name)
      else
        raise NoFixtureExist, "#{name} fixture was not found for class #{klass}"
      end
    end

create(klass, name, attributes = {}) click to toggle source

Creates an instance from given hash of attributes, saves it and adds it to the record map.

@param klass [Class, DataMapper::Resource] @param name [Symbol] @param attributes [Hash]

@api private

@return [DataMapper::Resource] added instance

    # File lib/dm-sweatshop/sweatshop.rb, line 74
    def self.create(klass, name, attributes = {})
      record(klass, name, klass.create(attributes(klass, name).merge(attributes)))
    end

create!(klass, name, attributes = {}) click to toggle source

Same as create but calls Model#create! and does save invalid models

@param klass [Class, DataMapper::Resource] @param name [Symbol] @param attributes [Hash]

@api private

@return [DataMapper::Resource] added instance

    # File lib/dm-sweatshop/sweatshop.rb, line 60
    def self.create!(klass, name, attributes = {})
      record(klass, name, klass.create!(attributes(klass, name).merge(attributes)))
    end

expand_callable_values(hash) click to toggle source

Returns a Hash with callable values evaluated.

@param hash [Hash]

@return [Hash] existing instance of a model from the model map

@api private

    # File lib/dm-sweatshop/sweatshop.rb, line 133
    def self.expand_callable_values(hash)
      expanded = {}
      hash.each do |key, value|
        if value.respond_to?(:call)
          expanded[key] = value.call
        else
          expanded[key] = value
        end
      end
      expanded
    end

make(klass, name, attributes = {}) click to toggle source

Creates an instance from given hash of attributes and adds it to records map without saving.

@param klass [Class, DataMapper::Resource] @param name [Symbol] @param attributes [Hash]

@api private

@return [DataMapper::Resource] added instance

    # File lib/dm-sweatshop/sweatshop.rb, line 88
    def self.make(klass, name, attributes = {})
      record(klass, name, klass.new(attributes(klass, name).merge(attributes)))
    end

pick(klass, name) click to toggle source

Returns a pre existing instance of a model from the record map

@param klass [Class, DataMapper::Resource] @param name [Symbol]

@return [DataMapper::Resource] existing instance of a model from the record map @raise DataMapper::Sweatshop::NoFixtureExist when requested fixture does not exist in the record map

@api private

    # File lib/dm-sweatshop/sweatshop.rb, line 101
    def self.pick(klass, name)
      self.record_map[klass][name.to_sym].pick || raise(NoFixtureExist, "no #{name} context fixtures have been generated for the #{klass} class")
    end

record(klass, name, instance) click to toggle source

Adds an instance to records map.

@param klass [Class, DataMapper::Resource] @param name [Symbol] @param instance [DataMapper::Resource]

@api private

@return [DataMapper::Resource] added instance

    # File lib/dm-sweatshop/sweatshop.rb, line 45
    def self.record(klass, name, instance)
      self.record_map[klass][name.to_sym] << instance
      instance
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.