Module: Ooor::FieldMethods

Extended by:
ActiveSupport::Concern
Included in:
Base
Defined in:
lib/ooor/field_methods.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_symbol, *arguments) ⇒ Object

# Raise NoMethodError if the named attribute does not exist in order to preserve behavior expected by #clone.

def attribute(name)
  key = name.to_s
  if self.class.fields.has_key?(key) #TODO check not symbols
    get_attribute(key)
  elsif self.class.associations_keys.index(key)
    get_association(key)
  else
    raise NoMethodError
  end
end


187
188
189
190
191
192
193
194
195
196
197
# File 'lib/ooor/field_methods.rb', line 187

def method_missing(method_symbol, *arguments)
  self.class.reload_fields_definition(false, object_session)
  if id
    rpc_execute(method_symbol, [id], *arguments) #we assume that's an action
  else
    super
  end
rescue UnknownAttributeOrAssociationError => e
  e.klass = self.class
  raise e
end

Instance Method Details

#get_association(meth, *args) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/ooor/field_methods.rb', line 138

def get_association(meth, *args)
  return @associations[meth] || :undef if @skip
  if @loaded_associations.has_key?(meth)
    @loaded_associations[meth]
  elsif @associations.has_key?(meth)
    @loaded_associations[meth] = relationnal_result(meth, *args)
  else
    if @attributes["id"]
      @associations[meth] = rpc_execute('read', [@attributes["id"]], [meth], *args || object_session)[0][meth]
      @loaded_associations[meth] = relationnal_result(meth, *args)
    elsif self.class.one2many_associations.has_key?(meth) || self.class.many2many_associations.has_key?(meth)
      load_x2m_association(self.class.all_fields[meth]['relation'], [], *args)
    else
      nil
    end
  end
end

#get_attribute(meth, *args) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/ooor/field_methods.rb', line 119

def get_attribute(meth, *args)
  if @attributes.has_key?(meth)
    @attributes[meth]
  else #lazy loading
    if @attributes["id"]
      @attributes[meth] = rpc_execute('read', [@attributes["id"]], [meth], *args || object_session)[0][meth]
    else
      nil
    end
  end
end

#set_association(meth, *args) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/ooor/field_methods.rb', line 156

def set_association(meth, *args)
  value = sanitize_association(meth, args[0])
  if self.class.many2one_associations.has_key?(meth) # TODO detect false positives changes for other associations too
    if @associations[meth].is_a?(Array) && @associations[meth][0] == value \
       || @associations[meth] == value #\
      return value
    end
  end
  @skip = true
  send("#{meth}_will_change!")
  @skip = false
  if value.is_a?(Ooor::Base) || value.is_a?(Array) && !value.empty? && value.all? {|i| i.is_a?(Ooor::Base)}
    @loaded_associations[meth] = value
  else
    @loaded_associations.delete(meth)
  end
  @associations[meth] = value
end

#set_attribute(meth, *args) ⇒ Object



131
132
133
134
135
136
# File 'lib/ooor/field_methods.rb', line 131

def set_attribute(meth, *args)
  value = sanitize_attribute(meth, args[0])
  @attributes[meth] ||= nil
  send("#{meth}_will_change!") unless @attributes[meth] == value
  @attributes[meth] = value
end