lib/alba/resource.rb in alba-2.4.3 vs lib/alba/resource.rb in alba-3.0.0
- old
+ new
@@ -1,20 +1,21 @@
require_relative 'association'
require_relative 'conditional_attribute'
require_relative 'constants'
+require_relative 'type'
require_relative 'typed_attribute'
require_relative 'nested_attribute'
require_relative 'deprecation'
require_relative 'layout'
module Alba
# This module represents what should be serialized
module Resource
# @!parse include InstanceMethods
# @!parse extend ClassMethods
- DSLS = {_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 :DSLS
+ 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
+ private_constant :INTERNAL_VARIABLES
WITHIN_DEFAULT = Object.new.freeze
private_constant :WITHIN_DEFAULT
# `setup` method is meta-programmatically defined here for performance.
@@ -22,11 +23,11 @@
def self.included(base) # rubocop:disable Metrics/MethodLength
super
setup_method_body = 'private def _setup;'
base.class_eval do
# Initialize
- DSLS.each do |name, initial|
+ INTERNAL_VARIABLES.each do |name, initial|
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
@@ -57,29 +58,17 @@
# @return [String] serialized JSON string
def serialize(root_key: nil, meta: {})
serialize_with(as_json(root_key: root_key, meta: meta))
end
- if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('3.0')
- # For Rails compatibility
- # The first options is a dummy parameter but required
- # You can pass empty Hash if you don't want to pass any arguments
- #
- # @see #serialize
- # @see https://github.com/rails/rails/blob/7-0-stable/actionpack/lib/action_controller/metal/renderers.rb#L156
- def to_json(options, root_key: nil, meta: {})
- _to_json(root_key, meta, options)
- end
- else
- # For Rails compatibility
- # The first options is a dummy parameter
- #
- # @see #serialize
- # @see https://github.com/rails/rails/blob/7-0-stable/actionpack/lib/action_controller/metal/renderers.rb#L156
- def to_json(options = {}, root_key: nil, meta: {})
- _to_json(root_key, meta, options)
- end
+ # For Rails compatibility
+ # The first options is a dummy parameter
+ #
+ # @see #serialize
+ # @see https://github.com/rails/rails/blob/7-0-stable/actionpack/lib/action_controller/metal/renderers.rb#L156
+ def to_json(options = {}, root_key: nil, meta: {})
+ _to_json(root_key, meta, options)
end
# Returns a Hash correspondng {#serialize}
#
# @param root_key [Symbol, nil, true]
@@ -263,31 +252,24 @@
else raise ::Alba::Error, "Unsupported type of attribute: #{attribute.class}"
end
value.nil? && nil_handler ? instance_exec(obj, key, attribute, &nil_handler) : value
end
- # TODO: from version 3, `_fetch_attribute_from_resource_first` is default
def fetch_attribute_from_object_and_resource(obj, attribute)
- _fetch_attribute_from_object_first(obj, attribute)
+ _fetch_attribute_from_resource_first(obj, attribute)
end
def _fetch_attribute_from_object_first(obj, attribute)
obj.__send__(attribute)
rescue NoMethodError
__send__(attribute, obj)
end
def _fetch_attribute_from_resource_first(obj, attribute)
- if @_resource_methods.include?(attribute)
- begin
- __send__(attribute, obj)
- rescue NoMethodError
- obj.__send__(attribute)
- end
- else
- obj.__send__(attribute)
- end
+ __send__(attribute, obj)
+ rescue NoMethodError
+ obj.__send__(attribute)
end
def nil_handler
@_on_nil
end
@@ -316,22 +298,16 @@
end
end
# Class methods
module ClassMethods
- attr_reader(*DSLS.keys)
+ 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
- DSLS.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
@@ -392,13 +368,13 @@
# @option options [Proc] if a condition to decide if this association should be serialized
# @param block [Block]
# @return [void]
# @see Alba::Association#initialize
def association(name, condition = nil, resource: nil, key: nil, params: {}, **options, &block)
- key_transformation = @_key_transformation_cascade ? @_transform_type : :none
+ transformation = @_key_transformation_cascade ? @_transform_type : :none
assoc = Association.new(
- name: name, condition: condition, resource: resource, params: params, nesting: nesting, key_transformation: key_transformation, helper: @_helper, &block
+ name: name, condition: condition, resource: resource, params: params, nesting: nesting, key_transformation: transformation, helper: @_helper, &block
)
@_attributes[key&.to_sym || name.to_sym] = options[:if] ? ConditionalAttribute.new(body: assoc, condition: options[:if]) : assoc
end
alias one association
alias many association
@@ -543,6 +519,9 @@
def prefer_object_method!
alias_method :fetch_attribute_from_object_and_resource, :_fetch_attribute_from_object_first
end
end
end
+
+ Serializer = Resource
+ public_constant :Serializer
end