Class: Cryptoform::Config::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/cryptoform/config/builder.rb

Constant Summary collapse

PORTS =
1..65_535
Config =
Data.define(:port, :states)

Instance Method Summary collapse

Constructor Details

#initialize(cryptofile) ⇒ Builder

Returns a new instance of Builder.



7
8
9
10
11
12
# File 'lib/cryptoform/config/builder.rb', line 7

def initialize(cryptofile)
  @port = 3000
  @states = {}

  instance_eval(cryptofile)
end

Instance Method Details

#configObject



32
33
34
# File 'lib/cryptoform/config/builder.rb', line 32

def config
  Config.new(@port, @states.transform_values(&:config))
end

#port(port) ⇒ Object



14
15
16
# File 'lib/cryptoform/config/builder.rb', line 14

def port(port)
  @port = port
end

#state(name) ⇒ Object

Raises:

  • (ArgumentError)


18
19
20
21
22
23
24
# File 'lib/cryptoform/config/builder.rb', line 18

def state(name, &)
  name = name.to_sym

  raise ArgumentError, "state #{name} already configured" if @states.key?(name)

  @states[name] = Cryptoform::Config::StateConfigBuilder.new(name, &)
end

#validate!Object



26
27
28
29
30
# File 'lib/cryptoform/config/builder.rb', line 26

def validate!
  @states.each_value(&:validate!)
  raise Cryptoform::ConfigValidationError, "at least one state must be configured" if @states.empty?
  raise Cryptoform::ConfigValidationError, "port must be an in range 0-65545" unless PORTS.include?(@port)
end