Class: PortableObjectTemplate

Inherits:
ObjectTemplate show all
Defined in:
lib/object-template.rb

Overview

Template specified by array or hash in a portable format composed of strings, numbers, booleans, arrays, and hashes. Special entry values correspond to wildcards and matchers of several kinds. See the unit tests for examples.

The objects matched include anything constructed out of numbers, booleans, including null, and strings using hashes and arrays. In other words, objects that can be serialized with json or msgpack.

Constant Summary collapse

CLASS_FOR =
{
  "number" => Numeric,
  "string" => String,
  "list"   => Array,
  "map"    => Hash
}

Constants inherited from ObjectTemplate

ObjectTemplate::VERSION

Instance Method Summary collapse

Methods inherited from ObjectTemplate

#===, #initialize, #inspect, #optimize!

Constructor Details

This class inherits a constructor from ObjectTemplate

Instance Method Details

#fill_matchers(k, v) ⇒ Object

:nodoc:



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/object-template.rb', line 105

def fill_matchers k, v  # :nodoc:
  case v
  when nil
    @matchers << [k, nil]
      # This must be there to ensure the key exists in the hash case.
  when Hash
    v.each do |kk, vv|
      case kk
      when :value, "value"
        @matchers << [k, vv]
      when :set, "set"
        @matchers << [k, MemberMatchingSet.new(vv)]
      when :type, "type"
        @matchers << [k, CLASS_FOR[vv.to_s]]
      when :range, "range"
        @matchers << [k, Range.new(*vv)]
      when :regex, "regex"
        @matchers << [k, Regexp.new(vv)]
      else
        raise ArgumentError,
          "unrecognized match specifier: #{kk.inspect}"
      end
    end
  else
    raise ArgumentError,
      "expected nil or Hash in template, found #{v.inspect}"
  end
end