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
Set a static value.
-
#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
.call(&block) ⇒ Rule
Constructs a rule tree given block
30 31 32 33 34 35 36 37 38 |
# File 'lib/remap/compiler.rb', line 30 def self.call(&block) unless block return Rule::Void.new end new([]).tap do |compiler| compiler.instance_exec(&block) end.rule end |
Instance Method Details
#all(&block) ⇒ Rule::Path::Segment::Quantifier::All
Selects all elements
379 380 381 382 383 384 385 |
# File 'lib/remap/compiler.rb', line 379 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
460 461 462 463 464 465 466 467 468 469 |
# File 'lib/remap/compiler.rb', line 460 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
317 318 319 320 321 322 323 |
# File 'lib/remap/compiler.rb', line 317 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
187 188 189 190 191 192 193 194 195 |
# File 'lib/remap/compiler.rb', line 187 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
489 490 491 492 493 494 495 |
# File 'lib/remap/compiler.rb', line 489 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
124 125 126 |
# File 'lib/remap/compiler.rb', line 124 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
149 150 151 |
# File 'lib/remap/compiler.rb', line 149 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
516 517 518 519 520 521 522 |
# File 'lib/remap/compiler.rb', line 516 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]
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::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
94 95 96 97 98 99 100 101 102 |
# File 'lib/remap/compiler.rb', line 94 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
431 432 433 434 435 436 437 |
# File 'lib/remap/compiler.rb', line 431 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
527 528 529 |
# File 'lib/remap/compiler.rb', line 527 def rule Rule::Collection.call(rules: rules) end |
#set(*path, to:, &block) ⇒ Rule::Set
Set a static value
232 233 234 235 236 237 238 239 240 |
# File 'lib/remap/compiler.rb', line 232 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
263 264 265 |
# File 'lib/remap/compiler.rb', line 263 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
289 290 291 |
# File 'lib/remap/compiler.rb', line 289 def to?(*path, map: EMPTY_ARRAY, &block) map?(*map, to: path, &block) end |
#value(value, &block) ⇒ Rule::Static::Fixed
Static value to be selected
405 406 407 408 409 410 411 |
# File 'lib/remap/compiler.rb', line 405 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
350 351 352 353 354 355 356 357 358 |
# File 'lib/remap/compiler.rb', line 350 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 |