Class: Remap::Compiler

Inherits:
Object
  • Object
show all
Extended by:
Dry::Initializer, Forwardable
Includes:
Dry::Core::Constants
Defined in:
lib/remap/compiler.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.call(&block) ⇒ Rule

Constructs a rule tree given block

Returns:



17
18
19
20
21
22
23
# File 'lib/remap/compiler.rb', line 17

def self.call(&block)
  unless block
    return Rule::Void.new
  end

  new.tap { _1.instance_eval(&block) }.rule
end

Instance Method Details

#allRule::Path::Segment::Quantifier::All

Selects all elements

Returns:

  • (Rule::Path::Segment::Quantifier::All)


109
110
111
# File 'lib/remap/compiler.rb', line 109

def all
  Selector::All.new(EMPTY_HASH)
end

#at(index) ⇒ Path::Segment::Key

Selects index element in input

Parameters:

  • index (Integer)

Returns:

  • (Path::Segment::Key)

Raises:

  • (ArgumentError)

    if index is not an Integer



137
138
139
140
141
# File 'lib/remap/compiler.rb', line 137

def at(index)
  Selector::Index.new(index: index)
rescue Dry::Struct::Error
  raise ArgumentError, "Selector at(index) requires an integer argument, got [#{index}] (#{index.class})"
end

#each(&block) ⇒ Rule::Each

Iterates over the input value, passes each value to its block and merges the result back together

Returns:

Raises:

  • (ArgumentError)

    if no block given



80
81
82
83
84
85
86
# File 'lib/remap/compiler.rb', line 80

def each(&block)
  unless block
    raise ArgumentError, "no block given"
  end

  add Rule::Each.new(rule: call(&block))
end

#embed(mapper) ⇒ Rule::Embed

Maps using mapper

Parameters:

Returns:



46
47
48
49
50
# File 'lib/remap/compiler.rb', line 46

def embed(mapper)
  add Rule::Embed.new(mapper: mapper)
rescue Dry::Struct::Error
  raise ArgumentError, "Embeded mapper must be [Remap::Mapper], got [#{mapper}]"
end

#firstPath::Segment::Key Also known as: any

Selects first element in input

Returns:

  • (Path::Segment::Key)

    ]



146
147
148
# File 'lib/remap/compiler.rb', line 146

def first
  at(0)
end

#lastPath::Segment::Key

Selects last element in input

Returns:

  • (Path::Segment::Key)


154
155
156
# File 'lib/remap/compiler.rb', line 154

def last
  at(-1)
end

#map(*path, to: EMPTY_ARRAY, &block) ⇒ Rule::Map

Maps path to #to with block inbetween

Parameters:

  • path ([])
    Array<Segment>, Segment
  • to ([]) (defaults to: EMPTY_ARRAY)
    Array<Symbol>, Symbol

Returns:



31
32
33
34
35
36
37
38
39
# File 'lib/remap/compiler.rb', line 31

def map(*path, to: EMPTY_ARRAY, &block)
  add Rule::Map.new(
    path: {
      map: path.flatten,
      to: [to].flatten
    },
    rule: call(&block)
  )
end

#option(id) ⇒ Rule::Static::Option

Static option to be selected

Parameters:

  • id (Symbol)

Returns:

  • (Rule::Static::Option)


127
128
129
# File 'lib/remap/compiler.rb', line 127

def option(id)
  Static::Option.new(name: id)
end

#ruleRule

The final rule

Returns:



161
162
163
# File 'lib/remap/compiler.rb', line 161

def rule
  Rule::Collection.call(rules: rules)
end

#selfRule

Returns:



12
# File 'lib/remap/compiler.rb', line 12

delegate call: self

#set(*path, to:) ⇒ Rule::Set

Parameters:

  • *path ([])
    Symbol, Array<Symbol>
  • to (Hash)

    a customizable set of options

Options Hash (to:):

Returns:

Raises:

  • (ArgumentError)

    if no path given if path is not a Symbol or Array<Symbol>



59
60
61
62
63
# File 'lib/remap/compiler.rb', line 59

def set(*path, to:)
  add Rule::Set.new(path: { to: path.flatten, map: EMPTY_ARRAY }, value: to)
rescue Dry::Struct::Error => e
  raise ArgumentError, e.message
end

#to(*path, map: EMPTY_ARRAY, &block) ⇒ Rule::Map

Maps to path from #map with block inbetween

Parameters:

  • path (Array<Symbol>, Symbol)
  • map (Array<Segment>, Segment) (defaults to: EMPTY_ARRAY)

Returns:



71
72
73
# File 'lib/remap/compiler.rb', line 71

def to(*path, map: EMPTY_ARRAY, &block)
  map(*map, to: path, &block)
end

#value(value) ⇒ Rule::Static::Fixed

Static value to be selected

Parameters:

  • value (Any)

Returns:

  • (Rule::Static::Fixed)


118
119
120
# File 'lib/remap/compiler.rb', line 118

def value(value)
  Static::Fixed.new(value: value)
end

#wrap(type, &block) ⇒ Rule::Wrap

Wraps output in type

Parameters:

  • type (:array)

Yield Returns:

Returns:

Raises:

  • (ArgumentError)

    if type is not :array



96
97
98
99
100
101
102
103
104
# File 'lib/remap/compiler.rb', line 96

def wrap(type, &block)
  unless block
    raise ArgumentError, "no block given"
  end

  add Rule::Wrap.new(type: type, rule: call(&block))
rescue Dry::Struct::Error => e
  raise ArgumentError, e.message
end