Class: EasySerializer::Base
- Inherits:
-
Object
- Object
- EasySerializer::Base
- Includes:
- Helpers
- Defined in:
- lib/easy_serializer/base.rb
Instance Attribute Summary collapse
-
#object ⇒ Object
readonly
Returns the value of attribute object.
Class Method Summary collapse
- .attribute(name, opts = {}, &block) ⇒ Object
- .attributes(*args) ⇒ Object
- .cache(bool, opts = {}, &block) ⇒ Object
- .call(obj) ⇒ Object (also: serialize, to_hash, to_h)
- .collection(name, opts = {}, &block) ⇒ Object
Instance Method Summary collapse
- #__cache ⇒ Object
-
#initialize(object) ⇒ Base
constructor
A new instance of Base.
- #send_to_serializer(serializer, value) ⇒ Object
- #serialize ⇒ Object (also: #to_h, #to_hash)
Methods included from Helpers
Constructor Details
#initialize(object) ⇒ Base
Returns a new instance of Base.
7 8 9 |
# File 'lib/easy_serializer/base.rb', line 7 def initialize(object) @object = object end |
Instance Attribute Details
#object ⇒ Object (readonly)
Returns the value of attribute object.
6 7 8 |
# File 'lib/easy_serializer/base.rb', line 6 def object @object end |
Class Method Details
.attribute(name, opts = {}, &block) ⇒ Object
20 21 22 23 |
# File 'lib/easy_serializer/base.rb', line 20 def attribute(name, opts = {}, &block) @__serializable_attributes ||= [] @__serializable_attributes << Attribute.new(name, opts, block) end |
.attributes(*args) ⇒ Object
31 32 33 34 35 |
# File 'lib/easy_serializer/base.rb', line 31 def attributes(*args) args.each do |input| attribute(*input) end end |
.cache(bool, opts = {}, &block) ⇒ Object
25 26 27 28 29 |
# File 'lib/easy_serializer/base.rb', line 25 def cache(bool, opts = {}, &block) if bool @__cache = opts.merge(block: block) end end |
.call(obj) ⇒ Object Also known as: serialize, to_hash, to_h
13 14 15 |
# File 'lib/easy_serializer/base.rb', line 13 def call(obj) new(obj).serialize end |
.collection(name, opts = {}, &block) ⇒ Object
37 38 39 40 |
# File 'lib/easy_serializer/base.rb', line 37 def collection(name, opts = {}, &block) @__serializable_attributes ||= [] @__serializable_attributes << Collection.new(name, opts, block) end |
Instance Method Details
#__cache ⇒ Object
55 56 57 |
# File 'lib/easy_serializer/base.rb', line 55 def __cache self.class.instance_variable_get(:@__cache) end |
#send_to_serializer(serializer, value) ⇒ Object
50 51 52 53 |
# File 'lib/easy_serializer/base.rb', line 50 def send_to_serializer(serializer, value) return unless value option_to_value(serializer, value).call(value) end |
#serialize ⇒ Object Also known as: to_h, to_hash
43 44 45 |
# File 'lib/easy_serializer/base.rb', line 43 def serialize @serialize ||= serialized_from_cache || _serialize end |