Module: NaranyaEcm::Rest::Model

Extended by:
ActiveSupport::Concern
Defined in:
lib/naranya_ecm/rest/model.rb

Defined Under Namespace

Modules: ClassMethods Classes: LocalizedAttribute

Constant Summary collapse

BOOLEAN =

Constant proc that validates if a given value is a boolean:

Proc.new { |value| !!value == value }

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



107
108
109
110
111
112
113
114
115
116
# File 'lib/naranya_ecm/rest/model.rb', line 107

def method_missing(method, *args, &block)
  if method =~ /^(\w+)=$/i
    self.class.send :field, $1.to_sym, type: args.first.class
    self.send method, *args
  else
    super # You *must* call super if you don't handle the
          # method, otherwise you'll mess up Ruby's method
          # lookup.
  end
end

Instance Method Details

#associationsObject



157
158
159
160
161
162
# File 'lib/naranya_ecm/rest/model.rb', line 157

def associations
  self.instance_variables
    .map { |instance_variable| instance_variable[1..-1].to_sym }
    .select { |accessor_name| self.is_association?(accessor_name) }
    .inject({}.with_indifferent_access) { |hash, accessor_name| hash[accessor_name] = self.send accessor_name; hash }
end

#attribute_for_inspect(attr_name) ⇒ Object

Returns an #inspect-like string for the value of the attribute attr_name. String attributes are truncated upto 50 characters, Date and Time attributes are returned in the :db format, Array attributes are truncated upto 10 values. Other attributes return the value of #inspect without modification.

person = Person.create!(name: 'David Heinemeier Hansson ' * 3)

person.attribute_for_inspect(:name)
# => "\"David Heinemeier Hansson David Heinemeier Hansson ...\""

person.attribute_for_inspect(:created_at)
# => "\"2012-10-22 00:15:07\""

person.attribute_for_inspect(:tag_ids)
# => "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...]"


142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/naranya_ecm/rest/model.rb', line 142

def attribute_for_inspect(attr_name)
  value = self.public_send(attr_name)

  if value.is_a?(String) && value.length > 50
    "#{value[0, 50]}...".inspect
  elsif value.is_a?(Date) || value.is_a?(Time)
    %("#{value.to_s(:db)}")
  elsif value.is_a?(Array) && value.size > 10
    inspected = value.first(10).inspect
    %(#{inspected[0...-1]}, ...])
  else
    value.inspect
  end
end

#attributesObject



118
119
120
121
122
123
# File 'lib/naranya_ecm/rest/model.rb', line 118

def attributes
  self.class.fields.values.inject('id' => self.id) do |hsh, attr_data|
    hsh[attr_data[:name].to_s] = self.public_send attr_data[:name]
    hsh
  end.with_indifferent_access
end

#initialize_with_defaults(*args) ⇒ Object



96
97
98
99
100
101
102
103
104
105
# File 'lib/naranya_ecm/rest/model.rb', line 96

def initialize_with_defaults(*args)
  init_internals

  initialize_without_defaults(*args)
  # inicializar con los valores default los campos que esten nulos y tengan un default definido:
  self.class.fields.values
    .select { |f| instance_variable_get("@#{f[:name]}".to_sym).nil? && f[:default] }
    .each { |f| instance_variable_set("@#{f[:name]}".to_sym, f[:default].call) }

end

#inspectObject

Returns the contents of the record as a nicely formatted string.



169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/naranya_ecm/rest/model.rb', line 169

def inspect
  # We check defined?(@attributes) not to issue warnings if the object is
  # allocated but not initialized.
  inspection = (['id'] + self.class.fields.keys.sort).map { |name|
    value = attribute_for_inspect(name)
    "#{name}: #{value}" unless value == 'nil'
  }.compact.join(", ")

  inspection = "not initialized" if inspection.blank?

  "#<#{self.class} #{inspection}>"
end

#pathObject



164
165
166
# File 'lib/naranya_ecm/rest/model.rb', line 164

def path
  "#{self.class.path}/#{self.id}"
end

#readonly!Object

Marks this record as read only.



263
264
265
# File 'lib/naranya_ecm/rest/model.rb', line 263

def readonly!
  @readonly = true
end

#readonly?Boolean

Returns true if the resource is read only.

Returns:

  • (Boolean)


258
259
260
# File 'lib/naranya_ecm/rest/model.rb', line 258

def readonly?
  @readonly
end