class Coercible::Coercer
Coercer object
@example
coercer = Coercible::Coercer.new coercer[String].to_boolean('yes') # => true coercer[Integer].to_string(1) # => '1'
@api public
Attributes
Return coercer instances
@return [Array<Coercer::Object>]
@api private
Returns global configuration for coercers
@return [Configuration]
@api private
Public Class Methods
Build a new coercer
@example
Coercible::Coercer.new { |config| # set configuration }
@yieldparam [Configuration]
@return [Coercer]
@api public
# File lib/coercible/coercer.rb, line 41 def self.new(&block) configuration = Configuration.build(config_keys) configurable_coercers.each do |coercer| configuration.send("#{coercer.config_name}=", coercer.config) end yield(configuration) if block_given? super(configuration) end
Initialize a new coercer instance
@param [Hash] coercers
@param [Configuration] config
@return [undefined]
@api private
# File lib/coercible/coercer.rb, line 84 def initialize(config, coercers = {}) @coercers = coercers @config = config end
Private Class Methods
Return configuration keys for Coercer instance
@return [Array<Symbol>]
@api private
# File lib/coercible/coercer.rb, line 58 def self.config_keys configurable_coercers.map(&:config_name) end
Return coercer classes that are configurable
@return [Array<Class>]
@api private
# File lib/coercible/coercer.rb, line 68 def self.configurable_coercers(&block) Coercer::Object.descendants.select { |descendant| descendant.respond_to?(:config) } end
Public Instance Methods
Access a specific coercer object for the given type
@example
coercer[String] # => string coercer coercer[Integer] # => integer coercer
@param [Class] type
@return [Coercer::Object]
@api public
# File lib/coercible/coercer.rb, line 101 def [](klass) coercers[klass] || initialize_coercer(klass) end
Private Instance Methods
Find configuration for the given coercer type
@return [Configuration]
@api private
# File lib/coercible/coercer.rb, line 130 def config_for(coercer) config.send(coercer.config_name) end
Initialize a new coercer instance for the given type
If a coercer class supports configuration it will receive it from the global configuration object
@return [Coercer::Object]
@api private
# File lib/coercible/coercer.rb, line 115 def initialize_coercer(klass) coercers[klass] = begin coercer = Coercer::Object.determine_type(klass) || Coercer::Object args = [ self ] args << config_for(coercer) if coercer.respond_to?(:config_name) coercer.new(*args) end end