plow 0.1.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -4,6 +4,6 @@ coverage
4
4
  rdoc
5
5
  pkg
6
6
  .yardoc
7
- doc
8
7
  plow.gemspec
9
- plow.tmproj
8
+ plow.tmproj
9
+ yardoc
data/.yardopts CHANGED
@@ -1,6 +1,8 @@
1
+ --output-dir ./yardoc
1
2
  --markup markdown
2
3
  --private
3
4
  -
4
5
  LICENSE
5
- HISTORY
6
- SECURITY
6
+ doc/HISTORY.markdown
7
+ doc/SECURITY.markdown
8
+ doc/ROAD-MAP.markdown
data/README.markdown CHANGED
@@ -2,26 +2,24 @@
2
2
 
3
3
  Image courtesy of [Mother Earth News](http://www.motherearthnews.com/Modern-Homesteading/1974-05-01/Walking-Plow.aspx)
4
4
 
5
- PLOW Release 0.1.0 (October 13th 2009)
5
+ Plow release 1.0.0 (November 21, 2009)
6
6
  ======================================
7
7
 
8
- Copyright © 2009 Ryan Sobol. Licensed under the MIT license. Please see the {file:LICENSE} for more information.
8
+ Copyright (c) 2009 Ryan Sobol. Licensed under the MIT license. Please see the {file:LICENSE} for more information.
9
9
 
10
- **Homepage**: [http://github.com/ryansobol/plow](http://github.com/ryansobol/plow)
11
-
12
- **Documentation**: [http://rdoc.info/projects/ryansobol/plow](http://rdoc.info/projects/ryansobol/plow)
10
+ **Homepage**: [http://gemcutter.org/gems/plow](http://gemcutter.org/gems/plow)
11
+ **Source Code**: [http://github.com/ryansobol/plow](http://github.com/ryansobol/plow)
12
+ **Documentation**: [http://yardoc.org/docs/ryansobol-plow](http://yardoc.org/docs/ryansobol-plow)
13
13
  **Code Metrics**: [http://devver.net/caliper/project?repo=git%3A%2F%2Fgithub.com%2Fryansobol%2Fplow.git](http://devver.net/caliper/project?repo=git%3A%2F%2Fgithub.com%2Fryansobol%2Fplow.git)
14
+ **Bug Tracker**: [http://github.com/ryansobol/plow/issues](http://github.com/ryansobol/plow/issues)
15
+ **Wiki**: [http://wiki.github.com/ryansobol/plow](http://wiki.github.com/ryansobol/plow)
14
16
 
15
17
  WHAT'S NEW?
16
18
  -----------
17
19
 
18
- **v0.1.0**
19
-
20
- * Canonical 'plow' namespace reserved on [Gemcutter](http://gemcutter.org/gems/plow) and [Rubyforge](http://rubyforge.org/projects/plow/)
21
-
22
- I'm currently working at towards assembling a "complete software" package for you. Please stay tuned for the official launch of version 1.0.0. :D
20
+ The first official public release of Plow!
23
21
 
24
- Please see {file:HISTORY} for the historical overview of the project.
22
+ Please see {file:doc/HISTORY.markdown} for the historical overview of the project.
25
23
 
26
24
  SYNOPSIS
27
25
  --------
@@ -47,13 +45,12 @@ REQUIREMENTS
47
45
 
48
46
  * Ruby 1.9.1
49
47
 
50
- **Optional automated specification dependencies**
48
+ **Optional development dependencies**
51
49
 
52
- * RSpec 1.2.8
53
-
54
- **Optional generated API documentation dependencies**
55
-
56
- * YARD 0.2.3.5
50
+ * Rake 0.8.7 (bundled with Ruby 1.9.1)
51
+ * Jeweler 1.3.0
52
+ * RSpec 1.2.9
53
+ * YARD 0.4.0
57
54
  * BlueCloth 2.0.5
58
55
 
59
56
  INSTALLING
@@ -63,7 +60,9 @@ Plow is distributed though the RubyGems ecosystem. Assuming you've already inst
63
60
 
64
61
  sudo gem install plow
65
62
 
66
- LET ME BE CRYSTAL CLEAR. DO NOT INSTALL THIS PACKAGE TO ANY NON-ROOT USER'S GEM REPOSITORY. YOU'VE BEEN WARNED. Please see {file:SECURITY} for a detailed explanation of this advisory.
63
+ **LET ME BE CRYSTAL CLEAR. DO NOT INSTALL THIS PACKAGE TO ANY NON-ROOT USER'S GEM REPOSITORY. YOU'VE BEEN WARNED.**
64
+
65
+ Please see {file:doc/SECURITY.markdown} for a detailed explanation of this advisory.
67
66
 
68
67
  Note that Plow is RubyGems **compatible** while also simultaneously **decoupled** from it. Feel free to install Plow manually or use any Ruby package management system of your choice.
69
68
 
@@ -89,13 +88,38 @@ Plow is bundled with two executables named `plow` and `plow1.9`. They are nearl
89
88
  EXAMPLE
90
89
  -------
91
90
 
92
- $ plow steve www.apple.com apple.com
91
+ $ sudo plow steve www.apple.com apple.com
92
+ Plow 1.0.0. Copyright (c) 2009 Ryan Sobol. Licensed under the MIT license.
93
+ ==> creating steve user
94
+ Adding user `steve' ...
95
+ Adding new group `steve' (1001) ...
96
+ Adding new user `steve' (1001) with group `steve' ...
97
+ Creating home directory `/home/steve' ...
98
+ Copying files from `/etc/skel' ...
99
+ Enter new UNIX password:
100
+ Retype new UNIX password:
101
+ passwd: password updated successfully
102
+ Changing the user information for steve
103
+ Enter the new value, or press ENTER for the default
104
+ Full Name []:
105
+ Room Number []:
106
+ Work Phone []:
107
+ Home Phone []:
108
+ Other []:
109
+ Is the information correct? [y/N] y
110
+ ==> existing /home/steve
111
+ ==> creating /home/steve/sites
112
+ ==> creating /home/steve/sites/www.apple.com
113
+ ==> creating /home/steve/sites/www.apple.com/public
114
+ ==> creating /home/steve/sites/www.apple.com/log
115
+ ==> creating /etc/apache2/sites-available/www.apple.com.conf
116
+ ==> installing /etc/apache2/sites-available/www.apple.com.conf
93
117
 
94
- ... plow interation ...
118
+ $ su - steve
119
+ Password:
95
120
 
96
121
  $ tree /home/steve/sites/
97
122
  /home/steve/sites/
98
- |-- README
99
123
  `-- www.apple.com
100
124
  |-- log
101
125
  | `-- apache2
@@ -103,50 +127,71 @@ EXAMPLE
103
127
  | `-- error.log
104
128
  `-- public
105
129
  `-- index.html
106
-
107
- 4 directories, 4 files
130
+
131
+ 4 directories, 3 files
108
132
 
109
133
  $ ls -hal /home/steve/sites/www.apple.com/log/apache2/
110
- total 196K
111
- drwxr-x--- 2 root steve 4.0K Sep 5 03:11 .
112
- drwxr-xr-x 3 steve steve 4.0K Sep 5 03:09 ..
113
- -rw-r----- 1 root steve 136K Sep 9 11:10 access.log
114
- -rw-r----- 1 root steve 48K Sep 9 09:06 error.log
134
+ total 8.0K
135
+ drwxr-x--- 2 root steve 4.0K 2009-11-21 16:33 .
136
+ drwxr-xr-x 3 steve steve 4.0K 2009-11-21 16:33 ..
137
+ -rw-r----- 1 root steve 0 2009-11-21 16:33 access.log
138
+ -rw-r----- 1 root steve 0 2009-11-21 16:33 error.log
139
+
140
+ $ cat /etc/apache2/sites-available/www.apple.com.conf
141
+
142
+ <VirtualHost *:80>
143
+ ServerAdmin webmaster
144
+ ServerName www.apple.com
145
+
146
+ ServerAlias apple.com
147
+
148
+ DirectoryIndex index.html
149
+ DocumentRoot /home/steve/sites/www.apple.com/public
150
+
151
+ LogLevel warn
152
+ ErrorLog /home/steve/sites/www.apple.com/log/apache2/error.log
153
+ CustomLog /home/steve/sites/www.apple.com/log/apache2/access.log combined
154
+ </VirtualHost>
155
+
115
156
 
116
157
  MOTIVATION
117
158
  ----------
118
159
 
119
- There exists numerous software products for controlling and/or managing a unix-based operating systems and web servers. So, why did I decide to write yet another? With Plow, I needed to prove to myself that I have the necessary proficiency **to contribute free, open-source, and complete software for the benefit human-kind**.
160
+ As of the time of writing, there exists countless software products for managing unix-based operating systems and web servers. So then, why did I decide to write yet another? For me, Plow was a self-issued challenge **to contribute free, open-source, and complete software for the benefit human-kind**.
120
161
 
121
- The terms "free" and "open-source" are common enough to be implicitly understood, or at least, [easily googled](http://www.google.com/searchq=free+open-source). But what of the term "complete software"? For me, complete software means four exclusive characteristics:
162
+ The terms "free" and "open-source" software are common enough to be implicitly understood, or at least, [easily googled](http://www.google.com/searchq=free+open-source). But what of the term "complete software"? For me, complete software has four exclusive characteristics:
122
163
 
123
- * Usability enhanced through elegant user-interface combined with straight-forward documentation
164
+ * Usability enhanced through elegant user-interface and straight-forward documentation
124
165
  * Stability verified through automated test specifications
125
166
  * Performance demonstrated through benchmarks
126
167
  * Sustainability preserved through both human and computer readable code
127
168
 
128
- I have tirelessly worked to achieve a high standard in all four characteristics. As my peer, I am grateful of your input on improving this project in any of these areas. Please see the sections on REPORTING ISSUES and CONTRIBUTING for further information.
169
+ Though never finished, I have worked tirelessly to set a the bar high within each of these characteristics. As my peer, I am grateful of your input on improving this project. If you are interested in generously donating your time to Plow, please read the below sections on REPORTING ISSUES and CONTRIBUTING to learn how to best direct your energy.
129
170
 
130
171
  REPORTING ISSUES
131
172
  ----------------
132
173
 
133
- Is Plow not behaving like you expect it should? Please forgive me! Would you take a moment to shed light on my negligence over at the [Issue Tracker](http://github.com/ryansobol/plow/issues)?
174
+ Is Plow not behaving like you expect it should? Please forgive me! Would you take a moment to shed light on my negligence over at the [Issue Tracker](http://github.com/ryansobol/plow/issues)? Here's a **Pro Tip** -- you can read through existing issues and vote for which issues you'd like to see resolved first!
134
175
 
135
- Thank you, in advance, for taking the time to help improve Plow.
176
+ Thank you for taking the time to help improve Plow.
136
177
 
137
178
  CONTRIBUTING
138
179
  ------------
139
180
 
140
- Is Plow not behaving like you need? Open-source to the rescue! There is a plethora of documentation to bring you up to speed. The API documentation is generated, once the dependencies are met (please see the REQUIREMENTS section), by running the follow:
181
+ Is Plow not behaving like you need? Open-source to the rescue! There is a plethora of documentation to bring a Rubyist of any level up to speed. The API documentation is generated, once the dependencies are met (please see the REQUIREMENTS section), by issuing the follow command:
141
182
 
142
- $ rake yardoc
183
+ $ rake yard
143
184
 
144
- Patches are always welcome and appreciated! To contribute your work back to the source, the process is straight-forward:
185
+ Patches are always welcome and appreciated! The process is straight-forward for contributing your work back to the source:
145
186
 
146
187
  * Fork the project -- may I suggest [Github](http://www.github.com)?
147
- * Make your feature addition or bug fix with specifications -- it's important that I don't break your hard work in a future version unintentionally.
148
- * Please do not casually alter files in the project root. (e.g. HISTORY, LICENSE, Rakefile, README.markdown, ROAD-MAP, SECURITY, VERSION, etc.)
188
+ * Make your feature addition or bug fix **with specifications**. It's important that I don't break your hard work in a future version unintentionally.
189
+ * Please do not casually alter files in the project root. (e.g. LICENSE, Rakefile, README.markdown, VERSION, etc.)
149
190
  * Commit your changes and publish the change-set.
150
- * Send me a pull request. All specs must pass!
191
+ * Send me a pull request. Remember, all specs must pass!
192
+
193
+ Before making your change, take a moment to get a feel for the style of coding, specifications, and in-line documentation.
194
+
195
+ Please see {file:doc/ROAD-MAP.markdown} to learn how to flow with the project. In the near future, I plan on pushing this content directly into the [Issue Tracker](http://github.com/ryansobol/plow/issues).
151
196
 
152
- Take a moment to get a feel for the style of coding, specifications, and in-line documentation. I apologize in advance for setting the standards so high. Please see {file:ROAD-MAP} to learn how to flow with the project.
197
+ Again, thank you for taking the time to help improve Plow.
data/Rakefile CHANGED
@@ -1,52 +1,58 @@
1
- require 'rubygems'
2
- require 'rake'
1
+ # encoding: UTF-8
2
+ require 'lib/plow'
3
+ Plow::Dependencies.warn_at_exit
4
+
5
+ ###################################################################################################
3
6
 
4
7
  begin
8
+ # TODO experiment with how rubygems activates gems by version requirement in ruby 1.9.1.
9
+ # gem 'jeweler', '1.3.0'
5
10
  require 'jeweler'
6
11
  Jeweler::Tasks.new do |gem|
12
+ # The gem object is a kind of Gem::Specification
13
+ # For additional settings, see http://www.rubygems.org/read/chapter/20
7
14
  gem.name = "plow"
8
- gem.rubyforge_project = "plow"
15
+ gem.rubyforge_project = "plow" # rubyforge's days are numbered...
9
16
  gem.summary = "Plows the fertile soil of your filesystem into neatly organized plots of web-site templates"
10
17
  gem.description = "Plows the fertile soil of your filesystem into neatly organized plots of web-site templates"
11
18
  gem.email = "code@ryansobol.com"
12
19
  gem.homepage = "http://github.com/ryansobol/plow"
13
- gem.authors = ["Ryan Sobol"]
14
- gem.add_development_dependency "rspec", "= 1.2.8"
15
- gem.add_development_dependency "yard", "= 0.2.3.5"
16
- gem.add_development_dependency "bluecloth", "= 2.0.5" # hidden yard dependency for markdown support
17
- # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
20
+ gem.authors = ["Ryan Sobol"]
21
+
22
+ gem.required_ruby_version = "~> #{Plow::Dependencies::REQUIRED_RUBY_VERSION}"
23
+
24
+ Plow::Dependencies::DEVELOPMENT_GEMS.each_pair do |name, version|
25
+ gem.add_development_dependency(name.to_s, version)
26
+ end
18
27
  end
28
+
19
29
  Jeweler::GemcutterTasks.new
20
30
 
21
31
  Jeweler::RubyforgeTasks.new do |task|
22
- task.doc_task = "yardoc"
32
+ task.doc_task = false # rubyforge's days are numbered...
23
33
  end
24
- rescue LoadError
25
- puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
34
+ rescue LoadError => e
35
+ Plow::Dependencies.create_warning_for(e)
26
36
  end
27
37
 
28
- require 'spec/rake/spectask'
29
- Spec::Rake::SpecTask.new(:spec) do |task|
30
- task.libs << 'lib' << 'spec'
31
- task.spec_files = FileList['spec/**/*_spec.rb']
32
- task.spec_opts = %w{ -O spec/spec.opts }
33
- end
38
+ ###################################################################################################
34
39
 
35
- Spec::Rake::SpecTask.new(:rcov) do |task|
36
- task.libs << 'lib' << 'spec'
37
- task.pattern = 'spec/**/*_spec.rb'
38
- task.rcov = true
40
+ begin
41
+ require 'spec/rake/spectask'
42
+ Spec::Rake::SpecTask.new(:spec)
43
+ task :default => :spec
44
+ rescue LoadError => e
45
+ Plow::Dependencies.create_warning_for(e)
39
46
  end
40
47
 
41
- task :spec => :check_dependencies
42
-
43
- task :default => :spec
48
+ ###################################################################################################
44
49
 
45
50
  begin
46
51
  require 'yard'
47
- YARD::Rake::YardocTask.new
48
- rescue LoadError => e
49
- task :yardoc do
50
- abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
52
+ require 'bluecloth' # hidden yard dependency for markdown support
53
+ YARD::Rake::YardocTask.new(:yard) do |t|
54
+ t.options += ['--title', "Plow #{Plow::VERSION} Documentation"]
51
55
  end
56
+ rescue LoadError => e
57
+ Plow::Dependencies.create_warning_for(e)
52
58
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 1.0.0
data/bin/plow CHANGED
@@ -1,10 +1,4 @@
1
1
  #!/usr/bin/env ruby
2
2
  # encoding: UTF-8
3
-
4
- unless $LOAD_PATH.include?(File.expand_path(File.dirname(__FILE__) + '/../lib'))
5
- $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + '/../lib'))
6
- end
7
-
8
- require 'plow'
9
-
3
+ require File.expand_path(File.dirname(__FILE__) + '/../lib/plow')
10
4
  Plow::Application.launch(*ARGV)
data/bin/plow1.9 CHANGED
@@ -1,10 +1,4 @@
1
1
  #!/usr/bin/env ruby1.9
2
2
  # encoding: UTF-8
3
-
4
- unless $LOAD_PATH.include?(File.expand_path(File.dirname(__FILE__) + '/../lib'))
5
- $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + '/../lib'))
6
- end
7
-
8
- require 'plow'
9
-
3
+ require File.expand_path(File.dirname(__FILE__) + '/../lib/plow')
10
4
  Plow::Application.launch(*ARGV)
@@ -0,0 +1,41 @@
1
+ Plow release 1.0.0 (November 21, 2009)
2
+ ========================================
3
+
4
+ The first official public release of Plow!
5
+
6
+ SYNOPSIS
7
+ --------
8
+
9
+ Plows the fertile soil of your filesystem into neatly organized plots of web-site templates
10
+
11
+ FEATURES
12
+ --------
13
+
14
+ 1. Sharpens it's blade by ensuring that both a Linux system user account and it's home path exist
15
+ 2. Penetrates the soil by forming the web-site root path within the user home
16
+ 3. Seeds the web-site with an index page and web server log files
17
+ 4. Fertilizes the web-site by installing a virtual host configuration into the web server
18
+
19
+ REQUIREMENTS
20
+ ------------
21
+
22
+ **Supported operating system + web server combinations**
23
+
24
+ * Linux Ubuntu 8.04.3 LTS (Hardy Heron) + Apache 2.2.8
25
+
26
+ **Required dependencies**
27
+
28
+ * Ruby 1.9.1
29
+
30
+ **Optional development dependencies**
31
+
32
+ * Rake 0.8.7 (bundled with Ruby 1.9.1)
33
+ * Jeweler 1.3.0
34
+ * RSpec 1.2.9
35
+ * YARD 0.4.0
36
+ * BlueCloth 2.0.5
37
+
38
+ Plow release 0.1.0 (October 13, 2009)
39
+ =======================================
40
+
41
+ * Canonical gem namespace, 'plow', reserved on [Gemcutter](http://gemcutter.org/gems/plow) and [Rubyforge](http://plow.rubyforge.org/)
@@ -0,0 +1,12 @@
1
+ Road-map of Potential Improvements
2
+ ==================================
3
+
4
+ In no particular order of importance:
5
+
6
+ * Generating and installing web-site specific `logrotated` scripts
7
+ * Removing plowed directories and files and uninstalling service scripts
8
+ * Additional template generating strategies of operating systems and web-sever combinations
9
+ * Run-time checking and validating of operating systems and installed web-servers
10
+ * Generating different web-site template structures
11
+ * Automatic deployment of web-sites from SCM repositories
12
+ * Flexible argument parsing including option flags as needed
@@ -1,14 +1,15 @@
1
- == A Brief Note About Security
1
+ A Brief Note About Security
2
+ ===========================
2
3
 
3
4
  Any and all processes invoking this library must be owned, aka executed, by the root user. Root ownership is required by this package to automate system administrative tasks, including but not limited to:
4
5
 
5
6
  * Creating and/or modifying operating system user accounts that are _typically controlled by the root user_
6
7
  * Generating and installing virtual host configuration files for web servers that are _typically owned by the root user_
7
8
 
8
- By installing this package, you are acknowledging the risks inherit of the above implications. If this package, including the library source code and it's non-compiled executables, is owned by any non-root user, then anyone who has or gains access to that non-root account could modify the code in this package, without your knowledge. The next time you execute the program, as the root user, you could now be unwittingly comprising your own system by running malicious code injected into this package.
9
+ By installing this package, you are acknowledging the risks inherit of the aforementioned tasks. If this package, including the library source code and it's non-compiled executables, is owned by any non-root user, then anyone who has or gains access to that non-root account could modify the code in this package, without your knowledge. The next time you execute the program, as the root user, you could now be unwittingly comprising your own system by running malicious code injected into this package.
9
10
 
10
11
  If you wish to install this package as a RubyGem with a user account granted root privileges via sudo, then please install this package by running the following command:
11
12
 
12
13
  sudo gem install plow
13
14
 
14
- LET ME BE CRYSTAL CLEAR. DO NOT INSTALL THIS PACKAGE TO ANY NON-ROOT USER'S GEM REPOSITORY. YOU'VE BEEN WARNED.
15
+ **LET ME BE CRYSTAL CLEAR. DO NOT INSTALL THIS PACKAGE TO ANY NON-ROOT USER'S GEM REPOSITORY. YOU'VE BEEN WARNED.**
@@ -2,8 +2,34 @@
2
2
  require 'plow/generator'
3
3
 
4
4
  class Plow
5
+ # It's probably no surprise that `Plow::Application` is an **application** class.
6
+ #
7
+ # With a single public method, `.launch`, it has the distinct honor of being the point of
8
+ # execution for the library. As described further in the documentation, it designed to
9
+ # accept and parse raw command-line arguments (i.e. the `ARGV` constant).
10
+ #
11
+ # @see Plow::Application.launch
5
12
  class Application
6
13
  class << self
14
+ # The starting point of executation for the Plow library. The procedure for launching the
15
+ # `Plow::Application` is as follows:
16
+ #
17
+ # 1. Output a version stamp.
18
+ # 2. Ensure at least 2 arguments are provided or aborting execution with a usage message.
19
+ # 3. Parse the user-supplied arguments.
20
+ # 4. Start a new `Plow::Generator` while handling any library specific exceptions raised.
21
+ #
22
+ # @example A simple executable file (assume a working Ruby $LOAD_PATH)
23
+ # #!/usr/bin/env ruby1.9
24
+ # require 'plow'
25
+ # Plow::Application.launch(*ARGV)
26
+ #
27
+ # @param [Array] *arguments A splatted `Array` of user-specified, command-line arguments.
28
+ # At least 2 arguments must be provided.
29
+ # @return [Number] Success returns 0, while failure results in any raised `Exception`. :(
30
+ # @raise [SystemExit] Raised when a critical, library-specific exception is rescued and
31
+ # executation must be terminiated.
32
+ # @see http://www.ruby-doc.org/ruby-1.9/classes/SystemExit.html
7
33
  def launch(*arguments)
8
34
  puts version_stamp
9
35
 
@@ -58,8 +84,14 @@ MESSAGE
58
84
 
59
85
  private
60
86
 
87
+ # Similar to a time stamp, this will return a version stamp of the Plow library containing
88
+ # the version number along with the copyright and license details.
89
+ #
90
+ # @return [String] Version stamp
91
+ # @example Sample version stamp
92
+ # Plow 1.0.0. Copyright (c) 2009 Ryan Sobol. Licensed under the MIT license.
61
93
  def version_stamp
62
- "Plow v#{Plow::VERSION}. Copyright (c) 2009 Ryan Sobol. Licensed under the MIT license."
94
+ "Plow #{Plow::VERSION}. Copyright (c) 2009 Ryan Sobol. Licensed under the MIT license."
63
95
  end
64
96
  end
65
97
  end
@@ -1,13 +1,49 @@
1
1
  # encoding: UTF-8
2
2
 
3
3
  class Plow
4
+ # `BindingStruct` is an **adapter** between the friendly declarative-style of `Hash` key/value
5
+ # pairs and the built-in, `ERB` (i.e. embedded ruby) class, which takes an optional `Binding`
6
+ # object for the scope of template evaluation.
7
+ #
8
+ # This class is very similar to Ruby's built-in `OpenStruct` class. It differs internally as
9
+ # `BindingStruct` converts each element of the `Hash` object into an instance variable during
10
+ # initialization. Once initialized, an instance of `BindingStruct` is able to return a
11
+ # reference to it's internal `Binding` object. This object can be further passed along to an
12
+ # `ERB` object for evaluation within a template.
13
+ #
14
+ # @example Sample template file at /path/to/a/template/file.txt'
15
+ # <%= @first_name %> <%= @last_name %> invented the Universe.
16
+ #
17
+ # @example Evaluating an `ERB` template file with a `BindingStruct` context
18
+ # template = ERB.new(File.read('/path/to/a/template/file.txt'))
19
+ # context = Plow::BindingStruct.new({ first_name: 'Carl', last_name: 'Sagan' })
20
+ # result = template.result(context.get_binding) #=> Carl Sagan invented the Universe.
21
+ #
22
+ # @see http://www.ruby-doc.org/ruby-1.9/classes/Binding.html
23
+ # @see http://www.ruby-doc.org/ruby-1.9/classes/ERB.html
24
+ # @see http://www.ruby-doc.org/ruby-1.9/classes/OpenStruct.html
4
25
  class BindingStruct
5
- def initialize(hash)
6
- hash.each_pair do |name, val|
26
+
27
+ # Creates a new `BindingStruct` from a `Hash` where each key/value pair is converted to an
28
+ # instance variable.
29
+ #
30
+ # @param [Hash] source A source of data
31
+ # @example A peek into the internals of an instance post initialization
32
+ # bstruct = Plow::BindingStruct.new({first_name: 'Carl', last_name: 'Sagan'})
33
+ # bstruct.instance_variables.sort #=> [:@first_name, :@last_name]
34
+ # bstruct.instance_variable_get(:@first_name) #=> 'Carl'
35
+ # bstruct.instance_variable_get(:@last_name) #=> 'Sagan'
36
+ def initialize(source)
37
+ source.each_pair do |name, val|
7
38
  instance_variable_set("@#{name}".to_sym, val)
8
39
  end
9
40
  end
10
41
 
42
+ # Returns a reference to its internal `Binding` which can be used in conjunction with `ERB`
43
+ # template evaluation.
44
+ #
45
+ # @return [Binding] An reference to its internal `Binding`
46
+ # @see http://www.ruby-doc.org/ruby-1.9/classes/Binding.html
11
47
  def get_binding
12
48
  binding
13
49
  end
@@ -1,7 +1,15 @@
1
1
  # encoding: UTF-8
2
2
 
3
+ # Mindfully expanding the base `Object` class with sensible methods.
3
4
  class Object
4
- # See http://api.rubyonrails.org/classes/Object.html#M000279
5
+ # An object is blank if it‘s false, empty, or a whitespace string.
6
+ # For example, "", " ", nil, [], and {} are blank.
7
+ #
8
+ # @example This simplifies
9
+ # if !address.nil? && !address.empty?
10
+ # @example to
11
+ # if !address.blank?
12
+ # @see http://api.rubyonrails.org/classes/Object.html#M000279
5
13
  def blank?
6
14
  respond_to?(:empty?) ? empty? : !self
7
15
  end