Module: NaranyaEcm::Rest::Model::ClassMethods

Defined in:
lib/naranya_ecm/rest/model.rb

Instance Method Summary collapse

Instance Method Details

#allObject



243
244
245
# File 'lib/naranya_ecm/rest/model.rb', line 243

def all
  where()
end

#field(name, options = {}) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/naranya_ecm/rest/model.rb', line 193

def field(name, options = {})

  if options[:localize]
    lookup_alias_name = name
    name = "#{name}_translations".to_sym
  end

  fields[name] = {name: name}.merge(options).with_indifferent_access

  attr_accessor name
  define_attribute_method name

  if options[:localize]
    # Define localized setter with dirty notification:
    define_method("#{name}=".to_sym) do |value|
      value = {} if value.nil?
      value = LocalizedAttribute.new value if value.is_a?(Hash) && !value.is_a?(LocalizedAttribute)

      raise "Type Mismatch..." unless value.is_a?(LocalizedAttribute)

      variable_name = "@#{name}".to_sym
      self.send "#{name}_will_change!".to_sym unless value == instance_variable_get(variable_name)
      instance_variable_set variable_name, value
    end

    # Define lookup alias:
    define_method lookup_alias_name do
      self.send(name).lookup
    end

  else
    # Define normal setter with dirty notification:
    define_method("#{name}=".to_sym) do |value|
      variable_name = "@#{name}".to_sym
      self.send "#{name}_will_change!".to_sym unless value == instance_variable_get(variable_name)
      instance_variable_set variable_name, value
    end
  end
end

#fieldsObject



184
185
186
# File 'lib/naranya_ecm/rest/model.rb', line 184

def fields
  @_fields ||= {}.with_indifferent_access
end

#pathObject



233
234
235
# File 'lib/naranya_ecm/rest/model.rb', line 233

def path
  @path ||= "/#{self.name.demodulize.tableize}"
end

#primary_key(name, options = {}) ⇒ Object



188
189
190
191
# File 'lib/naranya_ecm/rest/model.rb', line 188

def primary_key(name, options = {})
  attr_accessor name
  define_attribute_method name
end

#where(given_conditions = {}) ⇒ Object



237
238
239
# File 'lib/naranya_ecm/rest/model.rb', line 237

def where(given_conditions={})
  Relation.new self, given_conditions.with_indifferent_access
end