class Patm::Pattern

Public Class Methods

build_from(plain) click to toggle source
# File lib/patm.rb, line 11
def self.build_from(plain)
  case plain
  when Pattern
    plain
  when ::Array
    build_from_array(plain)
  when ::Hash
    build_from_hash(plain)
  else
    Obj.new(plain)
  end
end
build_from_array(plain) click to toggle source
# File lib/patm.rb, line 24
def self.build_from_array(plain)
  array = plain.map{|a| build_from(a)}
  rest_index = array.index(&:rest?)
  if rest_index
    head = array[0...rest_index]
    rest = array[rest_index]
    tail = array[(rest_index+1)..-1]
    Arr.new(head, rest, tail)
  else
    Arr.new(array)
  end
end
build_from_hash(plain) click to toggle source
# File lib/patm.rb, line 37
def self.build_from_hash(plain)
  self::Hash.new(
    plain.each_with_object({}) do|(k, v), h|
      h[k] = build_from(v) if k != Patm.exact
    end,
    plain[Patm.exact]
  )
end

Public Instance Methods

&(rhs) click to toggle source
# File lib/patm.rb, line 79
def &(rhs)
  And.new([self, Pattern.build_from(rhs)])
end
[](name) click to toggle source
# File lib/patm.rb, line 83
def [](name)
  self & Named.new(name)
end
compile() click to toggle source
# File lib/patm.rb, line 87
def compile
  src, context, _ = self.compile_internal(0)

  Compiled.new(self.inspect, src || "true", context)
end
compile_internal(free_index, target_name = "_obj") click to toggle source

free_index:Numeric -> target_name:String -> [src:String|nil, context:Array, free_index:Numeric] variables: _ctx, _match, (target_name)

# File lib/patm.rb, line 95
def compile_internal(free_index, target_name = "_obj")
  [
    "_ctx[#{free_index}].execute(_match, #{target_name})",
    [self],
    free_index + 1
  ]
end
execute(match, obj) click to toggle source
# File lib/patm.rb, line 69
def execute(match, obj); true; end
opt() click to toggle source

Use in Hash pattern.

# File lib/patm.rb, line 65
def opt
  Opt.new(self)
end
opt?() click to toggle source
# File lib/patm.rb, line 71
def opt?
  false
end
rest?() click to toggle source
# File lib/patm.rb, line 75
def rest?
  false
end