lib/alba/resource.rb in alba-3.0.0 vs lib/alba/resource.rb in alba-3.0.1
- old
+ new
@@ -10,11 +10,11 @@
module Alba
# This module represents what should be serialized
module Resource
# @!parse include InstanceMethods
# @!parse extend ClassMethods
- INTERNAL_VARIABLES = {_attributes: {}, _key: nil, _key_for_collection: nil, _meta: nil, _transform_type: :none, _transforming_root_key: false, _key_transformation_cascade: true, _on_error: nil, _on_nil: nil, _layout: nil, _collection_key: nil, _helper: nil}.freeze # rubocop:disable Layout/LineLength
+ INTERNAL_VARIABLES = {_attributes: {}, _key: nil, _key_for_collection: nil, _meta: nil, _transform_type: :none, _transforming_root_key: false, _key_transformation_cascade: true, _on_error: nil, _on_nil: nil, _layout: nil, _collection_key: nil, _helper: nil, _resource_methods: []}.freeze # rubocop:disable Layout/LineLength
private_constant :INTERNAL_VARIABLES
WITHIN_DEFAULT = Object.new.freeze
private_constant :WITHIN_DEFAULT
@@ -263,13 +263,19 @@
rescue NoMethodError
__send__(attribute, obj)
end
def _fetch_attribute_from_resource_first(obj, attribute)
- __send__(attribute, obj)
- rescue NoMethodError
- obj.__send__(attribute)
+ if @_resource_methods.include?(attribute)
+ begin
+ __send__(attribute, obj)
+ rescue NoMethodError
+ obj.__send__(attribute)
+ end
+ else
+ obj.__send__(attribute)
+ end
end
def nil_handler
@_on_nil
end
@@ -299,9 +305,15 @@
end
# Class methods
module ClassMethods
attr_reader(*INTERNAL_VARIABLES.keys)
+
+ # This `method_added` is used for defining "resource methods"
+ def method_added(method_name)
+ _resource_methods << method_name.to_sym unless method_name.to_sym == :_setup
+ super
+ end
# @private
def inherited(subclass)
super
INTERNAL_VARIABLES.each_key { |name| subclass.instance_variable_set("@#{name}", instance_variable_get("@#{name}").clone) }