Module: Alba::Resource::InstanceMethods
- Included in:
- Alba::Resource
- Defined in:
- lib/alba/resource.rb,
sig/alba/resource.rbs
Overview
Instance methods
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
- #__send__ ⇒ Object
- #_fetch_attribute_from_object_first(obj, attribute) ⇒ Object
- #_fetch_attribute_from_resource_first(obj, attribute) ⇒ Object
- #_key ⇒ root_key_type
- #_key_for_collection ⇒ root_key_type
- #_metadata(block, meta) ⇒ generic_hash
- #_setup ⇒ void
-
#as_json(_options = EMPTY_HASH, root_key: nil, meta: EMPTY_HASH) ⇒ Hash
Returns a Hash corresponding #serialize.
-
#attributes ⇒ Hash[Symbol | String, attribute]
deprecated
Deprecated.
in favor of
select - #attributes_to_hash(obj, hash) ⇒ generic_hash
- #binding ⇒ Binding
- #check_within(association_name) ⇒ Object
-
#class ⇒ Class
These methods are available on the eventual resource instance.
- #collection_converter ⇒ Proc
- #converter ⇒ Proc
- #deprecated_serializable_hash ⇒ Object
- #deprecated_serializable_hash_for_collection ⇒ Object
- #do_select(key, value, attribute) ⇒ Boolean
- #encode ⇒ String
-
#fetch_attribute(obj, key, attribute) ⇒ Object
rubocop:disable Metrics.
- #fetch_attribute_from_object_and_resource(obj, attribute) ⇒ Object
- #fetch_key ⇒ String
- #handle_error(error, obj, key, attribute, hash) ⇒ Object
- #hash_from_traits(obj) ⇒ Object
- #hash_with_metadata(hash, meta) ⇒ Object
-
#initialize(object, params: EMPTY_HASH, within: WITHIN_DEFAULT, with_traits: nil, select: nil) ⇒ InstanceMethods
A new instance of InstanceMethods.
- #instance_eval ⇒ void
- #instance_exec ⇒ void
- #nil_handler ⇒ Object
- #resource_name(pluralized: false) ⇒ String
-
#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.
- #serializable_hash_for_collection ⇒ Object
-
#serialize(root_key: nil, meta: EMPTY_HASH) ⇒ String
Serialize object into JSON string.
- #serialize_with(hash) ⇒ String
- #set_key_and_attribute_body_from(obj, key, attribute, hash) ⇒ Object
-
#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) ⇒ Object
- #transforming_root_key? ⇒ Boolean
- #yield_if_within(association_name) {|arg0| ... } ⇒ Object
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
#__send__ ⇒ Object
62 |
# File 'sig/alba/resource.rbs', line 62
def __send__: (Symbol, *untyped) -> untyped
|
#_fetch_attribute_from_object_first(obj, attribute) ⇒ Object
309 310 311 312 313 314 315 |
# File 'lib/alba/resource.rb', line 309 def _fetch_attribute_from_object_first(obj, attribute) return obj.fetch(attribute) if obj.is_a?(Hash) obj.__send__(attribute) rescue NoMethodError, KeyError __send__(attribute, obj) end |
#_fetch_attribute_from_resource_first(obj, attribute) ⇒ Object
317 318 319 320 321 322 323 324 325 326 327 |
# File 'lib/alba/resource.rb', line 317 def _fetch_attribute_from_resource_first(obj, attribute) return obj.fetch(attribute) if obj.is_a?(Hash) if @_resource_methods.include?(attribute) __send__(attribute, obj) else obj.__send__(attribute) end rescue KeyError __send__(attribute, obj) end |
#_key ⇒ root_key_type
193 194 195 196 |
# File 'lib/alba/resource.rb', line 193 def _key k = @_key == true ? resource_name(pluralized: false) : @_key Alba.regularize_key(k) end |
#_key_for_collection ⇒ root_key_type
188 189 190 191 |
# File 'lib/alba/resource.rb', line 188 def _key_for_collection k = @_key_for_collection == true ? resource_name(pluralized: true) : @_key_for_collection Alba.regularize_key(k) end |
#_metadata(block, meta) ⇒ generic_hash
154 155 156 |
# File 'lib/alba/resource.rb', line 154 def (block, ) block ? instance_eval(&block).merge() : end |
#_setup ⇒ void
This method returns an undefined value.
55 |
# File 'sig/alba/resource.rbs', line 55
def _setup: () -> void
|
#as_json(_options = EMPTY_HASH, root_key: nil, meta: EMPTY_HASH) ⇒ Hash
Returns a Hash corresponding #serialize
96 97 98 99 100 101 102 103 104 105 |
# File 'lib/alba/resource.rb', line 96 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 |
#attributes ⇒ Hash[Symbol | String, attribute]
in favor of select
This is default behavior for getting attributes for serialization Override this method to filter certain attributes
243 244 245 |
# File 'lib/alba/resource.rb', line 243 def attributes @_attributes end |
#attributes_to_hash(obj, hash) ⇒ generic_hash
228 229 230 231 232 233 234 235 236 237 |
# File 'lib/alba/resource.rb', line 228 def attributes_to_hash(obj, hash) attributes.each do |key, attribute| set_key_and_attribute_body_from(obj, key, attribute, hash) rescue ::Alba::Error, FrozenError, TypeError raise rescue StandardError => e handle_error(e, obj, key, attribute, hash) end @with_traits.nil? ? hash : hash.merge!(hash_from_traits(obj)) end |
#binding ⇒ Binding
63 |
# File 'sig/alba/resource.rbs', line 63
def binding: () -> Binding
|
#check_within(association_name) ⇒ Object
338 339 340 341 342 343 344 345 346 347 348 |
# File 'lib/alba/resource.rb', line 338 def check_within(association_name) case @within when WITHIN_DEFAULT then WITHIN_DEFAULT # Default value, doesn't check within tree when Hash then @within.fetch(association_name, nil) # Traverse within tree when Array then @within.find { |item| item.to_sym == association_name } when Symbol then @within == association_name when nil, true, false then false # Stop here else raise Alba::Error, "Unknown type for within option: #{@within.class}" end end |
#class ⇒ Class
These methods are available on the eventual resource instance. Declaring them here models the module's inclusion context for Steep.
59 |
# File 'sig/alba/resource.rbs', line 59
def class: () -> Class
|
#collection_converter ⇒ Proc
219 220 221 222 223 224 225 226 |
# File 'lib/alba/resource.rb', line 219 def collection_converter lambda do |obj, a| a << {} h = a.last attributes_to_hash(obj, h) a end end |
#converter ⇒ Proc
213 214 215 216 217 |
# File 'lib/alba/resource.rb', line 213 def converter lambda do |obj| attributes_to_hash(obj, {}) end end |
#deprecated_serializable_hash ⇒ Object
130 131 132 |
# File 'lib/alba/resource.rb', line 130 def deprecated_serializable_hash Alba.collection?(@object) ? serializable_hash_for_collection : converter.call(@object) end |
#deprecated_serializable_hash_for_collection ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/alba/resource.rb', line 170 def deprecated_serializable_hash_for_collection if @_collection_key @object.to_h do |item| k = item.public_send(@_collection_key) key = Alba.regularize_key(k) [key, converter.call(item)] end else @object.each_with_object([], &collection_converter) end end |
#do_select(key, value, attribute) ⇒ Boolean
262 263 264 265 266 267 |
# File 'lib/alba/resource.rb', line 262 def do_select(key, value, attribute) # `select` can be overridden with both 2 and 3 parameters # Here we check the arity and build arguments accordingly args = @_select_arity == 3 ? [key, value, attribute] : [key, value] select(*args) end |
#encode ⇒ String
64 |
# File 'sig/alba/resource.rbs', line 64
def encode: (untyped) -> String
|
#fetch_attribute(obj, key, attribute) ⇒ Object
rubocop:disable Metrics
290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/alba/resource.rb', line 290 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) 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_attribute_from_object_and_resource(obj, attribute) ⇒ Object
305 306 307 |
# File 'lib/alba/resource.rb', line 305 def fetch_attribute_from_object_and_resource(obj, attribute) _fetch_attribute_from_resource_first(obj, attribute) end |
#fetch_key ⇒ String
183 184 185 186 |
# File 'lib/alba/resource.rb', line 183 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
269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/alba/resource.rb', line 269 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_from_traits(obj) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/alba/resource.rb', line 117 def hash_from_traits(obj) return {} if @with_traits.nil? Array(@with_traits).each_with_object({}) do |trait, hash| body = @_traits.fetch(trait) { raise Alba::Error, "Trait not found: #{trait}" } resource_class = Class.new(self.class) resource_class.instance_variable_set(:@_attributes, {}) resource_class.class_eval(&body) resource_class.transform_keys(@_transform_type) unless @_transform_type == :none hash.merge!(resource_class.new(obj, params: params, within: @within).serializable_hash) end end |
#hash_with_metadata(hash, meta) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/alba/resource.rb', line 141 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) ⇒ InstanceMethods
Returns a new instance of InstanceMethods.
55 56 57 58 59 60 61 62 |
# 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 warn '`select` keyword for Alba::Resource is deprecated', category: :deprecated, uplevel: 1 if select _setup end |
#instance_eval ⇒ void
This method returns an undefined value.
60 |
# File 'sig/alba/resource.rbs', line 60
def instance_eval: [A] () { () [self: self] -> A } -> A
|
#instance_exec ⇒ void
This method returns an undefined value.
61 |
# File 'sig/alba/resource.rbs', line 61
def instance_exec: [A] (*untyped) { (*untyped) [self: self] -> A } -> A
|
#nil_handler ⇒ Object
329 330 331 |
# File 'lib/alba/resource.rb', line 329 def nil_handler @_on_nil end |
#resource_name(pluralized: false) ⇒ String
198 199 200 201 202 203 204 205 206 207 |
# File 'lib/alba/resource.rb', line 198 def resource_name(pluralized: false) inflector = Alba.inflector raise Alba::Error, 'You must set inflector when setting root key as true.' unless inflector class_name = self.class.name suffix = class_name.end_with?('Resource') ? 'Resource' : 'Serializer' name = inflector.demodulize(class_name).delete_suffix(suffix) underscore_name = inflector.underscore(name) pluralized ? inflector.pluralize(underscore_name) : underscore_name end |
#select(_key, _value, _attribute) ⇒ Boolean
Default implementation for selecting attributes Override this method to filter attributes based on key and value
249 250 251 |
# File 'lib/alba/resource.rb', line 249 def select(_key, _value, _attribute) true end |
#serializable_hash ⇒ Hash Also known as: to_h
A Hash for serialization
110 111 112 |
# File 'lib/alba/resource.rb', line 110 def serializable_hash Alba.collection?(@object) ? serializable_hash_for_collection : attributes_to_hash(@object, {}) end |
#serializable_hash_for_collection ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/alba/resource.rb', line 158 def serializable_hash_for_collection if @_collection_key @object.to_h do |item| k = item.public_send(@_collection_key) key = Alba.regularize_key(k) [key, attributes_to_hash(item, {})] end else @object.map { |obj| attributes_to_hash(obj, {}) } end end |
#serialize(root_key: nil, meta: EMPTY_HASH) ⇒ String
Serialize object into JSON string
69 70 71 |
# File 'lib/alba/resource.rb', line 69 def serialize(root_key: nil, meta: EMPTY_HASH) serialize_with(as_json(root_key: root_key, meta: )) end |
#serialize_with(hash) ⇒ String
134 135 136 137 138 139 |
# File 'lib/alba/resource.rb', line 134 def serialize_with(hash) serialized_json = encode(hash) return serialized_json unless @_layout @_layout.serialize(resource: self, serialized_json: serialized_json, binding: binding) end |
#set_key_and_attribute_body_from(obj, key, attribute, hash) ⇒ Object
253 254 255 256 257 258 259 260 |
# File 'lib/alba/resource.rb', line 253 def set_key_and_attribute_body_from(obj, key, attribute, hash) key = transform_key(key) value = fetch_attribute(obj, key, attribute) # When `select` is not overridden, skip calling it for better performance return if !@_select_arity.nil? && !do_select(key, value, attribute) hash[key] = value unless Alba::REMOVE_KEY == value # rubocop:disable Style/YodaCondition end |
#to_json(options = EMPTY_HASH, root_key: nil, meta: EMPTY_HASH) ⇒ String
For Rails compatibility The first options is a dummy parameter
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/alba/resource.rb', line 78 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) ⇒ Object
284 285 286 287 288 |
# File 'lib/alba/resource.rb', line 284 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 |
#transforming_root_key? ⇒ Boolean
209 210 211 |
# File 'lib/alba/resource.rb', line 209 def transforming_root_key? @_transforming_root_key end |
#yield_if_within(association_name) {|arg0| ... } ⇒ Object
333 334 335 336 |
# File 'lib/alba/resource.rb', line 333 def yield_if_within(association_name) within = check_within(association_name) within ? yield(within) : Alba::REMOVE_KEY end |