Method: Fauxsql::DSL#attribute

Defined in:
lib/fauxsql/dsl.rb

#attribute(attribute_name, options = {}) ⇒ Object

DSL method to define a named Fauxsql attribute

calling with ‘power’ is like writing:

def power
  get_fauxsql_attribute(:power)
end

def power=(value)
  set_fauxsql_attribute(:power, value)
end


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fauxsql/dsl.rb', line 15

def attribute(attribute_name, options={})
  fauxsql_options[attribute_name] = normalize_options!(options)
  class_eval <<EORUBY, __FILE__, __LINE__
    def #{attribute_name}
      get_fauxsql_attribute(:#{attribute_name})
    end

    def #{attribute_name}=(value)
      set_fauxsql_attribute(:#{attribute_name}, value)
    end
EORUBY

  if options[:nest]
    class_eval <<EORUBY, __FILE__, __LINE__
      def #{attribute_name}_attributes=(vals)
        vals = Fauxsql::DSL.normalize_nested_vals!(vals)
        #{attribute_name} = #{attribute_name}.get_nested_record(vals)
      end
EORUBY
  end
end