herbaltee 0.0.1

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/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+ group :development do
3
+ gem 'rake'
4
+ gem 'rspec'
5
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,20 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.3)
5
+ rake (0.9.2.2)
6
+ rspec (2.11.0)
7
+ rspec-core (~> 2.11.0)
8
+ rspec-expectations (~> 2.11.0)
9
+ rspec-mocks (~> 2.11.0)
10
+ rspec-core (2.11.1)
11
+ rspec-expectations (2.11.3)
12
+ diff-lcs (~> 1.1.3)
13
+ rspec-mocks (2.11.3)
14
+
15
+ PLATFORMS
16
+ ruby
17
+
18
+ DEPENDENCIES
19
+ rake
20
+ rspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ (The MIT License)
2
+
3
+ Copyright (c) 2012 Matt Whiteley
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ 'Software'), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1 @@
1
+ This is a simple tool for writing out files (mainly software configuration) using `ERB` templates. Overwritten files are backed up with a timestamp suffix.
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ require 'rspec/core/rake_task'
2
+ RSpec::Core::RakeTask.new(:spec)
3
+ task :default => :spec
data/lib/herbaltee.rb ADDED
@@ -0,0 +1,19 @@
1
+ require 'erb'
2
+ require 'fileutils'
3
+ require 'logger'
4
+
5
+ module Herbaltee
6
+ DATE_FORMAT = '%Y%m%d%H%M%S'
7
+ LOG_LOCATION = STDOUT
8
+
9
+ def self.logger
10
+ @logger ||= Logger.new(LOG_LOCATION)
11
+ end
12
+
13
+ def self.now
14
+ Time.now.strftime(DATE_FORMAT)
15
+ end
16
+ end
17
+
18
+ require 'herbaltee/binder'
19
+ require 'herbaltee/template'
@@ -0,0 +1,32 @@
1
+ module Herbaltee
2
+ class Binder < BasicObject
3
+ attr_reader :__vars__
4
+ def __binding__
5
+ ::Kernel.binding
6
+ end
7
+
8
+ def initialize(vars = {})
9
+ basic_object_methods = [
10
+ :equal?,
11
+ :instance_eval,
12
+ :instance_exec,
13
+ :method_missing,
14
+ :singleton_method_added,
15
+ :singleton_method_removed,
16
+ :singleton_method_undefied,
17
+ ]
18
+ if (basic_object_methods & vars.keys).any?
19
+ ::Kernel.raise "Invalid variable name"
20
+ end
21
+ @__vars__ = vars
22
+ end
23
+
24
+ def method_missing(meth, *args)
25
+ if @__vars__ && @__vars__.has_key?(meth)
26
+ @__vars__[meth]
27
+ else
28
+ super
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,23 @@
1
+ module Herbaltee
2
+ class Template
3
+ attr_reader :content, :vars
4
+
5
+ def initialize(file, vars)
6
+ @content = File.open(file).read
7
+ @vars = vars
8
+ end
9
+
10
+ def render
11
+ b = Binder.new(@vars).__binding__
12
+ template = ERB.new(@content)
13
+ template.result(b)
14
+ end
15
+
16
+ def write(target)
17
+ if File.exists?(target)
18
+ FileUtils.cp(target, [target, Herbaltee.now].join('.'))
19
+ end
20
+ File.open(target, 'w') {|f| f.write(render)}
21
+ end
22
+ end
23
+ end
@@ -0,0 +1 @@
1
+ The value of x is: <%= x %>
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Herbaltee::Binder" do
4
+ it "makes an object with callable attributes from a given hash" do
5
+ binder = Herbaltee::Binder.new({foo: 'foo', derp: 'derp'})
6
+ binder.foo.should == 'foo'
7
+ binder.derp.should == 'derp'
8
+ end
9
+ it "prevents invalid attribute names" do
10
+ expect { Herbaltee::Binder.new({method_missing: "nope"}) }.to raise_error
11
+ end
12
+ end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+ require 'tempfile'
3
+
4
+ describe "Herbaltee::Template" do
5
+ describe "#render" do
6
+ it "renders a template with given variables" do
7
+ template = Herbaltee::Template.new('spec/fixtures/template.erb', {x: 42})
8
+ template.render.should == "The value of x is: 42\n"
9
+ end
10
+ end
11
+ describe "#write" do
12
+ it "writes a rendered template" do
13
+ template = Herbaltee::Template.new('spec/fixtures/template.erb', {x: 42})
14
+ Tempfile.open('template') do |file|
15
+ template.write(file.path)
16
+ file.read.should == template.render
17
+ end
18
+ end
19
+ it "creates a backup" do
20
+ template = Herbaltee::Template.new('spec/fixtures/template.erb', {x: 42})
21
+ template2 = Herbaltee::Template.new('spec/fixtures/template.erb', {x: 43})
22
+ Time.stub(:now).and_return(Time.at(0))
23
+ Tempfile.open('template') do |file|
24
+ template.write(file.path)
25
+ begin
26
+ template2.write(file.path)
27
+ file2 = [file.path, '19691231160000'].join('.')
28
+ file.read.should == template2.render
29
+ File.read(file2).should == template.render
30
+ ensure
31
+ File.delete(file2) if File.exists?(file2)
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,10 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Herbaltee" do
4
+ describe "#now" do
5
+ it "formats a timestamp correctly" do
6
+ Time.stub(:now).and_return(Time.at(0))
7
+ Herbaltee.now.should == "19691231160000"
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,2 @@
1
+ $:.unshift(File.join(File.dirname(File.dirname(__FILE__)), 'lib'))
2
+ require 'herbaltee'
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: herbaltee
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Matt Whiteley
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Render and write templates using ERB
47
+ email:
48
+ - mwhiteley@engineyard.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - lib/herbaltee/binder.rb
54
+ - lib/herbaltee/template.rb
55
+ - lib/herbaltee.rb
56
+ - LICENSE
57
+ - README.md
58
+ - spec/fixtures/template.erb
59
+ - spec/herbaltee/binder_spec.rb
60
+ - spec/herbaltee/template_spec.rb
61
+ - spec/herbaltee_spec.rb
62
+ - spec/spec_helper.rb
63
+ - Rakefile
64
+ - Gemfile
65
+ - Gemfile.lock
66
+ homepage: http://github.com/engineyard/herbaltee/
67
+ licenses: []
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '1.9'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubyforge_project:
86
+ rubygems_version: 1.8.23
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: Basic ERB templater
90
+ test_files:
91
+ - spec/fixtures/template.erb
92
+ - spec/herbaltee/binder_spec.rb
93
+ - spec/herbaltee/template_spec.rb
94
+ - spec/herbaltee_spec.rb
95
+ - spec/spec_helper.rb
96
+ - Rakefile
97
+ - Gemfile
98
+ - Gemfile.lock