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_to_hash(obj, hash) ⇒ generic_hash
- #binding ⇒ Binding
- #check_within(association_name) ⇒ Object
-
#class ⇒ Class
These methods are available on the eventual resource instance.
- #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) ⇒ 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
58 |
# File 'sig/alba/resource.rbs', line 58
def __send__: (Symbol, *untyped) -> untyped
|
#_fetch_attribute_from_object_first(obj, attribute) ⇒ Object
268 269 270 271 272 273 274 |
# File 'lib/alba/resource.rb', line 268 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
276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/alba/resource.rb', line 276 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
175 176 177 178 |
# File 'lib/alba/resource.rb', line 175 def _key k = @_key == true ? resource_name(pluralized: false) : @_key Alba.regularize_key(k) end |
#_key_for_collection ⇒ root_key_type
170 171 172 173 |
# File 'lib/alba/resource.rb', line 170 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
148 149 150 |
# File 'lib/alba/resource.rb', line 148 def (block, ) block ? instance_eval(&block).merge() : end |
#_setup ⇒ void
This method returns an undefined value.
51 |
# File 'sig/alba/resource.rbs', line 51
def _setup: () -> void
|
#as_json(_options = EMPTY_HASH, root_key: nil, meta: EMPTY_HASH) ⇒ Hash
Returns a Hash corresponding #serialize
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/alba/resource.rb', line 94 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_to_hash(obj, hash) ⇒ generic_hash
195 196 197 198 199 200 201 202 203 204 |
# File 'lib/alba/resource.rb', line 195 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
59 |
# File 'sig/alba/resource.rbs', line 59
def binding: () -> Binding
|
#check_within(association_name) ⇒ Object
297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/alba/resource.rb', line 297 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.
55 |
# File 'sig/alba/resource.rbs', line 55
def class: () -> Class
|
#do_select(key, value, attribute) ⇒ Boolean
221 222 223 224 225 226 |
# File 'lib/alba/resource.rb', line 221 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
60 |
# File 'sig/alba/resource.rbs', line 60
def encode: (untyped) -> String
|
#fetch_attribute(obj, key, attribute) ⇒ Object
rubocop:disable Metrics
249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/alba/resource.rb', line 249 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 nil_handler && value.nil? ? instance_exec(obj, key, attribute, &nil_handler) : value end |
#fetch_attribute_from_object_and_resource(obj, attribute) ⇒ Object
264 265 266 |
# File 'lib/alba/resource.rb', line 264 def fetch_attribute_from_object_and_resource(obj, attribute) _fetch_attribute_from_resource_first(obj, attribute) end |
#fetch_key ⇒ String
165 166 167 168 |
# File 'lib/alba/resource.rb', line 165 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
228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/alba/resource.rb', line 228 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
115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/alba/resource.rb', line 115 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
135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/alba/resource.rb', line 135 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) ⇒ InstanceMethods
Returns a new instance of InstanceMethods.
54 55 56 57 58 59 60 |
# File 'lib/alba/resource.rb', line 54 def initialize(object, params: EMPTY_HASH, within: WITHIN_DEFAULT, with_traits: nil) @object = object @params = params @within = within @with_traits = with_traits _setup end |
#instance_eval ⇒ void
This method returns an undefined value.
56 |
# File 'sig/alba/resource.rbs', line 56
def instance_eval: [A] () { () [self: self] -> A } -> A
|
#instance_exec ⇒ void
This method returns an undefined value.
57 |
# File 'sig/alba/resource.rbs', line 57
def instance_exec: [A] (*untyped) { (*untyped) [self: self] -> A } -> A
|
#nil_handler ⇒ Object
288 289 290 |
# File 'lib/alba/resource.rb', line 288 def nil_handler @_on_nil end |
#resource_name(pluralized: false) ⇒ String
180 181 182 183 184 185 186 187 188 189 |
# File 'lib/alba/resource.rb', line 180 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
208 209 210 |
# File 'lib/alba/resource.rb', line 208 def select(_key, _value, _attribute) true end |
#serializable_hash ⇒ Hash Also known as: to_h
A Hash for serialization
108 109 110 |
# File 'lib/alba/resource.rb', line 108 def serializable_hash Alba.collection?(@object) ? serializable_hash_for_collection : attributes_to_hash(@object, {}) end |
#serializable_hash_for_collection ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/alba/resource.rb', line 152 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
67 68 69 |
# File 'lib/alba/resource.rb', line 67 def serialize(root_key: nil, meta: EMPTY_HASH) serialize_with(as_json(root_key: root_key, meta: )) end |
#serialize_with(hash) ⇒ String
128 129 130 131 132 133 |
# File 'lib/alba/resource.rb', line 128 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
212 213 214 215 216 217 218 219 |
# File 'lib/alba/resource.rb', line 212 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
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/alba/resource.rb', line 76 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
243 244 245 246 247 |
# File 'lib/alba/resource.rb', line 243 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
191 192 193 |
# File 'lib/alba/resource.rb', line 191 def transforming_root_key? @_transforming_root_key end |
#yield_if_within(association_name) {|arg0| ... } ⇒ Object
292 293 294 295 |
# File 'lib/alba/resource.rb', line 292 def yield_if_within(association_name) within = check_within(association_name) within ? yield(within) : Alba::REMOVE_KEY end |