camel_yaml
Yaml (Camel YAML)
A yet another Crystal library for parsing and writing YAML documents.
Yaml does/is:
- parses comments to write them back
- integrates your own document schema to easily access typed contents
- a pure Crystal implementation not requiring libyaml or another library.
Also, Yaml is not fully compatible with the standard YAML for simplicity.
This project is experimental.
Installation
Add this to your application's shard.yml
:
dependencies:
camel_yaml:
github: mosop/camel_yaml
Code Examples
Basic
yaml = Yaml.parse(<<-EOS
smilies:
:): hello
:(: bye
EOS
)
yaml.raw
# => {":)" => "hello", ":(" => "bye"}
Multiple Documents
yaml = Yaml.parse(<<-EOS
smiley:
face: :)
greeting: hello
---
smiley:
face: :(
greeting: bye
EOS
)
yaml.documents[0].raw
# => {"smiley" => {"face" => ":)", "greeting" => "hello"}}
yaml.documents[1].raw
# => {"smiley" => {"face" => ":(", "greeting" => "bye"}}
Accessor
yaml = Yaml.parse(<<-EOS
ducks:
- name: huey
- name: dewey
- name: louie
EOS
)
yaml["ducks"].a
# => [{"name" => "huey"}, {"name" => "dewey"}, {"name" => "louie"}]
yaml["ducks"][0].h
# => {"name" => "huey"}
yaml["ducks"][0]["name"].s
# => "huey"
Numeral Access
yaml = Yaml.parse(<<-EOS
Yoshimi: Battles
the: Pink Robots
EOS
)
yaml[0].s # => "Battles"
yaml[1].s # => "Pink Robots"
Accessing Keys
yaml = Yaml.parse(<<-EOS
Yoshimi: Battles
the: Pink Robots
EOS
)
yaml[0].key # => "Yoshimi"
yaml[1].key # => "the"
Nilable Access
yaml = Yaml.parse(<<-EOS
foo:
EOS
)
yaml["bar"].s? # => nil
yaml = Yaml.parse(<<-EOS
cakes:
- name: strawberry
- name: cheese
- name: chocolat
EOS
)
yaml["cakes"][999]["name"].s? # => nil
Building
doc = Yaml.map do |map|
map["faces"].seq do |seq|
seq.comment = <<-EOS
add smiley icons below
one face a row
EOS
seq.s ":)"
seq.s ":("
seq.s ":P"
end
end
puts doc.pretty
doc["faces"][0].build do |s|
s.value = ";)"
s.trailing_comment = " wink!"
end
puts doc.pretty
Output:
---
faces:
# add smiley icons below
# one face a row
- :)
- :(
- :P
---
faces:
# add smiley icons below
# one face a row
- ;) # wink!
- :(
- :P
Custom Accessor
class Site < Yaml::Accessor
def name
s
end
def url
"http://mosop.#{s}"
end
end
class Sites < Yaml::Accessor
custom_seq Site
end
yaml = Yaml.parse(<<-EOS
- rocks
- yoga
- ninja
EOS
)
yaml[Sites][0].name # => "rocks"
yaml[Sites][0].url # => "http://mosop.rocks"
Layering
layer = Yaml.parse(<<-EOS
:): hello
EOS
).new_layer
layer.next_layer = Yaml.parse(<<-EOS
:(: bye
EOS
).new_layer
layer[":)"].s => # hello
layer[":("].s => # bye
Usage
require "camel_yaml"
and see Code Examples and Wiki.
Repository
camel_yaml
Owner
Statistic
- 1
- 0
- 0
- 0
- 1
- almost 8 years ago
- December 28, 2016
License
MIT License
Links
Synced at
Sun, 22 Dec 2024 23:14:22 GMT
Languages