blue_tree 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rvmrc ADDED
@@ -0,0 +1,48 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # This is an RVM Project .rvmrc file, used to automatically load the ruby
4
+ # development environment upon cd'ing into the directory
5
+
6
+ # First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
7
+ # Only full ruby name is supported here, for short names use:
8
+ # echo "rvm use 1.8.7" > .rvmrc
9
+ environment_id="ruby-1.8.7-p370@blue_tree"
10
+
11
+ # Uncomment the following lines if you want to verify rvm version per project
12
+ # rvmrc_rvm_version="1.15.8 (stable)" # 1.10.1 seams as a safe start
13
+ # eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
14
+ # echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
15
+ # return 1
16
+ # }
17
+
18
+ # First we attempt to load the desired environment directly from the environment
19
+ # file. This is very fast and efficient compared to running through the entire
20
+ # CLI and selector. If you want feedback on which environment was used then
21
+ # insert the word 'use' after --create as this triggers verbose mode.
22
+ if [[ -d "${rvm_path:-$HOME/.rvm}/environments"
23
+ && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
24
+ then
25
+ \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
26
+ [[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]] &&
27
+ \. "${rvm_path:-$HOME/.rvm}/hooks/after_use" || true
28
+ else
29
+ # If the environment file has not yet been created, use the RVM CLI to select.
30
+ rvm --create "$environment_id" || {
31
+ echo "Failed to create RVM environment '${environment_id}'."
32
+ return 1
33
+ }
34
+ fi
35
+
36
+ # If you use bundler, this might be useful to you:
37
+ # if [[ -s Gemfile ]] && {
38
+ # ! builtin command -v bundle >/dev/null ||
39
+ # builtin command -v bundle | GREP_OPTIONS= \grep $rvm_path/bin/bundle >/dev/null
40
+ # }
41
+ # then
42
+ # printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
43
+ # gem install bundler
44
+ # fi
45
+ # if [[ -s Gemfile ]] && builtin command -v bundle >/dev/null
46
+ # then
47
+ # bundle install | GREP_OPTIONS= \grep -vE '^Using|Your bundle is complete'
48
+ # fi
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'http://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in blue_tree.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Andy White
2
+
3
+ MIT License
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
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,100 @@
1
+ # BlueTree
2
+
3
+ Bluetree is a module of methods which allow you to create composable view
4
+ classes. Each class instance is initialized with an ERB template. The
5
+ templates of these views have access to all local instance variables and
6
+ methods plus access to the immediate parent and root node objects.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'blue_tree'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install blue_tree
21
+
22
+ ## Usage
23
+
24
+ Say you wanted to create a composite view using a couple of different classes:
25
+
26
+ require 'blue_tree'
27
+
28
+ class Page
29
+ include BlueTree::Node
30
+ use_template_path "path/to/my/templates" # defaults to "YOUR_GEM_PATH/templates"
31
+ use_template_ext ".foo.erb" # defaults to ".html.erb"
32
+
33
+ attr_reader :page_title, :introduction, :conclusion
34
+ # More Page specific code...
35
+ end
36
+
37
+ class Section
38
+ include BlueTree::Node
39
+
40
+ attr_reader :title, :description
41
+ # More Section specific code...
42
+ end
43
+
44
+ Set up your nodes (The symbol argument is the basename of the ERB template
45
+ file):
46
+
47
+ main_page = Page.new(:page_template)
48
+ section = Section.new(:section_template)
49
+ sub_section = Section.new(:sub_section_template)
50
+
51
+ Compose your nodes:
52
+
53
+ section.add_child sub_section
54
+ main_page.add_child section
55
+
56
+ What you can reference in the templates:
57
+
58
+ * any methods and variables in the current object
59
+ * *children_nodes* - returns an array of child node objects
60
+ * *parent_node* - returns the immediate parent object
61
+ * *root_node* - returns the root object
62
+
63
+ So for example, the sub_section template might look like this:
64
+
65
+ <h2><%= title %> (Part of the <%= root_node.page_title %> series)</h2>
66
+ <p>This follows on from the <%= parent_node.title %> section.</p>
67
+ <p><%= description %></p>
68
+
69
+ It's up to the templates to render any children nodes - so the main page
70
+ template might look like this (the trailing dash notation is accepted):
71
+
72
+ <h1><%= page_title %></h1>
73
+ <p><%= introduction %></p>
74
+ <% child_nodes.each do |c| -%>
75
+ <%= c.render %>
76
+ <% end -%>
77
+ <p><%= conclusion %></p>
78
+
79
+ Rendering:
80
+
81
+ main_page.render # => returns the recursively rendered string
82
+
83
+ ### Rendering arbitrary templates
84
+
85
+ You can also render templates not associated with any nodes by using the
86
+ *render_template* method. This can be useful if you want to reuse common
87
+ templates like headers or footers. The template must exist in your
88
+ *template_path*. Like any node, it has access to all variables and methods in the
89
+ current object, the parent object and the root object:
90
+
91
+ ...
92
+ <%= render_template(:common_footer_template) %>
93
+
94
+ ## Contributing
95
+
96
+ 1. Fork it
97
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
98
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
99
+ 4. Push to the branch (`git push origin my-new-feature`)
100
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'blue_tree/version'
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "blue_tree"
8
+ s.version = BlueTree::VERSION
9
+ s.authors = ["Andy White"]
10
+ s.email = ["andy@wireworldmedia.co.uk"]
11
+ s.description = %q{Build composite ERB templates}
12
+ s.summary = %q{Bluetree is a module of methods which allow you to create composable view
13
+ classes. Each class instance is initialized with an ERB template. The
14
+ templates of these views have access to all local instance variables and
15
+ methods plus access to the immediate parent and root node objects.}
16
+ s.homepage = ""
17
+ s.files = `git ls-files`.split($/)
18
+ s.executables = s.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
19
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
20
+ s.require_paths = ["lib"]
21
+ s.add_development_dependency "rspec", ">= 0"
22
+ end
@@ -0,0 +1,7 @@
1
+ <%= title %>
2
+
3
+ <%= description %>
4
+
5
+ <% child_nodes.each do |c| -%>
6
+ <%= c.render -%>
7
+ <% end -%>
@@ -0,0 +1,7 @@
1
+ Section 1.1
2
+
3
+ The root is titled 'The Main Title'.
4
+
5
+ This is a child of 'Section 1'.
6
+
7
+ Section 1.1 description...
@@ -0,0 +1,7 @@
1
+ <%= title %>
2
+
3
+ The root is titled '<%= root_node.page_title %>'.
4
+
5
+ This is a child of '<%= parent_node.title %>'.
6
+
7
+ <%= description %>
@@ -0,0 +1,9 @@
1
+ Section 1.2
2
+
3
+ Section 1.2 description...
4
+
5
+ ----
6
+ This is the footer.
7
+ This title is 'Section 1.2'.
8
+ Parent title is 'Section 1'.
9
+ Root title is 'The Main Title'.
@@ -0,0 +1,6 @@
1
+ <%= title %>
2
+
3
+ <%= description %>
4
+
5
+ ----
6
+ <%= render_template :footer %>
@@ -0,0 +1,4 @@
1
+ This is the footer.
2
+ This title is '<%= title %>'.
3
+ Parent title is '<%= parent_node.title %>'.
4
+ Root title is '<%= root_node.page_title %>'.
@@ -0,0 +1,5 @@
1
+ page start
2
+ <% children.each do |child| -%>
3
+ <%= child.render %>
4
+ <% end -%>
5
+ page end
@@ -0,0 +1,17 @@
1
+ The Main Title
2
+
3
+ The introduction...
4
+
5
+ Section 1
6
+
7
+ Section 1 description...
8
+
9
+ Section 1.1
10
+
11
+ The root is titled 'The Main Title'.
12
+
13
+ This is a child of 'Section 1'.
14
+
15
+ Section 1.1 description...
16
+
17
+ The conclusion...
@@ -0,0 +1,9 @@
1
+ <%= page_title %>
2
+
3
+ <%= introduction %>
4
+
5
+ <% child_nodes.each do |c| -%>
6
+ <%= c.render %>
7
+ <% end -%>
8
+
9
+ <%= conclusion %>
@@ -0,0 +1,2 @@
1
+ require "blue_tree/version"
2
+ require "blue_tree/node"
@@ -0,0 +1,75 @@
1
+ require 'erb'
2
+
3
+ module BlueTree
4
+ module Node
5
+ attr_reader :child_nodes, :parent_node, :root_node
6
+
7
+ module ClassMethods
8
+ def use_template_path(path)
9
+ @template_path = path
10
+ end
11
+
12
+ def template_path
13
+ @template_path
14
+ end
15
+
16
+ def use_template_ext(ext)
17
+ @template_ext = ext
18
+ end
19
+
20
+ def template_ext
21
+ @template_ext
22
+ end
23
+ end
24
+
25
+ def self.included(base)
26
+ base.extend ClassMethods
27
+ base.use_template_path(
28
+ File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'templates'))
29
+ )
30
+ base.use_template_ext '.html.erb'
31
+ end
32
+
33
+ def initialize(template_symbol)
34
+ @template = template_contents(template_symbol)
35
+ @child_nodes = []
36
+ @parent_node = nil
37
+ @root_node = nil
38
+ end
39
+
40
+ def template_path
41
+ self.class.template_path
42
+ end
43
+
44
+ def template_ext
45
+ self.class.template_ext
46
+ end
47
+
48
+ def template_contents(template_symbol)
49
+ File.read(File.join(template_path, "#{template_symbol}#{template_ext}"))
50
+ end
51
+
52
+ def add_child(child)
53
+ child.set_root_node(@root_node || self)
54
+ child.set_parent_node self
55
+ @child_nodes << child
56
+ end
57
+
58
+ def set_root_node(root_node)
59
+ @root_node = root_node
60
+ child_nodes.each { |child| child.set_root_node(root_node) }
61
+ end
62
+
63
+ def set_parent_node(parent)
64
+ @parent_node = parent
65
+ end
66
+
67
+ def render
68
+ ERB.new(@template, nil, '-').result(binding)
69
+ end
70
+
71
+ def render_template(template_symbol)
72
+ ERB.new(template_contents(template_symbol), nil, '-').result(binding)
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,3 @@
1
+ module BlueTree
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,4 @@
1
+ def fixtures(path = nil)
2
+ fixtures_path = File.join(File.dirname(__FILE__), '..', 'fixtures')
3
+ path.nil? ? fixtures_path : File.join(fixtures_path, path)
4
+ end
@@ -0,0 +1,112 @@
1
+ require 'blue_tree/node'
2
+ require File.dirname(__FILE__) + '/helper'
3
+
4
+ describe BlueTree::Node do
5
+ describe "Class defaults and settings" do
6
+ before(:each) do
7
+ class Foo
8
+ include BlueTree::Node
9
+ use_template_path fixtures
10
+ use_template_ext ".foo.erb"
11
+ end
12
+
13
+ class Bar
14
+ include BlueTree::Node
15
+ end
16
+ end
17
+
18
+ it "template_path set correctly" do
19
+ Foo.template_path.should == fixtures
20
+ Bar.template_path.should == File.expand_path(File.join(File.dirname(__FILE__), '..', 'templates'))
21
+ end
22
+
23
+ it "template_ext set correctly" do
24
+ Foo.template_ext.should == ".foo.erb"
25
+ Bar.template_ext.should == ".html.erb"
26
+ end
27
+ end
28
+
29
+ describe "Building a composite page" do
30
+ before(:each) do
31
+ class Page
32
+ include BlueTree::Node
33
+ use_template_path fixtures
34
+ use_template_ext '.txt.erb'
35
+ attr_accessor :page_title, :introduction, :conclusion
36
+ end
37
+
38
+ class Section
39
+ include BlueTree::Node
40
+ use_template_path fixtures
41
+ use_template_ext '.txt.erb'
42
+ attr_accessor :title, :description
43
+ end
44
+
45
+ @page = Page.new(:page)
46
+ @page.page_title = 'The Main Title'
47
+ @page.introduction = 'The introduction...'
48
+ @page.conclusion = 'The conclusion...'
49
+
50
+ @child1 = Section.new(:child1)
51
+ @child1.title = 'Section 1'
52
+ @child1.description = 'Section 1 description...'
53
+
54
+ @child2 = Section.new(:child2)
55
+ @child2.title = 'Section 1.1'
56
+ @child2.description = 'Section 1.1 description...'
57
+
58
+ @child1.add_child @child2
59
+ @page.add_child @child1
60
+ end
61
+
62
+ describe "Instance defaults" do
63
+ it "should have correct template path" do
64
+ @page.template_path.should == fixtures
65
+ end
66
+
67
+ it "should have correct template ext" do
68
+ @page.template_ext.should == '.txt.erb'
69
+ end
70
+ end
71
+
72
+ describe "Data access" do
73
+ it "should have access to child nodes" do
74
+ @page.child_nodes.should == [@child1]
75
+ end
76
+
77
+ it "should have access to parent node" do
78
+ @child1.parent_node.should == @page
79
+ @child2.parent_node.should == @child1
80
+ end
81
+
82
+ it "should have access to root node" do
83
+ @child1.root_node.should == @page
84
+ @child2.root_node.should == @page
85
+ end
86
+ end
87
+
88
+ describe "Rendering" do
89
+ it "leaf node should render as expected" do
90
+ @child2.render.should == File.read(fixtures('child2.txt'))
91
+ end
92
+
93
+ it "composite page should render as expected" do
94
+ @page.render.should == File.read(fixtures('page.txt'))
95
+ end
96
+
97
+ context "Arbitrary templates" do
98
+ before(:each) do
99
+ @child3 = Section.new(:child3)
100
+ @child3.title = 'Section 1.2'
101
+ @child3.description = 'Section 1.2 description...'
102
+
103
+ @child1.add_child @child3
104
+ end
105
+
106
+ it "should render arbritary template" do
107
+ @child3.render.should == File.read(fixtures('child3.txt'))
108
+ end
109
+ end
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,4 @@
1
+ child1
2
+ <% children.each do |child| -%>
3
+ <%= child.render -%>
4
+ <% end -%>
@@ -0,0 +1 @@
1
+ child2
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: blue_tree
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Andy White
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-09-11 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :development
33
+ version_requirements: *id001
34
+ description: Build composite ERB templates
35
+ email:
36
+ - andy@wireworldmedia.co.uk
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files: []
42
+
43
+ files:
44
+ - .gitignore
45
+ - .rvmrc
46
+ - Gemfile
47
+ - LICENSE.txt
48
+ - README.md
49
+ - Rakefile
50
+ - blue_tree.gemspec
51
+ - fixtures/child1.txt.erb
52
+ - fixtures/child2.txt
53
+ - fixtures/child2.txt.erb
54
+ - fixtures/child3.txt
55
+ - fixtures/child3.txt.erb
56
+ - fixtures/footer.txt.erb
57
+ - fixtures/page.foo.erb
58
+ - fixtures/page.txt
59
+ - fixtures/page.txt.erb
60
+ - lib/blue_tree.rb
61
+ - lib/blue_tree/node.rb
62
+ - lib/blue_tree/version.rb
63
+ - spec/helper.rb
64
+ - spec/node_spec.rb
65
+ - templates/child1.html.erb
66
+ - templates/child2.html.erb
67
+ homepage: ""
68
+ licenses: []
69
+
70
+ post_install_message:
71
+ rdoc_options: []
72
+
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ hash: 3
81
+ segments:
82
+ - 0
83
+ version: "0"
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ hash: 3
90
+ segments:
91
+ - 0
92
+ version: "0"
93
+ requirements: []
94
+
95
+ rubyforge_project:
96
+ rubygems_version: 1.8.24
97
+ signing_key:
98
+ specification_version: 3
99
+ summary: Bluetree is a module of methods which allow you to create composable view classes. Each class instance is initialized with an ERB template. The templates of these views have access to all local instance variables and methods plus access to the immediate parent and root node objects.
100
+ test_files:
101
+ - spec/helper.rb
102
+ - spec/node_spec.rb