Module: Schema
- Defined in:
- lib/schema/schema.rb,
lib/schema/include.rb,
lib/schema/version.rb
Defined Under Namespace
Modules: Include
Constant Summary collapse
- VERSION =
'0.0.5'
Class Method Summary collapse
Class Method Details
.include! ⇒ Object
37 38 39 |
# File 'lib/schema/schema.rb', line 37 def self.include! Object.send :include, Schema::Include end |
.transform(object, schema) ⇒ Object
transform
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/schema/schema.rb', line 7 def self.transform object, schema case schema when Array raise ArgumentError, "An empty Array is not a valid schema." unless schema.first # for now we only transform into the first schema-array element case object when nil [] when Array object.map { |e| e.transform(schema.first) } else [object.transform(schema.first)] end when Hash raise ArgumentError, "A #{schema.class} is no valid schema for a #{object.class}." unless object.is_a?(Hash) schema.inject({}) do |h, (key, subschema)| if key.to_s =~ /^(.*)\?$/ optionalkey = $1.to_sym h.update(object[optionalkey] ? { optionalkey => object[optionalkey].transform(subschema) } : {}) else h.update(key => object[key].transform(subschema)) end end when nil nil else schema.from(object) end end |