Class: DecoLite::Model

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model, FieldAssignable, FieldNamesPersistable, 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 collapse

MISSING_REQUIRED_FIELD_ERROR_TYPE =
:missing_required_field

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 collapse

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 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 FieldAssignable

#set_field_value, #set_field_values

Methods included from FieldRetrievable

get_field_value

Constructor Details

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

Returns a new instance of Model.



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

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 ||= {}

  load_hash!(hash: auto_attr_accessors, options:, add_loaded_fields: false) if auto_attr_accessors?

  load_hash!(hash:, options:) if hash.present?
end

Instance Attribute Details

#required_fieldsObject

Returns field names that will be used to validate whether or not these fields were loaded from hashes upon construction (#new) or via #load!.

You must override this method if you want to return field names that are required to be present.



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

def required_fields
  @required_fields ||= %i[]
end

Instance Method Details

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



54
55
56
# File 'lib/deco_lite/model.rb', line 54

def load!(hash:, options: {})
  load_hash! hash:, options:
end