Class: Fixtory::DSL::Row

Inherits:
BasicObject
Defined in:
lib/fixtory/dsl/row.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, table, &block) ⇒ Row

Returns a new instance of Row.



6
7
8
9
10
11
12
13
14
# File 'lib/fixtory/dsl/row.rb', line 6

def initialize(name, table, &block)
  @name = name.to_s
  @table = table
  @inserted = false
  @attributes = {
    _primary_key => ::ActiveRecord::FixtureSet.identify(@name)
  }
  instance_eval &block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(attribute, *args) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/fixtory/dsl/row.rb', line 24

def method_missing(attribute, *args)
  attribute = attribute.to_s
  value = args.first

  if value
    if reflection = @table._model_class._reflections[attribute.to_sym]
      if reflection.macro == :belongs_to
        attribute = reflection.association_foreign_key
        value = value._primary_key_value
      else
        (::Array === value ? value : [value]).each do |row|
          row.__send__(reflection.foreign_key, self._primary_key_value)
        end

        return
      end
    end

    @attributes[attribute] = value
  else
    if @attributes.key?(attribute)
      @attributes[attribute]
    else
      @table._builder.instance_eval(attribute)
    end
  end
end

Instance Method Details

#_primary_keyObject



16
17
18
# File 'lib/fixtory/dsl/row.rb', line 16

def _primary_key
  @table._model_class.primary_key
end

#_primary_key_valueObject



20
21
22
# File 'lib/fixtory/dsl/row.rb', line 20

def _primary_key_value
  @attributes[_primary_key]
end