Module: Alba::Resource::InstanceMethods
- Included in:
- Alba::Resource
- Defined in:
- lib/alba/resource.rb
Overview
Instance methods
Instance Attribute Summary collapse
-
#object ⇒ Object
readonly
Returns the value of attribute object.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
Instance Method Summary collapse
-
#as_json(root_key: nil, meta: {}) ⇒ Hash
Returns a Hash correspondng #serialize.
- #initialize(object, params: {}, within: WITHIN_DEFAULT) ⇒ Object
-
#serializable_hash ⇒ Hash
(also: #to_h)
A Hash for serialization.
-
#serialize(root_key: nil, meta: {}) ⇒ String
Serialize object into JSON string.
-
#to_json(options = {}, root_key: nil, meta: {}) ⇒ Object
For Rails compatibility The first options is a dummy parameter.
Instance Attribute Details
#object ⇒ Object (readonly)
Returns the value of attribute object.
42 43 44 |
# File 'lib/alba/resource.rb', line 42 def object @object end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
42 43 44 |
# File 'lib/alba/resource.rb', line 42 def params @params end |
Instance Method Details
#as_json(root_key: nil, meta: {}) ⇒ Hash
Returns a Hash correspondng #serialize
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/alba/resource.rb', line 77 def as_json(root_key: nil, meta: {}) key = root_key.nil? ? fetch_key : root_key key = Alba.regularize_key(key) if key && !key.empty? h = {key => serializable_hash} (h, ) else serializable_hash end end |
#initialize(object, params: {}, within: WITHIN_DEFAULT) ⇒ Object
47 48 49 50 51 52 |
# File 'lib/alba/resource.rb', line 47 def initialize(object, params: {}, within: WITHIN_DEFAULT) @object = object @params = params @within = within _setup end |
#serializable_hash ⇒ Hash Also known as: to_h
A Hash for serialization
91 92 93 |
# File 'lib/alba/resource.rb', line 91 def serializable_hash collection? ? serializable_hash_for_collection : converter.call(@object) end |
#serialize(root_key: nil, meta: {}) ⇒ String
Serialize object into JSON string
59 60 61 |
# File 'lib/alba/resource.rb', line 59 def serialize(root_key: nil, meta: {}) serialize_with(as_json(root_key: root_key, meta: )) end |
#to_json(options = {}, root_key: nil, meta: {}) ⇒ Object
For Rails compatibility The first options is a dummy parameter
68 69 70 |
# File 'lib/alba/resource.rb', line 68 def to_json( = {}, root_key: nil, meta: {}) _to_json(root_key, , ) end |