Class: AdaptiveConfiguration::PropertiesBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/adaptive_configuration/properties_builder.rb

Direct Known Subclasses

Builder

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ PropertiesBuilder

Returns a new instance of PropertiesBuilder.



8
9
10
11
# File 'lib/adaptive_configuration/properties_builder.rb', line 8

def initialize( &block )
  @definitions = {}
  self.instance_eval( &block ) if block_given?
end

Instance Attribute Details

#definitionsObject (readonly)

Returns the value of attribute definitions.



6
7
8
# File 'lib/adaptive_configuration/properties_builder.rb', line 6

def definitions
  @definitions
end

Instance Method Details

#parameter(name, *args) ⇒ Object

Raises:

  • (NameError)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/adaptive_configuration/properties_builder.rb', line 13

def parameter( name, *args )
  name = name.to_sym 
  options = nil

  raise NameError, "The name '#{name}' is reserved and cannot be used for parameters." \
    if AdaptiveConfiguration::Scaffold.instance_methods.include?( name )

  if args.first.is_a?( ::Hash )
    # when called without type: parameter :stream, as: :streams
    options = args.first
  else
    # when called with type: parameter :stream, Boolean, as: :streams
    options = args[ 1 ] || {}
    options[ :type ] = args.first
  end

  validate_in!( name, options[ :type ], options[ :in ] ) if options[ :in ] 
  
  @definitions[ name ] = options
end

#parameters(name, options = {}, &block) ⇒ Object

Raises:

  • (NameError)


34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/adaptive_configuration/properties_builder.rb', line 34

def parameters( name, options = {}, &block )
  
  raise NameError, "The name '#{name}' is reserved and cannot be used for parameters." \
    if AdaptiveConfiguration::Scaffold.instance_methods.include?( name )
  
  builder = PropertiesBuilder.new
  builder.instance_eval( &block ) if block
  @definitions[ name ] = options.merge( { 
    type: Object, 
    definitions: builder.definitions 
  } )

end