Class: DecoLite::Model

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model, FieldAssignable, FieldNamesPersistable, FieldRequireable, FieldsAutoloadable, HashLoadable, Hashable, ModelNameable, Optionable
Defined in:
lib/deco_lite/model.rb

Overview

This class defines the base class for classes that create dynamic models that can be used as decorators.

Constant Summary

Constants included from OptionsValidatable

OptionsValidatable::OPTIONS

Constants included from RequiredFieldsOptionable

RequiredFieldsOptionable::OPTION_REQUIRED_FIELDS, RequiredFieldsOptionable::OPTION_REQUIRED_FIELDS_AUTO, RequiredFieldsOptionable::OPTION_REQUIRED_FIELDS_DEFAULT, RequiredFieldsOptionable::OPTION_REQUIRED_FIELDS_VALUES

Constants included from NamespaceOptionable

NamespaceOptionable::OPTION_NAMESPACE, NamespaceOptionable::OPTION_NAMESPACE_DEFAULT

Constants included from FieldsOptionable

FieldsOptionable::OPTION_FIELDS, FieldsOptionable::OPTION_FIELDS_DEFAULT, FieldsOptionable::OPTION_FIELDS_MERGE, FieldsOptionable::OPTION_FIELDS_STRICT, FieldsOptionable::OPTION_FIELDS_VALUES

Constants included from FieldValidatable

FieldValidatable::FIELD_NAME_REGEX

Instance Attribute Summary

Attributes included from FieldNamesPersistable

#field_names

Instance Method Summary collapse

Methods included from Optionable

#options

Methods included from OptionsValidatable

#validate_option_fields!, #validate_option_keys!, #validate_option_namespace!, #validate_option_required_fields!, #validate_options!, #validate_options_present!

Methods included from ModelNameable

included

Methods included from Hashable

#to_h

Methods included from FieldAssignable

#set_field_value, #set_field_values

Methods included from FieldRetrievable

get_field_value

Methods included from FieldCreatable

#create_field_accessor, #create_field_accessors

Methods included from FieldValidatable

validate_field_name!

Methods included from FieldConflictable

#attr_accessor_exist?, #field_conflict?, #field_names_include?, #validate_field_conflicts!

Methods included from FieldNameNamespaceable

#field_name_or_field_name_with_namespace, #field_name_with_namespace

Methods included from FieldRequireable

#required_field_exist?, #required_fields, #validate_required_fields

Constructor Details

#initialize(hash: {}, options: {}) ⇒ Model

Returns a new instance of Model.



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/deco_lite/model.rb', line 29

def initialize(hash: {}, options: {})
  # Accept whatever options are sent, but make sure
  # we have defaults set up. #options_with_defaults
  # will merge options into OptionsDefaultable::DEFAULT_OPTIONS
  # so we have defaults for any options not passed in through
  # options.
  self.options = Options.with_defaults options

  hash ||= {}

  auto_fields = auto_attr_accessors.merge(hash)
  load!(hash: auto_fields, options: options) if hash.present? || auto_attr_accessors?
end

Instance Method Details

#load(hash:, options: {}) ⇒ Object



56
57
58
59
60
61
# File 'lib/deco_lite/model.rb', line 56

def load(hash:, options: {})
  puts 'WARNING: DecoLite::Model#load will be deprecated in a future release; ' \
       'use DecoLite::Model#load! instead!'

  load!(hash: hash, options: options)
end

#load!(hash:, options: {}) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/deco_lite/model.rb', line 43

def load!(hash:, options: {})
  # Merge options into the default options passed through the
  # constructor; these will override any options passed in when
  # this object was created, allowing us to retain any defaut
  # options while loading, but also provide option customization
  # of options when needed.
  options = Options.with_defaults(options, defaults: self.options)

  load_hash(hash: hash, deco_lite_options: options)

  self
end