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: {}) ⇒ 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: {}) ⇒ Base
Returns a new instance of Base.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/active_module/base.rb', line 7 def initialize(possible_modules_or_mapping = [], possible_modules: [], mapping: {}) 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
24 25 26 27 28 |
# File 'lib/active_module/base.rb', line 24 def ==(other) other.is_a?(Base) && possible_modules == other.possible_modules && mapping == other.mapping end |
#cast(value) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/active_module/base.rb', line 38 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
61 62 63 |
# File 'lib/active_module/base.rb', line 61 def deserialize(str) from_db[str] end |
#serializable?(object) ⇒ Boolean
34 35 36 |
# File 'lib/active_module/base.rb', line 34 def serializable?(object) possible_module?(object) end |
#serialize(module_instance) ⇒ Object
57 58 59 |
# File 'lib/active_module/base.rb', line 57 def serialize(module_instance) mapping[cast(module_instance)] end |
#type ⇒ Object
30 31 32 |
# File 'lib/active_module/base.rb', line 30 def type :active_module end |