Class: Remap::Compiler
- Defined in:
- lib/remap/compiler.rb
Overview
Constructs a Rule from the block passed to Base.define
Class Method Summary collapse
-
.call(&block) ⇒ Rule
Constructs a rule tree given block.
Instance Method Summary collapse
-
#all(&block) ⇒ Rule::Path::Segment::Quantifier::All
Selects all elements.
-
#at(index, &block) ⇒ Path::Segment::Key
Selects index element in input.
- #call ⇒ Rule
-
#each(&block) ⇒ Rule::Each
Iterates over the input value, passes each value to its block and merges the result back together.
-
#embed(mapper, &block) ⇒ Rule::Embed
Maps using mapper.
-
#first(&block) ⇒ Path::Segment::Key
(also: #any)
Selects first element in input.
-
#get(*path, backtrace: Kernel.caller, &block) ⇒ Rule::Map::Required
Select a path and uses the same path as output.
-
#get?(*path, backtrace: Kernel.caller, &block) ⇒ Rule::Map::Optional
Optional version of #get.
-
#last(&block) ⇒ Path::Segment::Key
Selects last element in input.
-
#map(*path, to: EMPTY_ARRAY, backtrace: Kernel.caller, &block) ⇒ Rule::Map::Required
Maps input path [input] to output path [to].
-
#map?(*path, to: EMPTY_ARRAY, backtrace: Kernel.caller, &block) ⇒ Rule::Map::Optional
Optional version of #map.
-
#option(id, backtrace: Kernel.caller, &block) ⇒ Rule::Static::Option
Static option to be selected.
-
#rule ⇒ Rule
The final rule.
- #rules ⇒ Array<Rule>
- #set(*path, to:, &block) ⇒ Rule::Set
-
#to(*path, map: EMPTY_ARRAY, backtrace: Kernel.caller, &block) ⇒ Rule::Map
Maps to path from map with block in between.
-
#to?(*path, map: EMPTY_ARRAY, &block) ⇒ Rule::Map::Optional
Optional version of #to.
-
#value(value, &block) ⇒ Rule::Static::Fixed
Static value to be selected.
-
#wrap(type, &block) ⇒ Rule::Wrap
Wraps output in type.
Methods inherited from Proxy
Class Method Details
Instance Method Details
#all(&block) ⇒ Rule::Path::Segment::Quantifier::All
Selects all elements
174 175 176 177 178 179 180 |
# File 'lib/remap/compiler.rb', line 174 def all(&block) if block raise ArgumentError, "all selector does not take a block" end Selector::All.new(EMPTY_HASH) end |
#at(index, &block) ⇒ Path::Segment::Key
Selects index element in input
214 215 216 217 218 219 220 221 222 223 |
# File 'lib/remap/compiler.rb', line 214 def at(index, &block) if block raise ArgumentError, "first selector does not take a block" end 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
145 146 147 148 149 150 151 |
# File 'lib/remap/compiler.rb', line 145 def each(&block) unless block raise ArgumentError, "#each requires a block" end add Rule::Each.new(rule: call(&block)) end |
#embed(mapper, &block) ⇒ Rule::Embed
Maps using mapper
94 95 96 97 98 99 100 101 102 |
# File 'lib/remap/compiler.rb', line 94 def (mapper, &block) if block raise ArgumentError, "#embed does not take a block" end add Rule::Embed.new(mapper: mapper) rescue Dry::Struct::Error raise ArgumentError, "Embeded mapper must be [Remap::Mapper], got [#{mapper}]" end |
#first(&block) ⇒ Path::Segment::Key Also known as: any
Selects first element in input
228 229 230 231 232 233 234 |
# File 'lib/remap/compiler.rb', line 228 def first(&block) if block raise ArgumentError, "first selector does not take a block" end at(0) end |
#get(*path, backtrace: Kernel.caller, &block) ⇒ Rule::Map::Required
Select a path and uses the same path as output
76 77 78 |
# File 'lib/remap/compiler.rb', line 76 def get(*path, backtrace: Kernel.caller, &block) map(path, to: path, backtrace: backtrace, &block) end |
#get?(*path, backtrace: Kernel.caller, &block) ⇒ Rule::Map::Optional
Optional version of #get
85 86 87 |
# File 'lib/remap/compiler.rb', line 85 def get?(*path, backtrace: Kernel.caller, &block) map?(path, to: path, backtrace: backtrace, &block) end |
#last(&block) ⇒ Path::Segment::Key
Selects last element in input
240 241 242 243 244 245 246 |
# File 'lib/remap/compiler.rb', line 240 def last(&block) if block raise ArgumentError, "last selector does not take a block" end at(-1) end |
#map(*path, to: EMPTY_ARRAY, backtrace: Kernel.caller, &block) ⇒ Rule::Map::Required
Maps input path [input] to output path [to]
46 47 48 49 50 51 52 53 54 |
# File 'lib/remap/compiler.rb', line 46 def map(*path, to: EMPTY_ARRAY, backtrace: Kernel.caller, &block) add Rule::Map::Required.call( path: { output: [to].flatten, input: path.flatten }, backtrace: backtrace, rule: call(&block)) end |
#map?(*path, to: EMPTY_ARRAY, backtrace: Kernel.caller, &block) ⇒ Rule::Map::Optional
Optional version of #map
61 62 63 64 65 66 67 68 69 |
# File 'lib/remap/compiler.rb', line 61 def map?(*path, to: EMPTY_ARRAY, backtrace: Kernel.caller, &block) add Rule::Map::Optional.call( path: { output: [to].flatten, input: path.flatten }, backtrace: backtrace, rule: call(&block)) end |
#option(id, backtrace: Kernel.caller, &block) ⇒ Rule::Static::Option
Static option to be selected
200 201 202 203 204 205 206 |
# File 'lib/remap/compiler.rb', line 200 def option(id, backtrace: Kernel.caller, &block) if block raise ArgumentError, "option selector does not take a block" end Static::Option.new(name: id, backtrace: backtrace) end |
#rule ⇒ Rule
The final rule
251 252 253 |
# File 'lib/remap/compiler.rb', line 251 def rule Rule::Collection.call(rules: rules) end |
#set(*path, to:, &block) ⇒ Rule::Set
111 112 113 114 115 116 117 118 119 |
# File 'lib/remap/compiler.rb', line 111 def set(*path, to:, &block) if block raise ArgumentError, "#set does not take a block" end add Rule::Set.new(path: path.flatten, value: to) rescue Dry::Struct::Error => e raise ArgumentError, e. end |
#to(*path, map: EMPTY_ARRAY, backtrace: Kernel.caller, &block) ⇒ Rule::Map
Maps to path from map with block in between
127 128 129 |
# File 'lib/remap/compiler.rb', line 127 def to(*path, map: EMPTY_ARRAY, backtrace: Kernel.caller, &block) map(*map, to: path, backtrace: backtrace, &block) end |
#to?(*path, map: EMPTY_ARRAY, &block) ⇒ Rule::Map::Optional
Optional version of #to
136 137 138 |
# File 'lib/remap/compiler.rb', line 136 def to?(*path, map: EMPTY_ARRAY, &block) map?(*map, to: path, &block) end |
#value(value, &block) ⇒ Rule::Static::Fixed
Static value to be selected
187 188 189 190 191 192 193 |
# File 'lib/remap/compiler.rb', line 187 def value(value, &block) if block raise ArgumentError, "option selector does not take a block" end Static::Fixed.new(value: value) end |
#wrap(type, &block) ⇒ Rule::Wrap
Wraps output in type
161 162 163 164 165 166 167 168 169 |
# File 'lib/remap/compiler.rb', line 161 def wrap(type, &block) unless block raise ArgumentError, "#wrap requires a block" end add Rule::Wrap.new(type: type, rule: call(&block)) rescue Dry::Struct::Error => e raise ArgumentError, e. end |