Class: Remap::Compiler
Overview
Constructs a Rule from the block passed to Base.define
Class Method Summary collapse
-
.call(backtrace: caller, &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.
- #call ⇒ Rule
-
#each(backtrace: caller, &block) ⇒ Rule::Each
Iterates over the input value, passes each value to its block and merges the result back together.
-
#embed(mapper, backtrace: caller) ⇒ Rule::Embed
Maps using mapper.
-
#first ⇒ Path::Segment::Key
(also: #any)
Selects first element in input.
-
#get(*path, backtrace: caller, &block) ⇒ Rule::Map::Required
Select a path and uses the same path as output.
-
#get?(*path, backtrace: caller, &block) ⇒ Rule::Map::Optional
Optional version of #get.
-
#last ⇒ Path::Segment::Key
Selects last element in input.
-
#map(*path, to: EMPTY_ARRAY, backtrace: caller, &block) ⇒ Rule::Map::Required
Maps input path [input] to output path [to].
-
#map?(*path, to: EMPTY_ARRAY, backtrace: caller, &block) ⇒ Rule::Map::Optional
Optional version of #map.
-
#option(id, backtrace: caller) ⇒ Rule::Static::Option
Static option to be selected.
- #rules ⇒ Array<Rule>
-
#set(*path, to:, backtrace: caller) ⇒ Rule::Set
Set a static value.
-
#to(*path, map: EMPTY_ARRAY, backtrace: caller, &block) ⇒ Rule::Map
Maps to path from map with block in between.
-
#to?(*path, map: EMPTY_ARRAY, backtrace: caller, &block) ⇒ Rule::Map::Optional
Optional version of #to.
-
#value(value, backtrace: caller) ⇒ Rule::Static::Fixed
Static value to be selected.
-
#wrap(type, backtrace: caller, &block) ⇒ Rule::Wrap
Wraps output in type.
Methods included from Catchable
Methods inherited from Proxy
Class Method Details
.call(backtrace: caller, &block) ⇒ Rule
Constructs a rule tree given block
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/remap/compiler.rb', line 34 def self.call(backtrace: caller, &block) unless block return Rule::VOID end rules = new([]).tap do |compiler| compiler.instance_exec(&block) end.rules Rule::Block.new(backtrace: backtrace, rules: rules) end |
Instance Method Details
#all ⇒ Rule::Path::Segment::Quantifier::All
Selects all elements
383 384 385 386 387 388 389 |
# File 'lib/remap/compiler.rb', line 383 def all if block_given? raise ArgumentError, "all selector does not take a block" end Selector::All.new(EMPTY_HASH) end |
#at(index) ⇒ Path::Segment::Key
Selects index element in input
464 465 466 467 468 469 470 471 472 473 |
# File 'lib/remap/compiler.rb', line 464 def at(index) if block_given? 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(backtrace: caller, &block) ⇒ Rule::Each
Iterates over the input value, passes each value to its block and merges the result back together
319 320 321 322 323 324 325 |
# File 'lib/remap/compiler.rb', line 319 def each(backtrace: caller, &block) unless block raise ArgumentError, "#each requires a block" end add rule(all, backtrace: backtrace, &block) end |
#embed(mapper, backtrace: caller) ⇒ Rule::Embed
Maps using mapper
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/remap/compiler.rb', line 181 def embed(mapper, backtrace: caller) if block_given? raise ArgumentError, "#embed does not take a block" end Types::Mapper[mapper] do raise ArgumentError, "Argument to #embed must be a mapper, got #{mapper.class}" end result = rule(backtrace: backtrace).add do |s0| build_embed(s0, mapper, backtrace) end add result end |
#first ⇒ Path::Segment::Key Also known as: any
Selects first element in input
493 494 495 496 497 498 499 |
# File 'lib/remap/compiler.rb', line 493 def first if block_given? raise ArgumentError, "first selector does not take a block" end at(0) end |
#get(*path, backtrace: caller, &block) ⇒ Rule::Map::Required
Select a path and uses the same path as output
118 119 120 |
# File 'lib/remap/compiler.rb', line 118 def get(*path, backtrace: caller, &block) add rule(path, to: path, backtrace: backtrace, &block) end |
#get?(*path, backtrace: caller, &block) ⇒ Rule::Map::Optional
Optional version of #get
143 144 145 |
# File 'lib/remap/compiler.rb', line 143 def get?(*path, backtrace: caller, &block) add rule?(path, to: path, backtrace: backtrace, &block) end |
#last ⇒ Path::Segment::Key
Selects last element in input
520 521 522 523 524 525 526 |
# File 'lib/remap/compiler.rb', line 520 def last if block_given? raise ArgumentError, "last selector does not take a block" end at(-1) end |
#map(*path, to: EMPTY_ARRAY, backtrace: caller, &block) ⇒ Rule::Map::Required
Maps input path [input] to output path [to]
67 68 69 |
# File 'lib/remap/compiler.rb', line 67 def map(*path, to: EMPTY_ARRAY, backtrace: caller, &block) add rule(*path, to: to, backtrace: backtrace, &block) end |
#map?(*path, to: EMPTY_ARRAY, backtrace: caller, &block) ⇒ Rule::Map::Optional
Optional version of #map
94 95 96 |
# File 'lib/remap/compiler.rb', line 94 def map?(*path, to: EMPTY_ARRAY, backtrace: caller, &block) add rule?(*path, to: to, backtrace: backtrace, &block) end |
#option(id, backtrace: caller) ⇒ Rule::Static::Option
Static option to be selected
435 436 437 438 439 440 441 |
# File 'lib/remap/compiler.rb', line 435 def option(id, backtrace: caller) if block_given? raise ArgumentError, "option selector does not take a block" end Static::Option.new(name: id, backtrace: backtrace) end |
#rules ⇒ Array<Rule>
11 |
# File 'lib/remap/compiler.rb', line 11 param :rules, type: Types.Array(Rule) |
#set(*path, to:, backtrace: caller) ⇒ Rule::Set
Set a static value
232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/remap/compiler.rb', line 232 def set(*path, to:, backtrace: caller) if block_given? raise ArgumentError, "#set does not take a block" end unless to.is_a?(Static) raise ArgumentError, "Argument to #set must be a static value, got #{to.class}" end add rule(to: path, backtrace: backtrace).add { to.call(_1) } end |
#to(*path, map: EMPTY_ARRAY, backtrace: caller, &block) ⇒ Rule::Map
Maps to path from map with block in between
265 266 267 |
# File 'lib/remap/compiler.rb', line 265 def to(*path, map: EMPTY_ARRAY, backtrace: caller, &block) add rule(*map, to: path, backtrace: backtrace, &block) end |
#to?(*path, map: EMPTY_ARRAY, backtrace: caller, &block) ⇒ Rule::Map::Optional
Optional version of #to
291 292 293 |
# File 'lib/remap/compiler.rb', line 291 def to?(*path, map: EMPTY_ARRAY, backtrace: caller, &block) add rule?(*map, to: path, backtrace: backtrace, &block) end |
#value(value, backtrace: caller) ⇒ Rule::Static::Fixed
Static value to be selected
409 410 411 412 413 414 415 |
# File 'lib/remap/compiler.rb', line 409 def value(value, backtrace: caller) if block_given? raise ArgumentError, "option selector does not take a block" end Static::Fixed.new(value: value, backtrace: backtrace) end |
#wrap(type, backtrace: caller, &block) ⇒ Rule::Wrap
Wraps output in type
352 353 354 355 356 357 358 359 360 361 362 |
# File 'lib/remap/compiler.rb', line 352 def wrap(type, backtrace: caller, &block) unless block raise ArgumentError, "#wrap requires a block" end unless type == :array raise ArgumentError, "Argument to #wrap must equal :array, got [#{type}] (#{type.class})" end add rule(backtrace: backtrace, &block).then { Array.wrap(_1) } end |