Class: AdaptiveConfiguration::Builder

Inherits:
PropertiesBuilder show all
Defined in:
lib/adaptive_configuration/builder.rb

Constant Summary collapse

DEFAULT_CONVERTERS =
{

  Array       => ->( v ) { Array( v ) },
  Date        => ->( v ) { v.respond_to?( :to_date ) ? v.to_date : Date.parse( v.to_s ) },
  Time        => ->( v ) { v.respond_to?( :to_time ) ? v.to_time : Time.parse( v.to_s ) },
  URI         => ->( v ) { URI.parse( v.to_s ) },
  String      => ->( v ) { String( v ) },
  Symbol      => ->( v ) { v.respond_to?( :to_sym ) ? v.to_sym : nil },
  Rational    => ->( v ) { Rational( v ) },
  Float       => ->( v ) { Float( v ) },
  Integer     => ->( v ) { Integer( v ) },
  TrueClass   => ->( v ) { 
    case v
    when Numeric 
      v.nonzero? ? true : nil 
    else
      v.to_s.match(  /\A\s*(true|yes)\s*\z/i ) ? true : nil 
    end
  },
  FalseClass  => ->( v ) {  
    case v
    when Numeric 
      v.zero? ? false : nil 
    else
      v.to_s.match(  /\A\s*(false|no)\s*\z/i ) ? false : nil 
    end
  }

}

Instance Attribute Summary

Attributes inherited from PropertiesBuilder

#definitions

Instance Method Summary collapse

Methods inherited from PropertiesBuilder

#parameter, #parameters

Constructor Details

#initializeBuilder

Returns a new instance of Builder.



42
43
44
45
# File 'lib/adaptive_configuration/builder.rb', line 42

def initialize
  super
  @converters = DEFAULT_CONVERTERS.dup 
end

Instance Method Details

#build(values = nil, &block) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/adaptive_configuration/builder.rb', line 51

def build( values = nil, &block )
  scaffold = AdaptiveConfiguration::Scaffold.new( 
    values,
    converters: @converters, 
    definitions: @definitions 
  )
  scaffold.instance_eval( &block ) if block
  scaffold.to_h
end

#build!(values = nil, &block) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/adaptive_configuration/builder.rb', line 61

def build!( values = nil, &block )
  scaffold = AdaptiveConfiguration::Scaffold.new( 
    values,
    converters: @converters, 
    definitions: @definitions 
  )
  scaffold.instance_eval( &block ) if block
  result = scaffold.to_h 
  validate!( result )
  result 
end

#convert(klass, &block) ⇒ Object



47
48
49
# File 'lib/adaptive_configuration/builder.rb', line 47

def convert( klass, &block )
  @converters[ klass ] = block 
end

#valid?(values) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
90
91
92
# File 'lib/adaptive_configuration/builder.rb', line 87

def valid?( values )
  traverse_and_validate_values( values, definitions: @definitions ) { 
    return false 
  }
  return true
end

#validate(values) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/adaptive_configuration/builder.rb', line 79

def validate( values )
  errors = []
  traverse_and_validate_values( values, definitions: @definitions ) { | error | 
    errors << error 
  }
  errors
end

#validate!(values) ⇒ Object



73
74
75
76
77
# File 'lib/adaptive_configuration/builder.rb', line 73

def validate!( values )
  traverse_and_validate_values( values, definitions: @definitions ) { | error | 
    raise error 
  } 
end