Git Product home page Git Product logo

camel_yaml's Introduction

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.

Build Status

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.

camel_yaml's People

Contributors

mosop avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.