Class: Oat::Serializer
- Inherits:
-
Object
- Object
- Oat::Serializer
- Defined in:
- lib/oat/serializer.rb
Instance Attribute Summary collapse
-
#adapter ⇒ Object
readonly
Returns the value of attribute adapter.
-
#adapter_class ⇒ Object
readonly
Returns the value of attribute adapter_class.
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#item ⇒ Object
readonly
Returns the value of attribute item.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(item, context = nil, _adapter_class = nil, parent_serializer = nil) ⇒ Serializer
constructor
A new instance of Serializer.
- #method_missing(name, *args, &block) ⇒ Object
- #to_hash ⇒ Object
- #top ⇒ Object
Constructor Details
#initialize(item, context = nil, _adapter_class = nil, parent_serializer = nil) ⇒ Serializer
Returns a new instance of Serializer.
23 24 25 26 27 28 |
# File 'lib/oat/serializer.rb', line 23 def initialize(item, context = nil, _adapter_class = nil, parent_serializer = nil) @item, @context = item, context @parent_serializer = parent_serializer @adapter_class = _adapter_class || self.class.adapter @adapter = @adapter_class.new(self) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/oat/serializer.rb', line 34 def method_missing(name, *args, &block) if adapter.respond_to?(name) adapter.send(name, *args, &block) else self.class.warn "[#{adapter.class}] does not implement ##{name}. Called with #{args}" end end |
Instance Attribute Details
#adapter ⇒ Object (readonly)
Returns the value of attribute adapter.
21 22 23 |
# File 'lib/oat/serializer.rb', line 21 def adapter @adapter end |
#adapter_class ⇒ Object (readonly)
Returns the value of attribute adapter_class.
21 22 23 |
# File 'lib/oat/serializer.rb', line 21 def adapter_class @adapter_class end |
#context ⇒ Object (readonly)
Returns the value of attribute context.
21 22 23 |
# File 'lib/oat/serializer.rb', line 21 def context @context end |
#item ⇒ Object (readonly)
Returns the value of attribute item.
21 22 23 |
# File 'lib/oat/serializer.rb', line 21 def item @item end |
Class Method Details
.adapter(adapter_class = nil) ⇒ Object
12 13 14 15 |
# File 'lib/oat/serializer.rb', line 12 def self.adapter(adapter_class = nil) self._adapter = adapter_class if adapter_class self._adapter end |
.schema(&block) ⇒ Object
7 8 9 10 |
# File 'lib/oat/serializer.rb', line 7 def self.schema(&block) @schema = block if block_given? @schema || Proc.new{} end |
.warn(msg) ⇒ Object
17 18 19 |
# File 'lib/oat/serializer.rb', line 17 def self.warn(msg) logger ? logger.warning(msg) : puts(msg) end |
Instance Method Details
#to_hash ⇒ Object
42 43 44 45 46 47 |
# File 'lib/oat/serializer.rb', line 42 def to_hash @to_hash ||= ( self.instance_eval &self.class.schema adapter.to_hash ) end |
#top ⇒ Object
30 31 32 |
# File 'lib/oat/serializer.rb', line 30 def top @top ||= @parent_serializer || self end |