Class: CinnamonSerial::Base
- Inherits:
-
Object
- Object
- CinnamonSerial::Base
- Extended by:
- Dsl
- Defined in:
- lib/cinnamon_serial/base.rb
Overview
This is the main parent class that all serializers must inherit from.
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#klasses ⇒ Object
readonly
Returns the value of attribute klasses.
-
#obj ⇒ Object
readonly
Returns the value of attribute obj.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
Class Method Summary collapse
Instance Method Summary collapse
- #[](attr) ⇒ Object
- #as_json(_options = {}) ⇒ Object
- #dig_opt(*keys) ⇒ Object
-
#initialize(obj, opts = {}, klasses = Set.new) ⇒ Base
constructor
A new instance of Base.
- #method_missing(method_sym, *arguments, &block) ⇒ Object
- #respond_to_missing?(method_sym, include_private = false) ⇒ Boolean
Methods included from Dsl
cinnamon_serial_specification, hydrate, serialize
Constructor Details
#initialize(obj, opts = {}, klasses = Set.new) ⇒ Base
Returns a new instance of Base.
26 27 28 29 30 31 32 33 |
# File 'lib/cinnamon_serial/base.rb', line 26 def initialize(obj, opts = {}, klasses = Set.new) @obj = normalize_object(obj) @opts = opts || {} @klasses = klasses materialize_data execute_hydrate_blocks end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_sym, *arguments, &block) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/cinnamon_serial/base.rb', line 47 def method_missing(method_sym, *arguments, &block) key = method_sym.to_s.sub('set_', '') if data.key?(method_sym.to_s) data[method_sym.to_s] elsif data.key?(key) @data[key] = arguments[0] else super end end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
21 22 23 |
# File 'lib/cinnamon_serial/base.rb', line 21 def data @data end |
#klasses ⇒ Object (readonly)
Returns the value of attribute klasses.
21 22 23 |
# File 'lib/cinnamon_serial/base.rb', line 21 def klasses @klasses end |
#obj ⇒ Object (readonly)
Returns the value of attribute obj.
21 22 23 |
# File 'lib/cinnamon_serial/base.rb', line 21 def obj @obj end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
21 22 23 |
# File 'lib/cinnamon_serial/base.rb', line 21 def opts @opts end |
Class Method Details
.map(enumerable, opts = {}) ⇒ Object
16 17 18 |
# File 'lib/cinnamon_serial/base.rb', line 16 def map(enumerable, opts = {}) enumerable.map { |e| new(e, opts) } end |
Instance Method Details
#[](attr) ⇒ Object
59 60 61 |
# File 'lib/cinnamon_serial/base.rb', line 59 def [](attr) send(attr) end |
#as_json(_options = {}) ⇒ Object
39 40 41 |
# File 'lib/cinnamon_serial/base.rb', line 39 def as_json( = {}) data end |
#dig_opt(*keys) ⇒ Object
35 36 37 |
# File 'lib/cinnamon_serial/base.rb', line 35 def dig_opt(*keys) opts.dig(*keys) end |
#respond_to_missing?(method_sym, include_private = false) ⇒ Boolean
43 44 45 |
# File 'lib/cinnamon_serial/base.rb', line 43 def respond_to_missing?(method_sym, include_private = false) data.key?(method_sym.to_s) || super end |