Class: JsonMapping
- Inherits:
-
Object
- Object
- JsonMapping
- Defined in:
- lib/json_mapping.rb
Overview
Stores and applies a mapping to an input ruby Hash
Defined Under Namespace
Classes: FormatError, PathError, TransformError
Instance Method Summary collapse
-
#apply(input_hash) ⇒ Array
An array of output hashes representing the mapped objects.
-
#initialize(schema_path, transforms = {}) ⇒ JsonMapping
constructor
A new instance of JsonMapping.
Constructor Details
#initialize(schema_path, transforms = {}) ⇒ JsonMapping
Returns a new instance of JsonMapping.
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/json_mapping.rb', line 21 def initialize(schema_path, transforms = {}) schema = YAML.safe_load(File.read(schema_path)) @conditions = (schema['conditions'] || {}).map do |key, value| [key, Object.const_get("Conditions::#{value['class']}").new(value['predicate'])] end.to_h @object_schemas = schema['objects'] @transforms = transforms || {} @logger = Logger.new($stdout) end |
Instance Method Details
#apply(input_hash) ⇒ Array
Returns An array of output hashes representing the mapped objects.
36 37 38 39 40 |
# File 'lib/json_mapping.rb', line 36 def apply(input_hash) raise FormatError, 'Must define objects under the \'objects\' name' if @object_schemas.nil? @object_schemas.map { |schema| parse_object(input_hash, schema) }.reduce(&:merge) end |