Class: PositionalGenerator::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/helpers/positional_generator.rb

Defined Under Namespace

Classes: Computed, Group, Int, Letter, Literal, Oneof

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(as_type) ⇒ Builder

Returns a new instance of Builder.



37
38
39
40
# File 'lib/helpers/positional_generator.rb', line 37

def initialize(as_type)
  @components = []
  @as_type = as_type
end

Instance Attribute Details

#as_typeObject (readonly)

Returns the value of attribute as_type.



35
36
37
# File 'lib/helpers/positional_generator.rb', line 35

def as_type
  @as_type
end

Instance Method Details

#buildString

Generate the value.

Returns:

  • (String)

    if as_type is :string



161
162
163
164
165
166
# File 'lib/helpers/positional_generator.rb', line 161

def build
  graph = build_graph
  stack = build_stack(graph)
  values = generate_values(stack)
  convert(values)
end

#computed(name: nil, deps: [], &block) ⇒ void

This method returns an undefined value.

Fill the position with an arbitrary value.

Examples:

Today’s date

computed do
  Date.today
end

A check digit

int(name: :a, length: 5)
computed(deps: [:a]) do |a|
  a.to_s.bytes.sum % 10
end

Parameters:

  • name (Symbol) (defaults to: nil)

    the name for this node in the group

  • deps (Array<Symbol>) (defaults to: [])

    the name of other fields that this one depends on

  • block (Method)

    the block that yields the arbitrary value. Its arguments are the deps.



117
118
119
# File 'lib/helpers/positional_generator.rb', line 117

def computed(name: nil, deps: [], &block)
  @components << Component.new(@components.count, name, deps, Computed.new(block))
end

#group(name: nil, &block) ⇒ void

This method returns an undefined value.

A group of generators. Useful for #oneof.

Parameters:

  • name (Symbol) (defaults to: nil)

    the name for this node in the group

  • block (Method)

    a subgenerator block



153
154
155
# File 'lib/helpers/positional_generator.rb', line 153

def group(name: nil, &block)
  @components << Component.new(@components.count, name, [], Group.new(@as_type, block))
end

#int(name: nil, length: 1, ranges: nil) ⇒ void

This method returns an undefined value.

Generate a value in the range of 0..9.

Examples:

a digit

int

five digits named :a

int(name: :a, length: 5)

digits of any length between 4 to 10

int(ranges: [1_000 .. 9_999_999_999)

Parameters:

  • name (Symbol) (defaults to: nil)

    the name for this node in the group

  • length (Integer) (defaults to: 1)

    how many digits to generate

  • ranges (Array<Range, Array, Set>) (defaults to: nil)

    an array of limitations on the generation. Elements can be a Range to select from within that range, or an Array or Set to select an element from within the list.



60
61
62
# File 'lib/helpers/positional_generator.rb', line 60

def int(name: nil, length: 1, ranges: nil)
  @components << Component.new(@components.count, name, [], Int.new(length, ranges))
end

#letter(name: nil, length: 1, ranges: ['a'..'z', 'A'..'Z']) ⇒ void

This method returns an undefined value.

Generate a value in the range of ‘a’..‘Z’.

Examples:

Generate a letter

letter

Generate five uppercase letters named :b

letter(name: :b, length: 5, ranges: ['A'..'Z'])

Generate three-letter strings from within specific values

letter(ranges: ['700'..'799', '7A0'..'7F9'])

Parameters:

  • name (Symbol) (defaults to: nil)

    the name for this node in the group

  • length (Integer, Range) (defaults to: 1)

    how many letters to generate

  • ranges (Array<Range, Array, Set>) (defaults to: ['a'..'z', 'A'..'Z'])

    an array of limitations on the generation. Elements can be a Range to select from within that range, or an Array or Set to select an element from within the list.



82
83
84
# File 'lib/helpers/positional_generator.rb', line 82

def letter(name: nil, length: 1, ranges: ['a'..'z', 'A'..'Z'])
  @components << Component.new(@components.count, name, [], Letter.new(length, ranges))
end

#lit(value, name: nil) ⇒ void

This method returns an undefined value.

Generate a literal String

Examples:

lit("-")

Parameters:

  • value (String)
  • name (Symbol) (defaults to: nil)

    the name for this node in the group



94
95
96
# File 'lib/helpers/positional_generator.rb', line 94

def lit(value, name: nil)
  @components << Component.new(@components.count, name, [], Literal.new(value))
end

#oneof(name: nil, &block) ⇒ void

This method returns an undefined value.

Fill the position with one of the results from the given generators.

Examples:

Either five digits, or two letters

oneof do |or_else|
  or_else.int(length: 5)
  or_else.letter(length: 2)
end

Either one letter; or a slash, five digits, then a slash.

oneof do |or_else|
  or_else.letter
  or_else.group do |g_|
    g_.lit("/")
    g_.digit(length: 5)
    g_.lit("/")
  end
end

Parameters:

  • name (Symbol) (defaults to: nil)

    the name for this node in the group

  • block (Method)

    subgenerator block



143
144
145
# File 'lib/helpers/positional_generator.rb', line 143

def oneof(name: nil, &block)
  @components << Component.new(@components.count, name, [], Oneof.new(self, block))
end