lib/arstotzka/class_methods.rb in arstotzka-1.3.0 vs lib/arstotzka/class_methods.rb in arstotzka-1.3.1
- old
+ new
@@ -10,12 +10,12 @@
#
# @param attribute [Symbol,String] attribute key
# @param options [Arstotzka::Options] fetcher options
#
# @return [Artotzka::FetcherBuilder]
- def add_fetcher(attribute, options)
- fetcher_builders[attribute.to_sym] = FetcherBuilder.new(options.merge(key: attribute))
+ def add_fetcher(attribute, options = {})
+ fetcher_builders[attribute] = FetcherBuilder.new(options.merge(key: attribute))
end
# @api private
#
# Return the fetcher for an attribute and instance
@@ -25,13 +25,45 @@
# @param attribute [Symbol,String] Name of method that will use this Fetcher
# @param instance [Object] instance that will contain the Hash needed by fetcher
#
# @return [Arstotzka::Fetcher]
def fetcher_for(attribute, instance)
- fetcher_builders[attribute.to_sym].build(instance)
+ return builder_for(attribute).build(instance) if fetcher_for?(attribute)
+
+ raise Exception::FetcherBuilderNotFound.new(attribute, self)
end
+ protected
+
+ # @api private
+ #
+ # Checks if class can build a fetcher for attribute
+ #
+ # @param attribute [::Symbol]
+ #
+ # @return [TrueClass,FalseClass]
+ def fetcher_for?(attribute)
+ return true if fetcher_builders.key?(attribute)
+ return unless superclass.include?(Arstotzka)
+
+ superclass.fetcher_for?(attribute)
+ end
+
+ # @api private
+ #
+ # Returns fetcher builder for an attribute
+ #
+ # @param attribute [::Symbol]
+ #
+ # @return [Arstotzka::FetcherBuilder]
+ def builder_for(attribute)
+ builder = fetcher_builders[attribute]
+ return superclass.builder_for(attribute) unless builder
+
+ builder
+ end
+
private
# @api public
# @!visibility public
#
@@ -67,10 +99,10 @@
# @see
# https://www.rubydoc.info/gems/activesupport/5.2.2/ActiveSupport/Concern
# ActiveSupport::Concern
def expose(*attr_names, **options_hash)
options = Options.new(options_hash.symbolize_keys)
- MethodBuilder.new(attr_names, self, options).build
+ MethodBuilder.new(attr_names.map(&:to_sym), self, options).build
end
# @private
# @api private
#