Class: ActiveModule::Base
- Inherits:
-
ActiveModel::Type::Value
- Object
- ActiveModel::Type::Value
- ActiveModule::Base
- Defined in:
- lib/active_module/base.rb
Instance Attribute Summary collapse
-
#mapping ⇒ Object
readonly
Returns the value of attribute mapping.
-
#possible_modules ⇒ Object
readonly
Returns the value of attribute possible_modules.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #cast(value) ⇒ Object
- #deserialize(str) ⇒ Object
-
#initialize(possible_modules_or_mapping = [], possible_modules: [], mapping: {}, enum_compatibility: false) ⇒ Base
constructor
A new instance of Base.
- #serializable?(object) ⇒ Boolean
- #serialize(module_instance) ⇒ Object
- #type ⇒ Object
Constructor Details
#initialize(possible_modules_or_mapping = [], possible_modules: [], mapping: {}, enum_compatibility: false) ⇒ Base
Returns a new instance of Base.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/active_module/base.rb', line 7 def initialize(possible_modules_or_mapping = [], possible_modules: [], mapping: {}, enum_compatibility: false) @enum_compatibility = enum_compatibility if possible_modules_or_mapping.is_a?(Array) @possible_modules = (possible_modules_or_mapping + possible_modules + mapping.keys).uniq @mapping = default_mapping.merge(mapping) else @possible_modules = (possible_modules_or_mapping.keys + possible_modules + mapping.keys) .uniq @mapping = default_mapping.merge(possible_modules_or_mapping) .merge(mapping) end super() end |
Instance Attribute Details
#mapping ⇒ Object (readonly)
Returns the value of attribute mapping.
5 6 7 |
# File 'lib/active_module/base.rb', line 5 def mapping @mapping end |
#possible_modules ⇒ Object (readonly)
Returns the value of attribute possible_modules.
5 6 7 |
# File 'lib/active_module/base.rb', line 5 def possible_modules @possible_modules end |
Instance Method Details
#==(other) ⇒ Object
26 27 28 29 30 |
# File 'lib/active_module/base.rb', line 26 def ==(other) other.is_a?(Base) && possible_modules == other.possible_modules && mapping == other.mapping end |
#cast(value) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/active_module/base.rb', line 40 def cast(value) case value when nil nil when ::Symbol sym_to_module(value) when ::Module if possible_module?(value) value else raise_invalid_module_value_error(value) end when ::String str_to_module(value) else raise_invalid_module_value_error(value) end end |
#deserialize(str) ⇒ Object
63 64 65 |
# File 'lib/active_module/base.rb', line 63 def deserialize(str) from_db[str] end |
#serializable?(object) ⇒ Boolean
36 37 38 |
# File 'lib/active_module/base.rb', line 36 def serializable?(object) possible_module?(object) end |
#serialize(module_instance) ⇒ Object
59 60 61 |
# File 'lib/active_module/base.rb', line 59 def serialize(module_instance) mapping[cast(module_instance)] end |
#type ⇒ Object
32 33 34 |
# File 'lib/active_module/base.rb', line 32 def type :active_module end |