Class: Alula::ObjectField

Inherits:
Object
  • Object
show all
Extended by:
ResourceAttributes
Defined in:
lib/alula/resource_attributes.rb

Direct Known Subclasses

Device::FeaturesSelected

Instance Method Summary collapse

Methods included from ResourceAttributes

date_fields, extended, field, field_names, filterable_fields, get_fields, get_http_methods, get_resource_path, get_type, http_methods, param_key, read_only_attributes, resource_path, sortable_fields, type

Constructor Details

#initialize(dirty_attributes, parent_field, properties = {}) ⇒ ObjectField

Assume properties is camel case



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/alula/resource_attributes.rb', line 210

def initialize(dirty_attributes, parent_field, properties = {})
  @values = properties
  @parent_dirty_attributes = dirty_attributes
  @parent_field = parent_field
  # Not used, just to make resource attributes happy
  @dirty_attributes = Set.new

  return unless properties
  valid_fields = self.class.get_fields

  properties.dup.each do |key, value|
    jsonKey = key;
    ruby_key = Alula::Util.underscore(key.to_s)

    self.public_send("#{ruby_key}=", value) if valid_fields.key?(ruby_key.to_sym)
  end
end

Instance Method Details

#as_jsonObject



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/alula/resource_attributes.rb', line 231

def as_json
  self.field_names.each_with_object({}) do |ruby_key, obj|
    key      = Util.camelize(ruby_key)
    val = self.send(ruby_key)

    if self.date_fields.include?(ruby_key) && ![nil, ''].include?(val)
      if val.respond_to? :strftime
        obj[key] = val.strftime('%Y-%m-%dT%H:%M:%S.%L%z')
      else
        obj[key] = val.to_s
      end
    else
      obj[key] = val
    end
  end
end

#mark_dirty(field_name, old_value, new_value) ⇒ Object



227
228
229
# File 'lib/alula/resource_attributes.rb', line 227

def mark_dirty(field_name, old_value, new_value)
  @parent_dirty_attributes << @parent_field if old_value != new_value
end