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(_options = EMPTY_HASH, root_key: nil, meta: EMPTY_HASH) ⇒ Hash
Returns a Hash corresponding #serialize.
- #initialize(object, params: EMPTY_HASH, within: WITHIN_DEFAULT, with_traits: nil, select: nil) ⇒ Object
-
#serializable_hash ⇒ Hash
(also: #to_h)
A Hash for serialization.
-
#serialize(root_key: nil, meta: EMPTY_HASH) ⇒ String
Serialize object into JSON string.
-
#to_json(options = EMPTY_HASH, root_key: nil, meta: EMPTY_HASH) ⇒ Object
For Rails compatibility The first options is a dummy parameter.
Instance Attribute Details
#object ⇒ Object (readonly)
Returns the value of attribute object.
47 48 49 |
# File 'lib/alba/resource.rb', line 47 def object @object end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
47 48 49 |
# File 'lib/alba/resource.rb', line 47 def params @params end |
Instance Method Details
#as_json(_options = EMPTY_HASH, root_key: nil, meta: EMPTY_HASH) ⇒ Hash
Returns a Hash corresponding #serialize
98 99 100 101 102 103 104 105 106 107 |
# File 'lib/alba/resource.rb', line 98 def as_json( = EMPTY_HASH, root_key: nil, meta: EMPTY_HASH) 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: EMPTY_HASH, within: WITHIN_DEFAULT, with_traits: nil, select: nil) ⇒ Object
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/alba/resource.rb', line 55 def initialize(object, params: EMPTY_HASH, within: WITHIN_DEFAULT, with_traits: nil, select: nil) @object = object @params = params @within = within @with_traits = with_traits # select override to share the same method with `trait` and `nested_attribute` # Trait and NestedAttribute generates anonymous class so it checks if it's anonymous class to prevent accidental overriding self.class.define_method(:select, &select) if select && self.class.name.nil? _setup end |
#serializable_hash ⇒ Hash Also known as: to_h
A Hash for serialization
112 113 114 |
# File 'lib/alba/resource.rb', line 112 def serializable_hash Alba.collection?(@object) ? serializable_hash_for_collection : attributes_to_hash(@object, {}) end |
#serialize(root_key: nil, meta: EMPTY_HASH) ⇒ String
Serialize object into JSON string
71 72 73 |
# File 'lib/alba/resource.rb', line 71 def serialize(root_key: nil, meta: EMPTY_HASH) serialize_with(as_json(root_key: root_key, meta: )) end |
#to_json(options = EMPTY_HASH, root_key: nil, meta: EMPTY_HASH) ⇒ Object
For Rails compatibility The first options is a dummy parameter
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/alba/resource.rb', line 80 def to_json( = EMPTY_HASH, root_key: nil, meta: EMPTY_HASH) confusing_keys = [:only, :except] = .keys.select { |k| confusing_keys.include?(k.to_sym) } unless .empty? .sort! .map! { |s| "\"#{s}\"" } = "You passed #{.join(' and ')} options but ignored. Please refer to the document: https://github.com/okuramasafumi/alba/blob/main/docs/rails.md" Kernel.warn() end serialize(root_key: root_key, meta: ) end |