An open API service indexing awesome lists of open source software.

https://github.com/dreamcat4/yamldoc

A simple solution for ERB enabled YAML file.
https://github.com/dreamcat4/yamldoc

Last synced: 3 months ago
JSON representation

A simple solution for ERB enabled YAML file.

Awesome Lists containing this project

README

        

= YamlDoc

YamlDoc is a simple Yaml reader/writer solution that uses an ERB enabled YAML file. Yaml is represented as a Hash tree. Features include

Autosave, object detection (modified in-place). Force uniform hash keys as String or Sym, dot notation for nested objects, and manual write for large files.

== Install

Install from gemcutter:

gem install yamldoc

== Quickstart

There are various ways to create

class Settings < YamlDoc
self.filename = "file.yml"
self.autosave = true
...
end
file = Settings.new

Or

settings = YamlDoc.new("file.yml")

Or

settings = YamlDoc.new :filename => "file.yml", :autosave => true

== YmlDoc Methods

In most cases the `:save` method is all you will need.

config.methods.inspect
=> [:load, :save, :methods, :properties, :reload_hooks]

`:load` will reload the existing file, or (with a parameter) merge the existing object into the newly loaded file (and re-save it). So generally use `YamlDoc.new` to load a different file.

== YmlDoc Properties

YamlDoc.defaults.inspect
=> {:ymldoc_pfx=>"", :deep_clone=>false, :force_keys=>false, :key_type=>String, :autosave=>false, :object_detection=>false, :filename=>nil}

== AutoYamlDoc Class

Of course it's tempting to just turn everything on for easy access to yml settings file. So we've gone ahead and created an "everythin on" class.

AutoYamlDoc.defaults.inspect
=> {:ymldoc_pfx=>"", :deep_clone=>false, :force_keys=>true, :key_type=>String, :autosave=>true, :object_detection=>true, :filename=>nil}

AutoYmlDoc.new("settings.yml")

== Object Manipulation

The deserialized Yaml object just a special kind of nested hash.

settings.a = "Somestring"
settings.b = [:label1,:label2,:label3]
settings.b[1].inspect
=> :label2

settings.defaults.d = { "key1" => "val1", ... }
settings.defaults.key1
=> "val1"

other_hash = { "key1" => 5 }
settings.merge! other_hash
settings.key1
=> 5

== Advanced yamldoc methods and properties

config.methods.inspect
=> [:load, :save, :methods, :properties, :reload_hooks]

You can rename the standard YmlDoc methods by setting the :ymldoc_pfx property. This is useful should any namespace clashes occur at the root (topmost) level of your yaml file. For example if we encountered a yaml file which has topmost nodes `save` and `:filename`. These are part of the YamlDoc API methods. So we use a `:ymldoc_pfx` to rename the YmlDoc object methods to something else. For example:

config.properties.inspect
=> [:object_detection, :root_obj, :ymldoc_pfx, :force_keys, :deep_clone, :detection_hooks, :key_type, :autosave, :filename]

config.ymldoc_pfx = "yml_"
config.reload_hooks
config.yml_methods.inspect
=> [:yml_load, :yml_save, :yml_methods, :yml_properties, :yml_reload_hooks]

config.save = "somestring"
config.save
=> "somestring"

config.yml_save()
# file saved.

== Copyright, License

Copyright (c) 2009 dreamcat4
Copyright (c) 2008 {Ben Johnson}[http://github.com/binarylogic] of {Binary Logic}[http://www.binarylogic.com], released under the MIT license