Class: DecoLite::Model
- Inherits:
-
Object
- Object
- DecoLite::Model
- 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
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
Instance Method Summary collapse
-
#initialize(hash: {}, options: {}) ⇒ Model
constructor
A new instance of Model.
- #load(hash:, options: {}) ⇒ Object
- #load!(hash:, options: {}) ⇒ Object
Methods included from Optionable
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
Methods included from Hashable
Methods included from FieldAssignable
#set_field_value, #set_field_values
Methods included from FieldRetrievable
Methods included from FieldCreatable
#create_field_accessor, #create_field_accessors
Methods included from FieldValidatable
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.with_defaults hash ||= {} auto_fields = auto_attr_accessors.merge(hash) load!(hash: auto_fields, 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: ) 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.with_defaults(, defaults: self.) load_hash(hash: hash, deco_lite_options: ) self end |