Module: Ooor::FieldMethods

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

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

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


196
197
198
199
200
201
202
203
204
205
206
# File 'lib/ooor/field_methods.rb', line 196

def method_missing(method_symbol, *arguments)
  self.class.reload_fields_definition(false)
  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 Attribute Details

#_display_nameObject Also known as: _name

Returns the value of attribute _display_name.



109
110
111
# File 'lib/ooor/field_methods.rb', line 109

def _display_name
  @_display_name
end

Instance Method Details

#_destroyObject



116
117
118
# File 'lib/ooor/field_methods.rb', line 116

def _destroy
  @marked_for_destruction
end

#_destroy=(dummy) ⇒ Object



112
113
114
# File 'lib/ooor/field_methods.rb', line 112

def _destroy=(dummy)
  @marked_for_destruction = true unless dummy.blank? || ["false", "0", 0].index(dummy)
end

#get_association(meth, *args) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/ooor/field_methods.rb', line 146

def get_association(meth, *args)
  return @associations[meth] || :undef if @skip
  lazy_load(meth, *args) if @lazy
  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 || context)[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(meth, [], *args)
    else
      nil
    end
  end
end

#get_attribute(meth, *args) ⇒ Object



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

def get_attribute(meth, *args)
  lazy_load(meth, *args) if @lazy && @attributes["id"] && !@attributes.has_key?(meth)
  if @attributes.has_key?(meth)
    @attributes[meth]
  elsif @attributes["id"] # if field is computed for instance
    @attributes[meth] = rpc_execute('read', [@attributes["id"]], [meth], *args || context)[0][meth]
  else
    nil
  end
end

#lazy_load(meth, *args) ⇒ Object



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

def lazy_load(meth, *args)
  @lazy = false
  fields = (self.class.fast_fields + [meth]).uniq
  load(rpc_execute('read', [@attributes["id"]], fields, *args || context)[0]).tap do
    @lazy = false
  end
end

#set_association(meth, *args) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/ooor/field_methods.rb', line 165

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



139
140
141
142
143
144
# File 'lib/ooor/field_methods.rb', line 139

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