Class: ResourceModel::Base

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Callbacks, ActiveModel::Naming
Includes:
ActiveModel::Conversion, ActiveModel::Validations, ActiveModel::Validations::Callbacks
Defined in:
lib/resource_model/base.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Base



441
442
443
# File 'lib/resource_model/base.rb', line 441

def initialize(attributes={})
  self.attributes = attributes
end

Class Attribute Details

.associated_model_attributesObject (readonly)

Returns the value of attribute associated_model_attributes.



110
111
112
# File 'lib/resource_model/base.rb', line 110

def associated_model_attributes
  @associated_model_attributes
end

.associated_model_collection_attributesObject (readonly)

Returns the value of attribute associated_model_collection_attributes.



144
145
146
# File 'lib/resource_model/base.rb', line 144

def associated_model_collection_attributes
  @associated_model_collection_attributes
end

.associated_resource_model_attributesObject (readonly)

Returns the value of attribute associated_resource_model_attributes.



22
23
24
# File 'lib/resource_model/base.rb', line 22

def associated_resource_model_attributes
  @associated_resource_model_attributes
end

.associated_resource_model_collection_attributesObject (readonly)

Returns the value of attribute associated_resource_model_collection_attributes.



59
60
61
# File 'lib/resource_model/base.rb', line 59

def associated_resource_model_collection_attributes
  @associated_resource_model_collection_attributes
end

.boolean_attributesObject (readonly)

Returns the value of attribute boolean_attributes.



197
198
199
# File 'lib/resource_model/base.rb', line 197

def boolean_attributes
  @boolean_attributes
end

.date_attributesObject (readonly)

Returns the value of attribute date_attributes.



299
300
301
# File 'lib/resource_model/base.rb', line 299

def date_attributes
  @date_attributes
end

.decimal_attributesObject (readonly)

Returns the value of attribute decimal_attributes.



247
248
249
# File 'lib/resource_model/base.rb', line 247

def decimal_attributes
  @decimal_attributes
end

.enum_attributesObject (readonly)

Returns the value of attribute enum_attributes.



361
362
363
# File 'lib/resource_model/base.rb', line 361

def enum_attributes
  @enum_attributes
end

.integer_attributesObject (readonly)

Returns the value of attribute integer_attributes.



222
223
224
# File 'lib/resource_model/base.rb', line 222

def integer_attributes
  @integer_attributes
end

.string_attributesObject (readonly)

Returns the value of attribute string_attributes.



339
340
341
# File 'lib/resource_model/base.rb', line 339

def string_attributes
  @string_attributes
end

.usd_attributesObject (readonly)

Returns the value of attribute usd_attributes.



273
274
275
# File 'lib/resource_model/base.rb', line 273

def usd_attributes
  @usd_attributes
end

Class Method Details

.boolean_accessor(attribute_name) ⇒ Object

Raises:

  • (ArgumentError)


199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/resource_model/base.rb', line 199

def self.boolean_accessor(attribute_name)
  raise ArgumentError, "Expected `#{attribute_name}` to be an instance of Symbol" unless attribute_name.is_a?(Symbol)
  if self.boolean_attributes.include?(attribute_name)
    raise ArgumentError, "Already contains a boolean accessor named `#{attribute_name}`"
  else
    @boolean_attributes << attribute_name
    self.class_eval "      attr_reader :\#{attribute_name}\n      def \#{attribute_name}=(value)\n        case value\n        when nil\n          @\#{attribute_name} = nil\n        when TrueClass, FalseClass\n          @\#{attribute_name} = value\n        else\n          @\#{attribute_name} = value.present? ? ['1', 1, 'true', 't'].include?(value) : nil\n        end\n      end\n    eos\n  end\nend\n"

.date_accessor(attribute_name, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/resource_model/base.rb', line 301

def self.date_accessor(attribute_name, options={})
  raise ArgumentError, "Expected `#{attribute_name}` to be an instance of Symbol" unless attribute_name.is_a?(Symbol)
  raise ArgumentError, "Expected options[:timezone]=`#{options[:timezone]}` to be an instance of Symbol, String, or Nil" unless options[:timezone].nil? || options[:timezone].is_a?(String) || options[:timezone].is_a?(Symbol)
  if self.date_attributes.include?(attribute_name)
    raise ArgumentError, "Already contains a date accessor named `#{attribute_name}`"
  else
    @date_attributes << attribute_name
    self.class_eval "      attr_reader :\#{attribute_name}\n      attr_reader :\#{attribute_name}_unconverted_value\n      def \#{attribute_name}=(value)\n        case value\n        when nil\n          @\#{attribute_name}_unconverted_value = nil\n          @\#{attribute_name} = nil\n        when Time, DateTime\n          @\#{attribute_name}_unconverted_value = value\n          timezone = \#{case options[:timezone]; when Symbol; \"self.send(\#{options[:timezone].inspect})\"; when String; \"ActiveSupport::TimeZone[\#{options[:timezone].inspect}]\"; else; \"ActiveSupport::TimeZone['UTC']\"; end}\n          @\#{attribute_name} = ActiveSupport::TimeWithZone.new(nil, timezone, (value.is_a?(ActiveSupport::TimeWithZone) ? value.time : value))\n        when String\n          @\#{attribute_name}_unconverted_value = value\n          parsed_date = DateTime.parse(value) rescue nil\n          timezone = \#{case options[:timezone]; when Symbol; \"self.send(\#{options[:timezone].inspect})\"; when String; \"ActiveSupport::TimeZone[\#{options[:timezone].inspect}]\"; else; \"ActiveSupport::TimeZone['UTC']\"; end}\n          @\#{attribute_name} = parsed_date ? ActiveSupport::TimeWithZone.new(nil, timezone, parsed_date) : nil\n        else\n          raise ArgumentError, 'Unexpected value passed to date_accessor \#{attribute_name}; expected Time, String, or nil.'\n        end\n      end\n      validate do\n        if self.\#{attribute_name}_unconverted_value.present? != self.\#{attribute_name}.present?\n          self.errors.add(:\#{attribute_name}, :invalid)\n        end\n      end\n    eos\n  end\nend\n"

.decimal_accessor(attribute_name, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/resource_model/base.rb', line 249

def self.decimal_accessor(attribute_name, options={})
  raise ArgumentError, "Expected `#{attribute_name}` to be an instance of Symbol" unless attribute_name.is_a?(Symbol)
  if self.decimal_attributes.include?(attribute_name)
    raise ArgumentError, "Already contains a decimal accessor named `#{attribute_name}`"
  else
    decimal_regex = /\A\s*[+-]?\s*\d*(\d,\d)*\d*\.?\d*\s*\Z/
    @decimal_attributes << attribute_name
    self.class_eval "      attr_reader :\#{attribute_name}\n      attr_reader :\#{attribute_name}_unconverted_value\n      def \#{attribute_name}=(value)\n        @\#{attribute_name}_unconverted_value = value\n        @\#{attribute_name} = value.present? && value.to_s =~ \#{decimal_regex.inspect} ? BigDecimal.new(value.to_s.gsub(/\\\\s|[,]/, '')) : nil\n      end\n      validate do\n        if self.\#{attribute_name}_unconverted_value.present? != self.\#{attribute_name}.present?\n          self.errors.add(:\#{attribute_name}, :not_a_number)\n        end\n      end\n    eos\n  end\nend\n"

.enum_accessor(attribute_name, enums:, raise_error_on_set: false, allow_nil: true) ⇒ Object

Raises:

  • (ArgumentError)


363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'lib/resource_model/base.rb', line 363

def self.enum_accessor(attribute_name, enums:, raise_error_on_set: false, allow_nil: true)
  raise ArgumentError, "Expected `#{attribute_name}` to be an instance of Symbol" unless attribute_name.is_a?(Symbol)
  raise ArgumentError, "Expected `enums`: `#{enums}` to be an instance of Array containing only instances of String" unless enums.is_a?(Array) && enums.all? { |e| e.is_a?(String) }
  raise ArgumentError, "Expected `raise_error_on_set`: `#{raise_error_on_set}` to be an instance of TrueClass or FalseClass" unless raise_error_on_set.is_a?(TrueClass) || raise_error_on_set.is_a?(FalseClass)
  raise ArgumentError, "Expected `allow_nil`: `#{allow_nil}` to be an instance of TrueClass or FalseClass" unless allow_nil.is_a?(TrueClass) || allow_nil.is_a?(FalseClass)

  if self.enum_attributes.include?(attribute_name)
    raise ArgumentError, "Already contains a enum accessor named `#{attribute_name}`"
  else
    @enum_attributes << attribute_name
    self.class_eval "      attr_reader :\#{attribute_name}\n      validates :\#{attribute_name}, inclusion: {in: \#{(enums + (allow_nil ? [nil] : [])).inspect}}\n      def \#{attribute_name}=(value)\n        value = nil unless value.present?\n        if \#{raise_error_on_set} && !((\#{allow_nil} && value.nil?) || \#{enums.inspect}.include?(value))\n          raise ArgumentError, 'Expected to receive valid enum string or nil.'\n        end\n        @\#{attribute_name} = value\n      end\n      def \#{attribute_name}_enums\n        \#{enums.inspect}\n      end\n    eos\n  end\nend\n"

.has_associated_model(attribute_name, options = {}) ⇒ Object

accepts_nested_attributes: nil

Raises:

  • (ArgumentError)


112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/resource_model/base.rb', line 112

def self.has_associated_model(attribute_name, options={}) # accepts_nested_attributes: nil
  raise ArgumentError, "Expected `#{attribute_name}` to be an instance of Symbol" unless attribute_name.is_a?(Symbol)
  if self.associated_model_attributes.include?(attribute_name)
    raise ArgumentError, "Already contains associated model named `#{attribute_name}`"
  else
    @associated_model_attributes << attribute_name
    class_name = options[:class_name] || "::#{attribute_name.to_s.camelcase}"
    self.class_eval "      attr_reader :\#{attribute_name}, :\#{attribute_name}_id\n      def \#{attribute_name}=(model)\n        raise ArgumentError, 'Expected instance of `\#{class_name}`' unless model.nil? || model.is_a?(\#{class_name})\n        @\#{attribute_name}_id = model.present? ? model.id : nil\n        @\#{attribute_name} = model\n      end\n\n      def \#{attribute_name}_id=(id)\n        if id.present?\n          scope = \#{class_name}.where(id: id)\n          eager_loads = \#{options[:eager_load].inspect}\n          if eager_loads\n            scope = scope.eager_load(eager_loads)\n          end\n          self.\#{attribute_name} = scope.first!\n        else\n          self.\#{attribute_name} = nil\n        end\n      end\n    eos\n  end\nend\n"

.has_associated_model_collection(attribute_name, class_name: nil, unique: true) ⇒ Object

Raises:

  • (ArgumentError)


146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/resource_model/base.rb', line 146

def self.has_associated_model_collection(attribute_name, class_name: nil, unique: true)
  raise ArgumentError, "Expected `#{attribute_name}` to be an instance of Symbol" unless attribute_name.is_a?(Symbol)
  if self.associated_model_collection_attributes.include?(attribute_name)
    raise ArgumentError, "Already contains associated model collection named `#{attribute_name}`"
  else
    @associated_model_collection_attributes << attribute_name
    class_name ||= "::#{attribute_name.to_s.singularize.camelcase}"
    self.class_eval "      attr_reader :\#{attribute_name}\n      def \#{attribute_name}_ids\n        self.\#{attribute_name}.collect { |model| model.id }\n      end\n\n      def \#{attribute_name}=(models)\n        case models\n        when nil\n          @\#{attribute_name} = []\n        when Array\n          models.each do |model|\n            unless model.is_a?(\#{class_name})\n              raise ArgumentError, \"Assignment of collection to \#{attribute_name} contained invalid type \\\#{model.class.name}; expected only instances of \#{class_name}\"\n            end\n          end\n          @\#{attribute_name} = models\n        else\n          if models.respond_to?(:to_a)\n            self.\#{attribute_name} = models.to_a\n          else\n            raise ArgumentError, 'Expected instance of array'\n          end\n        end\n      end\n\n      def \#{attribute_name}_ids=(ids)\n        if ids.present?\n          ids = ids.collect do |i|\n            unless i.is_a?(Integer) || (i.is_a?(String) && i =~ /\\\\A\\\\s*\\\\d+\\\\s*\\\\Z/)\n              raise ArgumentError, \"Assignment of collection to \#{attribute_name}_ids contained value (\\\#{i}) not an integer or stringified integer\"\n            end\n            i.to_i\n          end\#{unique ? '.uniq' : ''}\n          self.\#{attribute_name} = \#{class_name}.where(id: ids).to_a\n        else\n          self.\#{attribute_name} = []\n        end\n      end\n    eos\n  end\nend\n"

.has_associated_resource_model(attribute_name, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/resource_model/base.rb', line 24

def self.has_associated_resource_model(attribute_name, options={})
  raise ArgumentError, "Expected `#{attribute_name}` to be an instance of Symbol" unless attribute_name.is_a?(Symbol)
  if self.associated_resource_model_attributes.include?(attribute_name)
    raise ArgumentError, "Already contains associated resource model named `#{attribute_name}`"
  else
    @associated_resource_model_attributes << attribute_name
    class_name = options[:class_name] || "::#{attribute_name.to_s.camelcase}"
    self.class_eval "      attr_reader :\#{attribute_name}\n      def \#{attribute_name}=(model)\n        raise ArgumentError, 'Expected instance of `\#{class_name}`' unless model.nil? || model.is_a?(\#{class_name})\n        @\#{attribute_name}_attributes = nil\n        @\#{attribute_name} = model\n      end\n\n      def \#{attribute_name}_attributes=(attributes)\n        if attributes\n          model = \#{class_name}.new(attributes)\n          self.\#{attribute_name} = model\n        else\n          self.\#{attribute_name} = nil\n        end\n        @\#{attribute_name}_attributes = attributes\n      end\n\n      validate do\n        if self.\#{attribute_name} && !self.\#{attribute_name}.valid?\n          self.errors.add(:\#{attribute_name}, :invalid)\n        end\n      end\n    eos\n  end\nend\n"

.has_associated_resource_model_collection(attribute_name, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/resource_model/base.rb', line 61

def self.has_associated_resource_model_collection(attribute_name, options={})
  raise ArgumentError, "Expected `#{attribute_name}` to be an instance of Symbol" unless attribute_name.is_a?(Symbol)
  if self.associated_resource_model_collection_attributes.include?(attribute_name)
    raise ArgumentError, "Already contains associated resource model collection named `#{attribute_name}`"
  else
    @associated_resource_model_collection_attributes << attribute_name
    class_name = options[:class_name] || "::#{attribute_name.to_s.singularize.camelcase}"
    self.class_eval "      attr_reader :\#{attribute_name}\n      def \#{attribute_name}=(models)\n        case models\n        when nil\n          @\#{attribute_name} = []\n        when Array\n          models.each do |model|\n            unless model.is_a?(\#{class_name})\n              raise ArgumentError, \"Assignment of collection to \#{attribute_name} contained invalid type \\\#{model.class.name}; expected only instances of \#{class_name}\"\n            end\n          end\n          @\#{attribute_name} = models\n        else\n          if models.respond_to?(:to_a)\n            self.\#{attribute_name} = models.to_a\n          else\n            raise ArgumentError, 'Expected instance of array'\n          end\n        end\n      end\n\n      def \#{attribute_name}_attributes=(attributes={})\n        if attributes\n          self.\#{attribute_name} = attributes.collect do |(k, model_attributes)|\n            \#{class_name}.new(model_attributes)\n          end\n        else\n          self.\#{attribute_name} = []\n        end\n      end\n\n      validate do\n        if !self.\#{attribute_name}.inject(true) { |valid, model| model.valid? && valid }\n          self.errors.add(:\#{attribute_name}, :invalid)\n        end\n      end\n    eos\n  end\nend\n"

.inherited(klass) ⇒ Object



390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# File 'lib/resource_model/base.rb', line 390

def self.inherited(klass)
  klass.class_eval "    @associated_model_attributes = []\n    @associated_model_collection_attributes = []\n    @associated_resource_model_attributes = []\n    @associated_resource_model_collection_attributes = []\n    @boolean_attributes = []\n    @integer_attributes = []\n    @decimal_attributes = []\n    @usd_attributes = []\n    @date_attributes = []\n    @string_attributes = []\n    @enum_attributes = []\n  eos\n  class << klass\n    def associated_model_attributes\n      self.superclass.associated_model_attributes + @associated_model_attributes\n    end\n    def associated_model_collection_attributes\n      self.superclass.associated_model_collection_attributes + @associated_model_collection_attributes\n    end\n    def associated_resource_model_attributes\n      self.superclass.associated_resource_model_attributes + @associated_resource_model_attributes\n    end\n    def associated_resource_model_collection_attributes\n      self.superclass.associated_resource_model_collection_attributes + @associated_resource_model_collection_attributes\n    end\n    def boolean_attributes\n      self.superclass.boolean_attributes + @boolean_attributes\n    end\n    def integer_attributes\n      self.superclass.integer_attributes + @integer_attributes\n    end\n    def decimal_attributes\n      self.superclass.decimal_attributes + @decimal_attributes\n    end\n    def usd_attributes\n      self.superclass.usd_attributes + @usd_attributes\n    end\n    def date_attributes\n      self.superclass.date_attributes + @date_attributes\n    end\n    def string_attributes\n      self.superclass.date_attributes + @string_attributes\n    end\n    def enum_attributes\n      self.superclass.date_attributes + @enum_attributes\n    end\n  end\nend\n"

.integer_accessor(attribute_name) ⇒ Object

Raises:

  • (ArgumentError)


224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/resource_model/base.rb', line 224

def self.integer_accessor(attribute_name)
  raise ArgumentError, "Expected `#{attribute_name}` to be an instance of Symbol" unless attribute_name.is_a?(Symbol)
  if self.integer_attributes.include?(attribute_name)
    raise ArgumentError, "Already contains a integer accessor named `#{attribute_name}`"
  else
    @integer_attributes << attribute_name
    self.class_eval "      attr_reader :\#{attribute_name}\n      attr_reader :\#{attribute_name}_unconverted_value\n      def \#{attribute_name}=(value)\n        @\#{attribute_name}_unconverted_value = value\n        @\#{attribute_name} = value.present? && value.to_s =~ /\\\\A\\\\s*\\\\-?\\\\d+\\\\s*\\\\Z/ ? value.to_i : nil\n      end\n      validate do\n        if self.\#{attribute_name}_unconverted_value.present? != self.\#{attribute_name}.present?\n          self.errors.add(:\#{attribute_name}, :not_an_integer)\n        end\n      end\n    eos\n  end\nend\n"

.model_nameObject



9
10
11
# File 'lib/resource_model/base.rb', line 9

def self.model_name
  ActiveModel::Name.new(self, nil, self.name)
end

.string_accessor(attribute_name, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/resource_model/base.rb', line 341

def self.string_accessor(attribute_name, options={})
  raise ArgumentError, "Expected `#{attribute_name}` to be an instance of Symbol" unless attribute_name.is_a?(Symbol)
  if self.string_attributes.include?(attribute_name)
    raise ArgumentError, "Already contains a string accessor named `#{attribute_name}`"
  else
    @string_attributes << attribute_name
    self.class_eval "      attr_reader :\#{attribute_name}\n      def \#{attribute_name}=(value)\n        if \#{options[:strip] == true} && value\n          value = value.strip\n        end\n        value = nil unless value.present?\n        @\#{attribute_name} = value\n      end\n    eos\n  end\nend\n"

.usd_accessor(attribute_name, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/resource_model/base.rb', line 275

def self.usd_accessor(attribute_name, options={})
  raise ArgumentError, "Expected `#{attribute_name}` to be an instance of Symbol" unless attribute_name.is_a?(Symbol)
  if self.usd_attributes.include?(attribute_name)
    raise ArgumentError, "Already contains a usd accessor named `#{attribute_name}`"
  else
    usd_regex = /\A\s*[+-]?\s*\$?\s*\d*(\d,\d)*\d*\.?\d*\s*\Z/
    @usd_attributes << attribute_name
    self.class_eval "      attr_reader :\#{attribute_name}\n      attr_reader :\#{attribute_name}_unconverted_value\n      def \#{attribute_name}=(value)\n        @\#{attribute_name}_unconverted_value = value\n        @\#{attribute_name} = value.present? && value.to_s =~ \#{usd_regex.inspect} ? BigDecimal.new(value.to_s.gsub(/[\\$,]|\\\\s/, '')) : nil\n      end\n      validate do\n        if self.\#{attribute_name}_unconverted_value.present? != self.\#{attribute_name}.present?\n          self.errors.add(:\#{attribute_name}, :invalid)\n        end\n      end\n    eos\n  end\nend\n"

.view_templateObject

Raises:

  • (NotImplementedError)


13
14
15
# File 'lib/resource_model/base.rb', line 13

def self.view_template
  raise NotImplementedError
end

Instance Method Details

#attributes=(attributes) ⇒ Object



445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
# File 'lib/resource_model/base.rb', line 445

def attributes=(attributes)
  (
    self.class.associated_resource_model_collection_attributes +
    self.class.associated_model_collection_attributes
  ).each do |attribute|
    unless (value = self.send(attribute)).is_a?(Array) && value.present?
      self.send("#{attribute}=", [])
    end
  end

  if attributes.present?
    attributes = attributes.dup

    (
      self.class.associated_resource_model_attributes +
      self.class.associated_resource_model_collection_attributes
    ).each do |attribute|
      attribute_attributes_key = "#{attribute}_attributes".to_sym
      if attributes.key?(attribute)
        self.send("#{attribute}=", attributes.delete(attribute))
      end
      if attributes.key?(attribute_attributes_key)
        self.send("#{attribute_attributes_key}=", attributes.delete(attribute_attributes_key))
      end
    end

    self.class.associated_model_attributes.each do |attribute|
      id_attribute = "#{attribute}_id".to_sym
      if attributes.key?(attribute)
        self.send("#{attribute}=", attributes.delete(attribute))
        attributes.delete(id_attribute)
      elsif attributes.key?(id_attribute)
        self.send("#{id_attribute}=", attributes.delete(id_attribute))
      end
    end

    self.class.associated_model_collection_attributes.each do |attribute|
      ids_attribute = "#{attribute}_ids"
      if attributes.key?(attribute)
        self.send("#{attribute}=", attributes.delete(attribute))
        attributes.delete(ids_attribute)
      elsif attributes.key?(ids_attribute)
        self.send("#{ids_attribute}=", attributes.delete(ids_attribute))
      end
    end

    attributes.each do |name, value|
      send("#{name}=", value)
    end
  end
end

#persisted?Boolean



17
18
19
# File 'lib/resource_model/base.rb', line 17

def persisted?
  self.respond_to?(:id) && self.id.present?
end

#to_json_attributesObject



497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
# File 'lib/resource_model/base.rb', line 497

def to_json_attributes
  hash = {}
  self.class.boolean_attributes.each do |attribute_name|
    hash[attribute_name] = self.send(attribute_name)
  end
  self.class.integer_attributes.each do |attribute_name|
    hash[attribute_name] = self.send(attribute_name)
  end
  self.class.decimal_attributes.each do |attribute_name|
    hash[attribute_name] = self.send(attribute_name).andand.to_s
  end
  self.class.usd_attributes.each do |attribute_name|
    hash[attribute_name] = self.send(attribute_name).andand.to_s
  end
  self.class.date_attributes.each do |attribute_name|
    hash[attribute_name] = self.send(attribute_name).andand.iso8601
  end
  self.class.string_attributes.each do |attribute_name|
    hash[attribute_name] = self.send(attribute_name)
  end
  self.class.enum_attributes.each do |attribute_name|
    hash[attribute_name] = self.send(attribute_name)
  end
  self.class.associated_model_attributes.each do |attribute_name|
    id_attribute_name = "#{attribute_name}_id"
    value = self.send(id_attribute_name)
    if value
      hash[id_attribute_name.to_sym] = value
    end
  end
  self.class.associated_model_collection_attributes.each do |attribute_name|
    ids_attribute_name = "#{attribute_name}_ids"
    value = self.send(ids_attribute_name)
    if value
      hash[ids_attribute_name] = value
    end
  end
  self.class.associated_resource_model_attributes.each do |attribute_name|
    value = self.send(attribute_name)
    if value
      hash["#{attribute_name}_attributes".to_sym] = value.to_json_attributes
    end
  end
  self.class.associated_resource_model_collection_attributes.each do |attribute_name|
    hash["#{attribute_name}_attributes".to_sym] = self.send(attribute_name).inject({}) do |sub_hash, item|
      sub_hash[sub_hash.size] = item.to_json_attributes
      sub_hash
    end
  end

  hash
end