ruyml 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +1 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +20 -0
- data/LICENSE.txt +21 -0
- data/README.md +85 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/ruyml +48 -0
- data/bin/setup +8 -0
- data/lib/ruyml/version.rb +3 -0
- data/lib/ruyml.rb +53 -0
- data/ruyml.gemspec +24 -0
- metadata +88 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 46b25c10159d9211c79de3ed33a83129c9ab43c6
|
|
4
|
+
data.tar.gz: 38ee47d13a655055f155185385b2e0ea0de751bb
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 9a3808d44846fd8d9872ae574c44198ee4201a5526148c76536278e564882c91364727c1da6a352fdfe9169d6ec22469466a3ab81e531c45320c724a46d028fb
|
|
7
|
+
data.tar.gz: 1422071ca3be7056e4ec26f8ad964df3a64ae02c2c299ed035f314a7ffcac25da06b9a14493ed5ccea399703a5b967e66f88da20c48eef4a2b9677287f5bc73b
|
data/.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
*.swap
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2016 Xavier Lucas
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# RUYML
|
|
2
|
+
|
|
3
|
+
Ruby templating using YAML datasources
|
|
4
|
+
|
|
5
|
+
# Usage
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
Usage : ruyml [options]
|
|
9
|
+
|
|
10
|
+
Mandatory options:
|
|
11
|
+
-d, --datasources DATASOURCES
|
|
12
|
+
-t, --template TEMPLATE
|
|
13
|
+
|
|
14
|
+
Optional options:
|
|
15
|
+
-o, --output FILE
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Simple usage
|
|
19
|
+
|
|
20
|
+
Given a yaml file :
|
|
21
|
+
|
|
22
|
+
```yaml
|
|
23
|
+
alert:
|
|
24
|
+
message: 'hello world !'
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
And an erb file :
|
|
28
|
+
|
|
29
|
+
```erb
|
|
30
|
+
message = "<%= alert.message.capitalize %>"
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Running :
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
ruby ruyml.rb -d alert.yaml -t template.erb
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Gives the following output :
|
|
40
|
+
|
|
41
|
+
```
|
|
42
|
+
message = "Hello world !"
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### Advanced usage
|
|
46
|
+
#### Multiple data sources
|
|
47
|
+
You can refer to multiple YAML datasources in a template and render it using :
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
ruyml -d data1.yaml,data2.yaml -t template.erb
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
#### Accessing Hash methods
|
|
54
|
+
When iterating over an item which is not a leaf of the YAML file, you can access
|
|
55
|
+
methods of the Hash class directly. In case a propertie has the same name than
|
|
56
|
+
the method you are trying to use, simply use the `<method>!` form.
|
|
57
|
+
|
|
58
|
+
For instance :
|
|
59
|
+
|
|
60
|
+
```yaml
|
|
61
|
+
object:
|
|
62
|
+
each: value
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
```erb
|
|
66
|
+
Each object will have <%= object.each %>.
|
|
67
|
+
|
|
68
|
+
Full object properties:
|
|
69
|
+
<% object.each! do |k, v| -%>
|
|
70
|
+
<%= k %> : <%= v %>
|
|
71
|
+
<% end -%>
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Once rendered, the result is :
|
|
75
|
+
|
|
76
|
+
```
|
|
77
|
+
Each object will have value.
|
|
78
|
+
|
|
79
|
+
Full object properties:
|
|
80
|
+
each : value
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## Status
|
|
84
|
+
|
|
85
|
+
Alot of todos in there ... but you can feel the spirit.
|
data/Rakefile
ADDED
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "ruyml"
|
|
5
|
+
|
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
8
|
+
|
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
10
|
+
# require "pry"
|
|
11
|
+
# Pry.start
|
|
12
|
+
|
|
13
|
+
require "irb"
|
|
14
|
+
IRB.start
|
data/bin/ruyml
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
# *****************************************************************************
|
|
4
|
+
# RUYML : Ruby templating using YAML datasources
|
|
5
|
+
# *****************************************************************************
|
|
6
|
+
# @author : Xavier Lucas
|
|
7
|
+
# *****************************************************************************
|
|
8
|
+
|
|
9
|
+
require 'ruyml'
|
|
10
|
+
require 'yaml'
|
|
11
|
+
require 'optparse'
|
|
12
|
+
require 'ostruct'
|
|
13
|
+
|
|
14
|
+
# Parse CLI
|
|
15
|
+
data = {}
|
|
16
|
+
options = OpenStruct.new
|
|
17
|
+
parser = OptionParser.new do |opts|
|
|
18
|
+
opts.banner = "Usage : #{File.basename($0)} [options]"
|
|
19
|
+
opts.separator ""
|
|
20
|
+
opts.separator "Mandatory options:"
|
|
21
|
+
|
|
22
|
+
opts.on("-d", "--datasources DATASOURCES", Array) do |datasources|
|
|
23
|
+
options.datasources = datasources
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
opts.on("-t", "--template TEMPLATE") do |template|
|
|
27
|
+
options.template = template
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
opts.separator ""
|
|
31
|
+
opts.separator "Optional options:"
|
|
32
|
+
|
|
33
|
+
opts.on("-o", "--output FILE") do |output|
|
|
34
|
+
options.output = output
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
parser.parse!(ARGV)
|
|
39
|
+
|
|
40
|
+
if options.template.nil? || options.datasources.nil?
|
|
41
|
+
abort parser.help
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
options.datasources.each do |datasource|
|
|
45
|
+
data.merge!(YAML.load(File.read(datasource)))
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
Ruyml::Data.new(data).render(options.template, options.output)
|
data/bin/setup
ADDED
data/lib/ruyml.rb
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
require "ruyml/version"
|
|
2
|
+
|
|
3
|
+
require 'erb'
|
|
4
|
+
|
|
5
|
+
module Ruyml
|
|
6
|
+
|
|
7
|
+
class Data
|
|
8
|
+
# Create RUYML data from a YAML hash.
|
|
9
|
+
# Underlying hashes will be accessed
|
|
10
|
+
# as instances of this class. Other
|
|
11
|
+
# types are kept untouched and returned
|
|
12
|
+
# as is.
|
|
13
|
+
def initialize(hash)
|
|
14
|
+
@hash = hash
|
|
15
|
+
@hash.each do |k, v|
|
|
16
|
+
self.define_singleton_method k.to_sym do
|
|
17
|
+
return v.class == Hash ? Data.new(v) : v
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# Renders RUYML data using the given template.
|
|
23
|
+
# Rendered data is either written to an optional
|
|
24
|
+
# output file path or to stdout.
|
|
25
|
+
def render(template, output)
|
|
26
|
+
result = ERB.new(File.read(template), 0, '-').result(binding)
|
|
27
|
+
if !output.nil?
|
|
28
|
+
File.open(output, "w") do |file|
|
|
29
|
+
file.write(result)
|
|
30
|
+
end
|
|
31
|
+
else
|
|
32
|
+
puts result
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Access Hash class methods. If a YAML propertie
|
|
37
|
+
# matches the method name, then the target method
|
|
38
|
+
# is called if a '!' terminates the method name.
|
|
39
|
+
private
|
|
40
|
+
def method_missing(name, *args, &block)
|
|
41
|
+
method = name.to_s
|
|
42
|
+
if method =~ /.+!$/ && self.respond_to?(method.chop)
|
|
43
|
+
method.chop!
|
|
44
|
+
end
|
|
45
|
+
if @hash.respond_to?(method)
|
|
46
|
+
@hash.send(method, *args, &block)
|
|
47
|
+
else
|
|
48
|
+
raise NameError, "Undefined property or method '#{name}'"
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
end
|
data/ruyml.gemspec
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require 'ruyml/version'
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "ruyml"
|
|
8
|
+
spec.version = Ruyml::VERSION
|
|
9
|
+
spec.authors = ["Xavier Lucas"]
|
|
10
|
+
spec.email = ["xavier_lucas@ymail.com"]
|
|
11
|
+
|
|
12
|
+
spec.summary = %q{Ruby templating from YAML datasources.}
|
|
13
|
+
spec.description = %q{A command line interface and a library to render erb templates using one or multiple YAML datasources.}
|
|
14
|
+
spec.homepage = "https://github.com/xlucas/ruyml"
|
|
15
|
+
spec.license = "MIT"
|
|
16
|
+
|
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
|
18
|
+
spec.bindir = "bin"
|
|
19
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
|
20
|
+
spec.require_paths = ["lib"]
|
|
21
|
+
|
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
|
24
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: ruyml
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Xavier Lucas
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2016-05-17 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: bundler
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - ~>
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '1.11'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - ~>
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '1.11'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rake
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - ~>
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '10.0'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - ~>
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '10.0'
|
|
41
|
+
description: A command line interface and a library to render erb templates using
|
|
42
|
+
one or multiple YAML datasources.
|
|
43
|
+
email:
|
|
44
|
+
- xavier_lucas@ymail.com
|
|
45
|
+
executables:
|
|
46
|
+
- console
|
|
47
|
+
- ruyml
|
|
48
|
+
- setup
|
|
49
|
+
extensions: []
|
|
50
|
+
extra_rdoc_files: []
|
|
51
|
+
files:
|
|
52
|
+
- .gitignore
|
|
53
|
+
- Gemfile
|
|
54
|
+
- Gemfile.lock
|
|
55
|
+
- LICENSE.txt
|
|
56
|
+
- README.md
|
|
57
|
+
- Rakefile
|
|
58
|
+
- bin/console
|
|
59
|
+
- bin/ruyml
|
|
60
|
+
- bin/setup
|
|
61
|
+
- lib/ruyml.rb
|
|
62
|
+
- lib/ruyml/version.rb
|
|
63
|
+
- ruyml.gemspec
|
|
64
|
+
homepage: https://github.com/xlucas/ruyml
|
|
65
|
+
licenses:
|
|
66
|
+
- MIT
|
|
67
|
+
metadata: {}
|
|
68
|
+
post_install_message:
|
|
69
|
+
rdoc_options: []
|
|
70
|
+
require_paths:
|
|
71
|
+
- lib
|
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
73
|
+
requirements:
|
|
74
|
+
- - '>='
|
|
75
|
+
- !ruby/object:Gem::Version
|
|
76
|
+
version: '0'
|
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
78
|
+
requirements:
|
|
79
|
+
- - '>='
|
|
80
|
+
- !ruby/object:Gem::Version
|
|
81
|
+
version: '0'
|
|
82
|
+
requirements: []
|
|
83
|
+
rubyforge_project:
|
|
84
|
+
rubygems_version: 2.4.8
|
|
85
|
+
signing_key:
|
|
86
|
+
specification_version: 4
|
|
87
|
+
summary: Ruby templating from YAML datasources.
|
|
88
|
+
test_files: []
|