Class: Calyx::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/calyx/options.rb

Overview

Provides access to configuration options while evaluating a grammar.

Constant Summary collapse

DEFAULTS =

These options are used by default if not explicitly provided during initialization of the grammar.

{
  strict: true
}

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Options

Constructs a new options instance, merging the passed in options with the defaults.

Parameters:



14
15
16
# File 'lib/calyx/options.rb', line 14

def initialize(options={})
  @options = DEFAULTS.merge(options)
end

Instance Method Details

#merge(options) ⇒ Calyx::Options

Merges two instances together and returns a new instance.

Parameters:

Returns:



59
60
61
# File 'lib/calyx/options.rb', line 59

def merge(options)
  Options.new(@options.merge(options.to_h))
end

#randFloat

Returns the next pseudo-random number in the sequence defined by the internal random number generator state.

The value returned is a floating point number between 0.0 and 1.0, including 0.0 and excluding 1.0.

Returns:

  • (Float)


42
43
44
# File 'lib/calyx/options.rb', line 42

def rand
  rng.rand
end

#rngRandom

Returns the internal random number generator instance. If a seed or random instance is not passed-in directly, a new instance of ‘Random` is initialized by default.

Returns:

  • (Random)


23
24
25
26
27
28
29
30
31
32
33
# File 'lib/calyx/options.rb', line 23

def rng
  unless @options[:rng]
    @options[:rng] = if @options[:seed]
      Random.new(@options[:seed])
    else
      Random.new
    end
  end

  @options[:rng]
end

#strict?TrueClass, FalseClass

True if the strict mode option is enabled. This option defines whether or not to raise an error on missing rules. When set to false, missing rules are skipped over when the production is concatenated.

Returns:

  • (TrueClass, FalseClass)


51
52
53
# File 'lib/calyx/options.rb', line 51

def strict?
  @options[:strict]
end

#to_hHash

Serializes instance data to a hash.

Returns:

  • (Hash)


66
67
68
# File 'lib/calyx/options.rb', line 66

def to_h
  @options.dup
end