Shale is a Ruby object mapper and serializer for JSON, YAML, TOML, CSV and XML. It allows you to parse JSON, YAML, TOML, CSV and XML data and convert it into Ruby data structures, as well as serialize data structures into JSON, YAML, TOML, CSV or XML.
A much better shorthand than the second type. "index.html": "text/html" is much easier to write than { "file": "index.html", "mime": "text/html" },.
It is a mapping instead of an array, which means:
There is no order in this mapping, distinct from an array which retains order.
There is guaranteed uniqueness in the mapping, distinct from an array which does not enforce key uniqueness.
Not to mention it is much faster in processing.
I have encountered this problem in Capsium and also the ISO 10303 schema.yaml parsing.
Right now it requires this sort of override:
class Config < Shale::Mapper
attribute :schemas, Schema, collection: true
yaml do
map 'schemas', using: { from: :schemas_from_yaml, to: :schemas_to_yaml }
end
def schemas_from_yaml(model, value)
model.schemas = value.map do |k, v|
Schema.new(id: k, path: v['path'])
end
end
def schemas_to_yaml(model, doc)
doc['schemas'] = model.schemas.sort_by do |schema|
schema.id
end.to_h do |schema|
[schema.id, { 'path' => schema.path }]
end
end
end
Which is rather annoying for every class of this type, and you need to repeat all these methods for JSON and YAML.
Instead, this can easily be done with:
class Config < Shale::Mapper
attribute :schemas, Schema, collection: true
yaml do
# maps serialization 'schemas[].id' to 'Schema.new.id'., 'schemas[].value' to 'Schema.new.value'
map 'schemas', to: schemas, map: { key: :id, value: :path }
# maps serialization 'schemas[].id' to 'Schema.new.id'., 'schemas[].*' to 'Schema.new.*'
map 'schemas', to: schemas, map: { key: :id, value: :* }
end
end
In JSON there are often structures like this:
Or
However, Shale only supports collections in JSON arrays:
The first type differs from the second type:
"index.html": "text/html"
is much easier to write than{ "file": "index.html", "mime": "text/html" },
.I have encountered this problem in Capsium and also the ISO 10303 schema.yaml parsing.
Right now it requires this sort of override:
Which is rather annoying for every class of this type, and you need to repeat all these methods for JSON and YAML.
Instead, this can easily be done with: