Class: Coercible::Coercer

Inherits:
Object
  • Object
show all
Defined in:
lib/coercible/coercer.rb,
lib/coercible/coercer/date.rb,
lib/coercible/coercer/hash.rb,
lib/coercible/coercer/time.rb,
lib/coercible/coercer/array.rb,
lib/coercible/coercer/float.rb,
lib/coercible/coercer/object.rb,
lib/coercible/coercer/string.rb,
lib/coercible/coercer/symbol.rb,
lib/coercible/coercer/decimal.rb,
lib/coercible/coercer/integer.rb,
lib/coercible/coercer/numeric.rb,
lib/coercible/coercer/date_time.rb,
lib/coercible/coercer/true_class.rb,
lib/coercible/coercer/false_class.rb,
lib/coercible/coercer/configurable.rb,
lib/coercible/coercer/time_coercions.rb

Overview

Coercer object

Examples:


coercer = Coercible::Coercer.new

coercer[String].to_boolean('yes') # => true
coercer[Integer].to_string(1)     # => '1'

Defined Under Namespace

Modules: Configurable, TimeCoercions Classes: Array, Date, DateTime, Decimal, FalseClass, Float, Hash, Integer, Numeric, Object, String, Symbol, Time, TrueClass

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, coercers = {}) ⇒ undefined

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize a new coercer instance

Parameters:



84
85
86
87
# File 'lib/coercible/coercer.rb', line 84

def initialize(config, coercers = {})
  @coercers = coercers
  @config   = config
end

Instance Attribute Details

#coercersArray<Coercer::Object> (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return coercer instances

Returns:



21
22
23
# File 'lib/coercible/coercer.rb', line 21

def coercers
  @coercers
end

#configConfiguration (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns global configuration for coercers

Returns:



28
29
30
# File 'lib/coercible/coercer.rb', line 28

def config
  @config
end

Class Method Details

.new {|| ... } ⇒ Coercer

Build a new coercer

Examples:


Coercible::Coercer.new { |config| # set configuration }

Yield Parameters:

Returns:



41
42
43
44
45
46
47
48
49
50
51
# 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

Instance Method Details

#[](klass) ⇒ Coercer::Object

Access a specific coercer object for the given type

Examples:


coercer[String] # => string coercer
coercer[Integer] # => integer coercer

Parameters:

  • type (Class)

Returns:



101
102
103
# File 'lib/coercible/coercer.rb', line 101

def [](klass)
  coercers[klass] || initialize_coercer(klass)
end