Module: Symphony::ClassMethods

Defined in:
lib/symphony/symphony.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  :of => Object,
  :optional => false,
  :nillable => false
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#symphony_definitionsObject (readonly)

Returns the value of attribute symphony_definitions.



131
132
133
# File 'lib/symphony/symphony.rb', line 131

def symphony_definitions
  @symphony_definitions
end

Instance Method Details

#create_methods(defn) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/symphony/symphony.rb', line 157

def create_methods(defn)
  # reader
  class_eval "
    def #{defn[:method_name]}
      puts '#{defn[:method_name]}:' + self.class.symphony_definitions.find{ |d| d[:method_name] == :#{defn[:method_name]} }.inspect
      @#{defn[:method_name]} || self.class.symphony_definitions.find{ |d| d[:method_name] == :#{defn[:method_name]} }[:options][:default]
    end
  "

  # writer
  check_method = (defn[:multiplicity] == :many or defn[:multiplicity] > 1) ? 'check_made_of_only' : 'check_type'
  class_eval "
    def #{defn[:method_name]}=(obj)
      raise TypeError, \"#{defn[:method_name]} is not nillable\" if !#{defn[:options][:nillable]} and obj == nil
      #{check_method}(#{defn[:options][:of]}, obj) if obj != nil or !#{defn[:options][:nillable]}

      @#{defn[:method_name]} = obj
    end
  "
end

#has(multiplicity, name, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


137
138
139
140
141
142
143
144
145
146
147
# File 'lib/symphony/symphony.rb', line 137

def has(multiplicity, name, options={})
  initialize_symphony_definitions()
  
  raise ArgumentError, "multiplicity must be an integer or a symbol (i.e. :many)" if !multiplicity.is_a?(Integer) and !multiplicity.is_a?(Symbol)
  
  options = DEFAULT_OPTIONS.merge(options)
  defn = {:multiplicity => multiplicity, :method_name => name.to_sym, :options => options}
  @symphony_definitions << defn

  create_methods(defn)
end

#has_many(name, options) ⇒ Object



153
154
155
# File 'lib/symphony/symphony.rb', line 153

def has_many(name, options)
  has :many, name, options
end

#has_one(name, options) ⇒ Object



149
150
151
# File 'lib/symphony/symphony.rb', line 149

def has_one(name, options)
  has 1, name, options
end

#initialize_symphony_definitionsObject



133
134
135
# File 'lib/symphony/symphony.rb', line 133

def initialize_symphony_definitions
  @symphony_definitions ||= []
end