markbates-yamler 0.0.2

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.
data/README ADDED
@@ -0,0 +1,14 @@
1
+ Yamler - Making YAML easy and fun to use with Ruby
2
+ =========================================================================
3
+
4
+ Using YAML in Ruby is pretty easy, but I find myself doing several things
5
+ every time I use YAML. First is I always seem to pass it through ERB. The
6
+ other is that I want to pass it a binding, because I want to give it
7
+ access to some variables or methods. Finally I sometimes end up merging
8
+ several YAML files into one file, because other wise it would be too big
9
+ and unwieldy to manage. Enter Yamler:
10
+
11
+ === Examples:
12
+ # This will read the file in, parse it through ERB, and then run it
13
+ # through YAML#load
14
+ Yamler.load('/path/to/file.yml')
@@ -0,0 +1,7 @@
1
+ require 'erb'
2
+ require 'yaml'
3
+ require 'fileutils'
4
+
5
+ Dir.glob(File.join(File.dirname(__FILE__), 'yamler', '**/*.rb')).each do |f|
6
+ require f
7
+ end
@@ -0,0 +1,34 @@
1
+ module Yamler
2
+
3
+ class Template
4
+
5
+ attr_accessor :path # :nodoc:
6
+
7
+ # Takes the path to the YAML file you wish to render.
8
+ def initialize(path)
9
+ self.path = File.expand_path(path)
10
+ end
11
+
12
+ # Runs the YAML file through ERB using either the current
13
+ # templates <tt>binding</tt> or the specified one. This
14
+ # method returns a string and <i>NOT</i> a YAML object.
15
+ def render(b = binding)
16
+ res = ERB.new(File.read(self.path)).result(b)
17
+ res
18
+ end
19
+
20
+ def require_yaml(path)
21
+ path = File.extname(path) == '' ? "#{path}.yml" : path
22
+ unless File.exists?(path)
23
+ path = File.expand_path(File.join(File.dirname(self.path), path))
24
+ end
25
+ Yamler::Template.new(path).render(binding)
26
+ end
27
+
28
+ def __FILE__
29
+ self.path
30
+ end
31
+
32
+ end
33
+
34
+ end
@@ -0,0 +1,32 @@
1
+ module Yamler
2
+
3
+ class << self
4
+
5
+ # Mimics <tt>YAML#load</tt>, except that it creates a new <tt>Yamler::Template</tt>
6
+ # class and calls the <tt>render</tt> method on <tt>Yamler::Template</tt>.
7
+ #
8
+ # If a block is passed in the contents of that block will be made available to
9
+ # ERB when the rendering occurs.
10
+ #
11
+ # Examples:
12
+ # # Renders said file through ERB, and then through YAML.load:
13
+ # Yamler.load('/path/to/file.yml')
14
+ #
15
+ # Does the same as above but makes a method called say_hi
16
+ # available to the binding of the Yamler::Template instance.
17
+ # Yamler.load('/path/to/file.yml') do
18
+ # def say_hi
19
+ # 'hi'
20
+ # end
21
+ # end
22
+ def load(path, &block)
23
+ template = Yamler::Template.new(path)
24
+ if block_given?
25
+ template.instance_eval(&block)
26
+ end
27
+ YAML.load(template.render)
28
+ end
29
+
30
+ end
31
+
32
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: markbates-yamler
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - markbates
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-01-20 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: "yamler was developed by: markbates"
17
+ email: mark@mackframework.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ files:
25
+ - lib/yamler/template.rb
26
+ - lib/yamler/yamler.rb
27
+ - lib/yamler.rb
28
+ - README
29
+ - doc/classes/Yamler/Template.html
30
+ - doc/classes/Yamler.html
31
+ - doc/created.rid
32
+ - doc/files/lib/yamler/template_rb.html
33
+ - doc/files/lib/yamler/yamler_rb.html
34
+ - doc/files/lib/yamler_rb.html
35
+ - doc/files/README.html
36
+ - doc/fr_class_index.html
37
+ - doc/fr_file_index.html
38
+ - doc/fr_method_index.html
39
+ - doc/index.html
40
+ - doc/rdoc-style.css
41
+ has_rdoc: true
42
+ homepage: http://www.mackframework.com
43
+ post_install_message:
44
+ rdoc_options: []
45
+
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ requirements: []
61
+
62
+ rubyforge_project: magrathea
63
+ rubygems_version: 1.2.0
64
+ signing_key:
65
+ specification_version: 2
66
+ summary: yamler
67
+ test_files: []
68
+