lib/alba/resource.rb in alba-3.0.2 vs lib/alba/resource.rb in alba-3.0.3
- old
+ new
@@ -24,11 +24,11 @@
super
setup_method_body = 'private def _setup;'
base.class_eval do
# Initialize
INTERNAL_VARIABLES.each do |name, initial|
- instance_variable_set("@#{name}", initial.dup) unless instance_variable_defined?("@#{name}")
+ instance_variable_set(:"@#{name}", initial.dup) unless instance_variable_defined?(:"@#{name}")
setup_method_body << "@#{name} = self.class.#{name};"
end
base.define_method(:encode, Alba.encoder)
end
setup_method_body << 'end'
@@ -69,14 +69,15 @@
_to_json(root_key, meta, options)
end
# Returns a Hash correspondng {#serialize}
#
+ # @param _options [Hash] dummy parameter for Rails compatibility
# @param root_key [Symbol, nil, true]
# @param meta [Hash] metadata for this seialization
# @return [Hash]
- def as_json(root_key: nil, meta: {})
+ def as_json(_options = {}, 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}
hash_with_metadata(h, meta)
@@ -244,11 +245,11 @@
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(obj)
- when NestedAttribute then attribute.value(object: obj, params: params)
+ 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) }
else raise ::Alba::Error, "Unsupported type of attribute: #{attribute.class}"
end
value.nil? && nil_handler ? instance_exec(obj, key, attribute, &nil_handler) : value
end
@@ -314,10 +315,10 @@
end
# @private
def inherited(subclass)
super
- INTERNAL_VARIABLES.each_key { |name| subclass.instance_variable_set("@#{name}", instance_variable_get("@#{name}").clone) }
+ INTERNAL_VARIABLES.each_key { |name| subclass.instance_variable_set(:"@#{name}", instance_variable_get(:"@#{name}").clone) }
end
# Defining methods for DSLs and disable parameter number check since for users' benefits increasing params is fine
# Set multiple attributes at once