Class: Remap::Compiler
- Inherits:
-
Object
- Object
- Remap::Compiler
- Extended by:
- Dry::Initializer, Forwardable
- Includes:
- Dry::Core::Constants
- Defined in:
- lib/remap/compiler.rb
Class Method Summary collapse
-
.call(&block) ⇒ Rule
Constructs a rule tree given block.
Instance Method Summary collapse
-
#all ⇒ Rule::Path::Segment::Quantifier::All
Selects all elements.
-
#at(index) ⇒ Path::Segment::Key
Selects index element in input.
-
#each(&block) ⇒ Rule::Each
Iterates over the input value, passes each value to its block and merges the result back together.
-
#embed(mapper) ⇒ Rule::Embed
Maps using mapper.
-
#first ⇒ Path::Segment::Key
(also: #any)
Selects first element in input.
-
#last ⇒ Path::Segment::Key
Selects last element in input.
-
#map(*path, to: EMPTY_ARRAY, &block) ⇒ Rule::Map
Maps path to #to with block inbetween.
-
#option(id) ⇒ Rule::Static::Option
Static option to be selected.
-
#rule ⇒ Rule
The final rule.
- #self ⇒ Rule
- #set(*path, to:) ⇒ Rule::Set
-
#to(*path, map: EMPTY_ARRAY, &block) ⇒ Rule::Map
Maps to path from #map with block inbetween.
-
#value(value) ⇒ Rule::Static::Fixed
Static value to be selected.
-
#wrap(type, &block) ⇒ Rule::Wrap
Wraps output in type.
Class Method Details
Instance Method Details
#all ⇒ Rule::Path::Segment::Quantifier::All
Selects all elements
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
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
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
46 47 48 49 50 |
# File 'lib/remap/compiler.rb', line 46 def (mapper) add Rule::Embed.new(mapper: mapper) rescue Dry::Struct::Error raise ArgumentError, "Embeded mapper must be [Remap::Mapper], got [#{mapper}]" end |
#first ⇒ Path::Segment::Key Also known as: any
Selects first element in input
146 147 148 |
# File 'lib/remap/compiler.rb', line 146 def first at(0) end |
#last ⇒ Path::Segment::Key
Selects last element in input
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
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
127 128 129 |
# File 'lib/remap/compiler.rb', line 127 def option(id) Static::Option.new(name: id) end |
#rule ⇒ Rule
The final rule
161 162 163 |
# File 'lib/remap/compiler.rb', line 161 def rule Rule::Collection.call(rules: rules) end |
#set(*path, to:) ⇒ Rule::Set
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. end |
#to(*path, map: EMPTY_ARRAY, &block) ⇒ Rule::Map
Maps to path from #map with block inbetween
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
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
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. end |