Class: Alula::Dcp::ObjectField

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

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, resource_path, sortable_fields, type

Constructor Details

#initialize(dirty_attributes, parent_field, properties = nil) ⇒ ObjectField

Assume properties is camel case



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/alula/dcp/resource_attributes.rb', line 220

def initialize(dirty_attributes, parent_field, properties = nil)
  @values = properties
  @parent_field = parent_field
  @parent_dirty_attributes = dirty_attributes
  @dirty_attributes = Set.new
  return unless properties

  valid_fields = self.class.get_fields
  properties.dup.each do |key, value|
    ruby_key = Alula::Util.underscore(key.to_s)
    next unless valid_fields.key?(ruby_key.to_sym)

    public_send("#{ruby_key}=", value)
  end
end

Instance Method Details

#apply_attributes(attributes, merge_only: false) ⇒ Object

Take a hash of attributes and apply them to the model



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/alula/dcp/resource_attributes.rb', line 256

def apply_attributes(attributes, merge_only: false)
  res = attributes.each do |key, value|
    next unless field_names.include?(key.to_sym)

    opts = self.class.get_fields[key.to_sym]
    json_key = Util.camelize(key.to_s)
    if merge_only
      # merge the value into the existing value
      if value.is_a?(Array)
        value.each_index do |index|
          send(key)[index].apply_attributes(value[index], merge_only: true)
        end
      elsif value.is_a?(Hash) && send(key).respond_to?(:apply_attributes)
        send(key).apply_attributes(value, merge_only: true)
      elsif value.is_a?(Hash) && send(key).respond_to?(:each)
        send("#{key}=", value[key])
      elsif opts[:type] == :number
        @values[json_key] = value.to_i
      elsif opts[:type] == :boolean
        @values[json_key] = [true, 'true', 1, '1'].include? value
      else
        @values[json_key] = value
      end
    else
      send("#{key}=", value)
    end
  end
  return @values if merge_only

  res
end

#as_jsonObject



244
245
246
247
248
249
250
251
252
# File 'lib/alula/dcp/resource_attributes.rb', line 244

def as_json
  field_names.each_with_object({}) do |ruby_key, obj|
    key = Util.camelize(ruby_key)
    val = respond_to?(ruby_key) ? send(ruby_key) : nil
    next if val.nil?

    obj[key] = parse_value(val, ruby_key)
  end
end

#model_nameObject



236
237
238
# File 'lib/alula/dcp/resource_attributes.rb', line 236

def model_name
  self.class
end

#present?Boolean

Returns:

  • (Boolean)


240
241
242
# File 'lib/alula/dcp/resource_attributes.rb', line 240

def present?
  !@values.nil?
end