Class: Coercible::Coercer

Inherits:
Object
  • Object
show all
Defined in:
lib/project/coercer.rb,
lib/project/coercer/date.rb,
lib/project/coercer/hash.rb,
lib/project/coercer/time.rb,
lib/project/coercer/array.rb,
lib/project/coercer/float.rb,
lib/project/coercer/object.rb,
lib/project/coercer/string.rb,
lib/project/coercer/symbol.rb,
lib/project/coercer/decimal.rb,
lib/project/coercer/integer.rb,
lib/project/coercer/numeric.rb,
lib/project/coercer/date_time.rb,
lib/project/coercer/true_class.rb,
lib/project/coercer/false_class.rb,
lib/project/coercer/configurable.rb,
lib/project/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:



86
87
88
89
# File 'lib/project/coercer.rb', line 86

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:



23
24
25
# File 'lib/project/coercer.rb', line 23

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:



30
31
32
# File 'lib/project/coercer.rb', line 30

def config
  @config
end

Class Method Details

.new {|| ... } ⇒ Coercer

Build a new coercer

Examples:


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

Yield Parameters:

Returns:



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/project/coercer.rb', line 43

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:



103
104
105
# File 'lib/project/coercer.rb', line 103

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