class PortableObjectTemplate
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.
Constants
- BOOLEAN
- CLASS_FOR
Public Class Methods
column_spec_from(col_rot_spec)
click to toggle source
# File lib/object-template.rb 180 def self.column_spec_from col_rot_spec 181 case col_rot_spec 182 when nil 183 nil 184 185 when Range 186 range = col_rot_spec 187 raise if range.exclude_end? ## 188 raise unless range.first.kind_of? Numeric ## 189 {type: "number", range: [range.first, range.last]} 190 191 when Module 192 mdl = col_rot_spec 193 class_name,_ = CLASS_FOR.find {|k,v| v == mdl} 194 raise unless class_name 195 {type: class_name} 196 197 when Regexp 198 rx = col_rot_spec 199 {regex: rx.source} 200 201 when Set 202 set = col_rot_spec 203 if set == BOOLEAN 204 ## awkward API: must reference PortableObjectTemplate::BOOLEAN 205 {type: "boolean"} 206 else 207 {set: set.to_a} 208 end 209 210 else # assume it is a value 211 {value: col_rot_spec} 212 end 213 end
spec_from(rot_spec)
click to toggle source
Convert a ROT spec into a POT spec (without creating a ROT). Not completely general: some POTs cannot be represented in this way.
# File lib/object-template.rb 163 def self.spec_from rot_spec 164 case rot_spec 165 when Array 166 rot_spec.map do |col_rot_spec| 167 column_spec_from(col_rot_spec) 168 end 169 when Hash 170 h = {} 171 rot_spec.each do |k, col_rot_spec| 172 h[k] = column_spec_from(col_rot_spec) 173 end 174 h 175 else 176 raise ArgumentError 177 end 178 end