Class: DecoLite::Model

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model, FieldCreatable, FieldNamesPersistable, FieldRequireable, 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 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_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(options: {}) ⇒ Model



27
28
29
30
31
32
33
34
# File 'lib/deco_lite/model.rb', line 27

def initialize(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
end

Instance Method Details

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



49
50
51
52
53
54
# File 'lib/deco_lite/model.rb', line 49

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



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/deco_lite/model.rb', line 36

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