Module: Fauxsql::DSL

Defined in:
lib/fauxsql/dsl.rb

Defined Under Namespace

Classes: InvalidNesting

Instance Method Summary collapse

Instance Method Details

#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

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

DSL method to define a named Fauxsql list

calling with ‘squad_members’ is like writing:

def squad_members
  get_fauxsql_list(:squad_members)
end


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/fauxsql/dsl.rb', line 43

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

  if options[:nest]
    class_eval <<EORUBY, __FILE__, __LINE__
      def #{attribute_name}=(attrs)
        #{attribute_name}.clear
        attrs.each do |index, vals|
          vals = Fauxsql::DSL.normalize_nested_vals!(vals)
          record = #{attribute_name}.get_nested_record(vals)
          #{attribute_name} << record if record unless vals[:_delete]
        end
      end
EORUBY
  end
end

#manymany(attribute_name, classes, options) ⇒ Object

DSL method to define a named Fauxsql manymany relationship

calling with ‘friends’ is like writing:

def friends
  get_fauxsql_manymany(:friends, Other, :through => :friends)
end


101
102
103
104
105
# File 'lib/fauxsql/dsl.rb', line 101

def manymany(attribute_name, classes, options)
  define_method attribute_name do
    get_fauxsql_manymany(attribute_name, classes, options)
  end
end

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

DSL method to define a named Fauxsql map

calling with ‘mitigates’ is like writing:

def mitigates
  get_fauxsql_map(:mitigates)
end


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/fauxsql/dsl.rb', line 71

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

  if options[:nest]
    class_eval <<EORUBY, __FILE__, __LINE__
      def #{attribute_name}=(attrs)
        deletes = []
        attrs.each do |index, vals|
          vals = Fauxsql::DSL.normalize_nested_vals!(vals)
          key = #{attribute_name}.get_nested_record(vals)
          #{attribute_name}[key] = vals[:value]
          deletes << key if vals[:_delete]
        end
        deletes.each{ |key| #{attribute_name}.delete(key) }
      end
EORUBY
  end
end