Module: Alba::Resource::InstanceMethods
- Included in:
- Alba::Resource
- Defined in:
- lib/alba/resource.rb,
sig/alba/resource.rbs
Overview
Instance methods available when Resource is included
Instance Attribute Summary collapse
-
#object ⇒ Object
readonly
Returns the value of attribute object.
-
#params ⇒ generic_hash
readonly
Returns the value of attribute params.
Instance Method Summary collapse
- #_setup ⇒ void
-
#as_json(_options = EMPTY_HASH, root_key: nil, meta: EMPTY_HASH) ⇒ Hash
Returns a Hash corresponding #serialize.
- #assoc_name_from_params ⇒ Symbol, String
- #association_editor ⇒ Module?
- #attributes_from_association ⇒ [Alba::key_type, untyped]
- #conditional_attribute? ⇒ Boolean
- #encode ⇒ String
-
#fetch_attribute(obj, key, attribute) ⇒ Object
rubocop:disable Metrics.
- #fetch_key ⇒ String
- #handle_error(error, obj, key, attribute, hash) ⇒ Object
- #hash_with_metadata(hash, meta) ⇒ generic_hash
-
#initialize(object, params: EMPTY_HASH, within: WITHIN_DEFAULT, with_traits: nil, select: nil) ⇒ Object
Initialization.
- #on_nil ⇒ Object
-
#select(_key, _value, _attribute) ⇒ Boolean
Default implementation for selecting attributes Override this method to filter attributes based on key and value.
-
#serializable_hash ⇒ Hash
(also: #to_h)
A Hash for serialization.
-
#serialize(root_key: nil, meta: EMPTY_HASH) ⇒ String
Serialization methods.
- #serialize_with(hash) ⇒ String
-
#to_json(options = EMPTY_HASH, root_key: nil, meta: EMPTY_HASH) ⇒ String
For Rails compatibility The first options is a dummy parameter.
- #transform_key(key) ⇒ Alba::key_type
- #transform_keys ⇒ generic_hash
- #transformable_root? ⇒ Boolean
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 ⇒ generic_hash (readonly)
Returns the value of attribute params.
47 48 49 |
# File 'lib/alba/resource.rb', line 47 def params @params end |
Instance Method Details
#_setup ⇒ void
This method returns an undefined value.
52 |
# File 'sig/alba/resource.rbs', line 52
def _setup: () -> void
|
#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 |
#assoc_name_from_params ⇒ Symbol, String
48 |
# File 'sig/alba/resource.rbs', line 48
def assoc_name_from_params: (Symbol | String name) -> (Symbol | String)
|
#association_editor ⇒ Module?
47 |
# File 'sig/alba/resource.rbs', line 47
def association_editor: () -> Module?
|
#attributes_from_association ⇒ [Alba::key_type, untyped]
46 |
# File 'sig/alba/resource.rbs', line 46
def attributes_from_association: (Association association, untyped obj) -> [Alba::key_type, untyped]
|
#conditional_attribute? ⇒ Boolean
51 |
# File 'sig/alba/resource.rbs', line 51
def conditional_attribute?: (untyped object, ConditionalAttribute attribute) -> bool
|
#encode ⇒ String
38 |
# File 'sig/alba/resource.rbs', line 38
def encode: (generic_hash) -> String
|
#fetch_attribute(obj, key, attribute) ⇒ Object
rubocop:disable Metrics
291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'lib/alba/resource.rb', line 291 def fetch_attribute(obj, key, attribute) # rubocop:disable Metrics value = case attribute when Symbol then fetch_attribute_from_object_and_resource(obj, attribute) when Proc then instance_exec(obj, &attribute) when Alba::Association then yield_if_within(attribute.name.to_sym) { |within| attribute.to_h(obj, params: params, within: within) } when TypedAttribute then attribute.value(object: obj) { |attr| fetch_attribute(obj, key, attr) } when NestedAttribute then attribute.value(object: obj, params: params, within: @within, select: method(:select)) when ConditionalAttribute then attribute.with_passing_condition(resource: self, object: obj) { |attr| fetch_attribute(obj, key, attr) } # :nocov: else raise ::Alba::Error, "Unsupported type of attribute: #{attribute.class}" # :nocov: end value.nil? && nil_handler ? instance_exec(obj, key, attribute, &nil_handler) : value end |
#fetch_key ⇒ String
184 185 186 187 |
# File 'lib/alba/resource.rb', line 184 def fetch_key k = Alba.collection?(@object) ? _key_for_collection : _key transforming_root_key? ? transform_key(k) : k end |
#handle_error(error, obj, key, attribute, hash) ⇒ Object
270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/alba/resource.rb', line 270 def handle_error(error, obj, key, attribute, hash) case (on_error = @_on_error || :raise) when :raise, nil then raise(error) when :nullify then hash[key] = nil when :ignore then nil when Proc key, value = on_error.call(error, obj, key, attribute, self.class) hash[key] = value unless Alba::REMOVE_KEY == key # rubocop:disable Style/YodaCondition else # :nocov: raise Alba::Error, 'Impossible path' # :nocov: end end |
#hash_with_metadata(hash, meta) ⇒ generic_hash
142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/alba/resource.rb', line 142 def (hash, ) return hash if .empty? && @_meta&.last.nil? key, block = @_meta || :meta if key hash[key] = (block, ) else (block, ).each { |k, v| hash[k] = v } end hash end |
#initialize(object, params: EMPTY_HASH, within: WITHIN_DEFAULT, with_traits: nil, select: nil) ⇒ Object
Initialization
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 |
#on_nil ⇒ Object
50 |
# File 'sig/alba/resource.rbs', line 50
def on_nil: (untyped object, Symbol | String key) -> untyped
|
#select(_key, _value, _attribute) ⇒ Boolean
Default implementation for selecting attributes Override this method to filter attributes based on key and value
250 251 252 |
# File 'lib/alba/resource.rb', line 250 def select(_key, _value, _attribute) true 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
Serialization methods
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 |
#serialize_with(hash) ⇒ String
135 136 137 138 139 140 |
# File 'lib/alba/resource.rb', line 135 def serialize_with(hash) serialized_json = encode(hash) return serialized_json unless @_layout @_layout.serialize(resource: self, serialized_json: serialized_json, binding: binding) end |
#to_json(options = EMPTY_HASH, root_key: nil, meta: EMPTY_HASH) ⇒ String
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 |
#transform_key(key) ⇒ Alba::key_type
285 286 287 288 289 |
# File 'lib/alba/resource.rb', line 285 def transform_key(key) return Alba.regularize_key(key) if @_transform_type == :none || key.nil? || key.empty? # We can skip transformation Alba.transform_key(key, transform_type: @_transform_type) end |
#transform_keys ⇒ generic_hash
44 |
# File 'sig/alba/resource.rbs', line 44
def transform_keys: (generic_hash) -> generic_hash
|
#transformable_root? ⇒ Boolean
42 |
# File 'sig/alba/resource.rbs', line 42
def transformable_root?: () -> bool
|