Class: PortableObjectTemplate
- Inherits:
-
ObjectTemplate
- Object
- ObjectTemplate
- PortableObjectTemplate
- 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
- BOOLEAN =
MemberMatchingSet.new([true, false])
- CLASS_FOR =
{ "boolean" => BOOLEAN, "number" => Numeric, "integer" => Integer, "string" => String, "list" => Array, "map" => Hash }
Constants inherited from ObjectTemplate
Instance Attribute Summary
Attributes inherited from ObjectTemplate
Class Method Summary collapse
- .column_spec_from(col_rot_spec) ⇒ Object
-
.spec_from(rot_spec) ⇒ Object
Convert a ROT spec into a POT spec (without creating a ROT).
Instance Method Summary collapse
-
#fill_matchers(k, v) ⇒ Object
:nodoc:.
Methods inherited from ObjectTemplate
#===, #initialize, #inspect, #optimize!
Constructor Details
This class inherits a constructor from ObjectTemplate
Class Method Details
.column_spec_from(col_rot_spec) ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/object-template.rb', line 180 def self.column_spec_from col_rot_spec case col_rot_spec when nil nil when Range range = col_rot_spec raise if range.exclude_end? ## raise unless range.first.kind_of? Numeric ## {type: "number", range: [range.first, range.last]} when Module mdl = col_rot_spec class_name,_ = CLASS_FOR.find {|k,v| v == mdl} raise unless class_name {type: class_name} when Regexp rx = col_rot_spec {regex: rx.source} when Set set = col_rot_spec if set == BOOLEAN ## awkward API: must reference PortableObjectTemplate::BOOLEAN {type: "boolean"} else {set: set.to_a} end else # assume it is a value {value: col_rot_spec} end end |
.spec_from(rot_spec) ⇒ Object
Convert a ROT spec into a POT spec (without creating a ROT). Not completely general: some POTs cannot be represented in this way.
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/object-template.rb', line 163 def self.spec_from rot_spec case rot_spec when Array rot_spec.map do |col_rot_spec| column_spec_from(col_rot_spec) end when Hash h = {} rot_spec.each do |k, col_rot_spec| h[k] = column_spec_from(col_rot_spec) end h else raise ArgumentError end end |
Instance Method Details
#fill_matchers(k, v) ⇒ Object
:nodoc:
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/object-template.rb', line 114 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" if key_converter (vv = key_converter[vv]) rescue nil # error means templating object end @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 |