lib/alba.rb in alba-1.5.0 vs lib/alba.rb in alba-1.6.0

- old
+ new

@@ -1,21 +1,13 @@ require 'json' require_relative 'alba/version' +require_relative 'alba/errors' require_relative 'alba/resource' require_relative 'alba/deprecation' # Core module module Alba - # Base class for Errors - class Error < StandardError; end - - # Error class for backend which is not supported - class UnsupportedBackend < Error; end - - # Error class for type which is not supported - class UnsupportedType < Error; end - class << self attr_reader :backend, :encoder, :inferring, :_on_error, :_on_nil, :transforming_root_key # Accessor for inflector, a module responsible for incflecting strings attr_accessor :inflector @@ -58,16 +50,20 @@ resource = klass.new(object) resource.serialize(root_key: root_key || key) end # Enable inference for key and resource name - def enable_inference! - begin - require 'active_support/inflector' - rescue LoadError - raise ::Alba::Error, 'To enable inference, please install `ActiveSupport` gem.' + # + # @param with [Symbol, Class, Module] inflector + # When it's a Symbol, it sets inflector with given name + # When it's a Class or a Module, it sets given object to inflector + def enable_inference!(with: :default) + if with == :default + Alba::Deprecation.warn 'Calling `enable_inference!` without `with` keyword argument is deprecated. Pass `:active_support` to keep current behavior.' end + + @inflector = inflector_from(with) @inferring = true end # Disable inference for key and resource name def disable_inference! @@ -78,33 +74,43 @@ # # @param [Symbol] handler # @param [Block] # @raise [ArgumentError] if both handler and block params exist # @raise [ArgumentError] if both handler and block params don't exist + # @deprecated Use `Resource.on_error` instead # @return [void] def on_error(handler = nil, &block) + Alba::Deprecation.warn '`Alba.on_error` is deprecated, use `on_error` on resource class instead.' raise ArgumentError, 'You cannot specify error handler with both Symbol and block' if handler && block raise ArgumentError, 'You must specify error handler with either Symbol or block' unless handler || block @_on_error = handler || block end # Set nil handler # # @param block [Block] # @return [void] + # @deprecated Use `Resource.on_nil` instead def on_nil(&block) + Alba::Deprecation.warn '`Alba.on_nil` is deprecated, use `on_nil` on resource class instead.' @_on_nil = block end # Enable root key transformation + # + # @deprecated Use `Resource.transform_keys` with `root` option instead def enable_root_key_transformation! + Alba::Deprecation.warn '`Alba.enable_root_key_transformation!` is deprecated, use `transform_keys` on resource class instead.' @transforming_root_key = true end # Disable root key transformation + # + # @deprecated Use `Resource.transform_keys` with `root` option instead def disable_root_key_transformation! + Alba::Deprecation.warn '`Alba.disable_root_key_transformation!` is deprecated, use `transform_keys` on resource class instead.' @transforming_root_key = false end # @param block [Block] resource body # @return [Class<Alba::Resource>] resource class @@ -117,13 +123,14 @@ # @param name [String] a String Alba infers resource name with # @param nesting [String, nil] namespace Alba tries to find resource class in # @return [Class<Alba::Resource>] resource class def infer_resource_class(name, nesting: nil) - enable_inference! + raise Alba::Error, 'Inference is disabled so Alba cannot infer resource name. Use `Alba.enable_inference!` before use.' unless Alba.inferring + const_parent = nesting.nil? ? Object : Object.const_get(nesting) - const_parent.const_get("#{ActiveSupport::Inflector.classify(name)}Resource") + const_parent.const_get("#{inflector.classify(name)}Resource") end # Reset config variables # Useful for test cleanup def reset! @@ -133,10 +140,23 @@ @transforming_root_key = false # TODO: This will be true since 2.0 end private + def inflector_from(name_or_module) + case name_or_module + when :default, :active_support + require_relative 'alba/default_inflector' + Alba::DefaultInflector + when :dry + require 'dry/inflector' + Dry::Inflector.new + else + validate_inflector(name_or_module) + end + end + def set_encoder_from_backend @encoder = case @backend when :oj, :oj_strict then try_oj when :oj_rails then try_oj(mode: :rails) when :active_support then try_active_support @@ -164,9 +184,17 @@ def default_encoder lambda do |hash| JSON.dump(hash) end + end + + def validate_inflector(inflector) + unless %i[camelize camelize_lower dasherize classify].all? { |m| inflector.respond_to?(m) } + raise Alba::Error, "Given inflector, #{inflector.inspect} is not valid. It must implement `camelize`, `camelize_lower`, `dasherize` and `classify`." + end + + inflector end end reset! end