radius-template 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -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/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in radius-template.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 TODO: Write your name
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.
data/README.md ADDED
@@ -0,0 +1,201 @@
1
+ # Radius::Template
2
+
3
+ Simple Ruby on Rails wrapper around radius gem.
4
+
5
+ Based on the work of [Radius Template](https://github.com/macbury/Radius-template).
6
+
7
+ ## Changelog
8
+
9
+
10
+ * Gemification.
11
+ * Rails 3 compatible.
12
+ * Some performance things.
13
+ * Removed autolink option.
14
+ * Work with any object that respond to method :each, not only ActiveRecord Objects.
15
+ * Changed the naming of vars from composed class names. Example: WebContent: before => webcontent; after => web_content.
16
+
17
+
18
+ ## Installation
19
+
20
+ Add this line to your application's Gemfile:
21
+
22
+ gem 'radius-template'
23
+
24
+ And then execute:
25
+
26
+ $ bundle
27
+
28
+ Or install it yourself as:
29
+
30
+ $ gem install radius-template
31
+
32
+ ## Usage
33
+
34
+
35
+ ### Simple Example
36
+
37
+ The first argument is a namespace(tag_prefix) for your template
38
+
39
+ ``` ruby
40
+ parser = Radius::Template.new("template")
41
+
42
+ parser.define_tag 'hello' do |tag|
43
+ who = tag['who']
44
+ "Hello #{who}"
45
+ end
46
+
47
+ parser.parse("<template:hello who="wrold" />") # => "Hello world"
48
+ parser.parse("<template:hello who="Jack" />") # => "Hello Jack"
49
+ ```
50
+
51
+ ### Passing Variables
52
+
53
+ In second argument you can pass variables to your template
54
+
55
+
56
+ ``` ruby
57
+ parser = Radius::Template.new("template", {
58
+ hello: "world",
59
+ number: 2.00
60
+ })
61
+
62
+ parser.parse("<template:hello />") # => "world"
63
+ parser.parse("<template:number />") # => "2.00"
64
+ ```
65
+
66
+ ### ActiveRecord Objects, Mongoid or any class kind...
67
+
68
+ The radius radius_attr_accessible tells what attributes will be accessible in the template.
69
+
70
+
71
+ ``` ruby
72
+ class Post < ActiveRecord::Base
73
+ radius_attr_accessible :title, :body
74
+ end
75
+
76
+ parser = Radius::Template.new("blog", {
77
+ today_posts: Post.all,
78
+ top_post: Post.top.first
79
+ })
80
+ ```
81
+
82
+ Passing a local variable named posts with Active Record Post array will create this tag `<blog:today_posts />` and it is an array with your posts. Inside this tag you will get this tag `<blog:post />`. The name of this tag is from singleton name of the model. Each attribute specified in _radius_attr_accessible_ is accessible from it (`<blog:post:title />`, `<blog:post:body />`).
83
+
84
+ ``` ruby
85
+ template = <<-TEMPLATE
86
+ <ul>
87
+ <blog:today_posts>
88
+ <li>
89
+ <blog:post /><br />
90
+ <blog:post:body>
91
+ </li>
92
+ </blog:today_posts>
93
+ </ul>
94
+ <p>
95
+ <blog:top_post>
96
+ See the top post - <blog:top_post:title />
97
+ <blog:top_post />
98
+ </p>
99
+ TEMPLATE
100
+
101
+ parser.parse(template) # =>
102
+ #
103
+ # <ul>
104
+ # <li>
105
+ # <a href="/posts/1">The title of the post</a><br />
106
+ # Hello World!
107
+ # </li>
108
+ # </ul>
109
+ # <p>
110
+ # See the top post - Hello World
111
+ # </p>
112
+ ```
113
+
114
+
115
+ ### Radius Drops
116
+
117
+ Drops acts like helpers for your template
118
+
119
+ Create "drops" directory in app folder and add it to load paths in environment.rb
120
+
121
+ ``` ruby
122
+ config.load_paths += %W( #{RAILS_ROOT}/app/drops )
123
+ ```
124
+
125
+
126
+ Next create a text_drop.rb in app/drops dir:
127
+
128
+
129
+ ``` ruby
130
+ class SimpleDrop < Radius::Drop
131
+ include ActionView::Helpers::TextHelper
132
+
133
+ register_tag "truncate" do |tag|
134
+ length = tag['length'] || 255
135
+ truncate(tag.expand, :length => length.to_i)
136
+ end
137
+
138
+ register_tag "variable_test" do |tag|
139
+ "#{@a_test_variable} from drop" #you can access variable that your passed into the template
140
+ end
141
+ end
142
+ ```
143
+
144
+ You must pass drops in the array as third argument
145
+
146
+
147
+
148
+ ``` ruby
149
+ parser = Radius::Template.new("app", {
150
+ a_test_variable: "Hello World"
151
+ }, [SimpleDrop])
152
+
153
+ template = <<-TEMPLATE
154
+ <app:truncate length="10">
155
+ Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
156
+ </app:truncate>
157
+
158
+ <app:variable_test />
159
+ TEMPLATE
160
+
161
+ parser.parse(template) # => Lorem ipsum dolor sit amet, co...
162
+ # Hello World from Drop
163
+
164
+ ```
165
+
166
+
167
+
168
+ ### Radius Drops + ActiveRecord (or Mongoid or any class kind...)
169
+
170
+
171
+ What if you have a photo url in your active record model and you want to create a image tag?
172
+
173
+
174
+ ``` ruby
175
+ class ProductsDrop < Radius::Drop
176
+
177
+ register_tag "product:photo" do |tag|
178
+ product = tag.locals.product rescue tag.missing!
179
+ if photo
180
+ attributes = tag.attributes.clone
181
+ image_tag(product.photo_url, attributes)
182
+ else
183
+ image_tag("/image/missing.png")
184
+ end
185
+ end
186
+
187
+ end
188
+
189
+ # <product:photo alt="product photo" /> # => <img src="/images/product/1.png" alt="product photo" />
190
+ ```
191
+
192
+
193
+
194
+
195
+ ## Contributing
196
+
197
+ 1. Fork it
198
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
199
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
200
+ 4. Push to the branch (`git push origin my-new-feature`)
201
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,6 @@
1
+ require 'radius'
2
+ require "radius/template/version"
3
+ require "radius/template/expose_to_radius"
4
+ require "radius/template/radius_delegation_open_struct_ext"
5
+ require "radius/template/radius_drop"
6
+ require "radius/template/radius_template"
@@ -0,0 +1,22 @@
1
+ module ExposeToRadius
2
+ module ClassMethods
3
+ def radius_attr_accessible(*attributes)
4
+ unless attributes.kind_of?(Array)
5
+ attributes = [attributes]
6
+ end
7
+
8
+ define_method("to_radius") do
9
+ attributes
10
+ end
11
+ end
12
+
13
+ end
14
+
15
+ def self.included(receiver)
16
+ receiver.extend ClassMethods
17
+ end
18
+ end
19
+
20
+ if Object.const_defined?("ActiveRecord")
21
+ ActiveRecord::Base.send(:include, ExposeToRadius)
22
+ end
@@ -0,0 +1,24 @@
1
+ module Radius
2
+ module DelegatingOpenStructExt
3
+
4
+ def set(key, value)
5
+ unless object.nil?
6
+ @object.set(key, value)
7
+ end
8
+ @hash[key.to_sym] = value
9
+ end
10
+
11
+ def get(key)
12
+ val = @hash[key.to_sym]
13
+
14
+ if val.nil? && !object.nil?
15
+ @object.get(key)
16
+ else
17
+ val
18
+ end
19
+ end
20
+
21
+ end
22
+ end
23
+
24
+ Radius::DelegatingOpenStruct.send(:include, Radius::DelegatingOpenStructExt)
@@ -0,0 +1,71 @@
1
+ module Radius
2
+ module Taggable
3
+ def self.included(base)
4
+ base.extend(ClassMethods)
5
+ base.module_eval do
6
+ protected
7
+ def params
8
+ @params ||= request.parameters unless request.nil?
9
+ end
10
+
11
+ def request_uri
12
+ @request_url ||= request.request_uri unless request.nil?
13
+ end
14
+ end
15
+ end
16
+
17
+ def render_tag(name, tag_binding)
18
+ send "tag:#{name}", tag_binding
19
+ end
20
+
21
+ def return_array(name, tag_binding)
22
+ send "array:#{name}", tag_binding
23
+ end
24
+
25
+ def return_object(name, tag_binding)
26
+ send "object:#{name}", tag_binding
27
+ end
28
+
29
+ def tags
30
+ self.methods.grep(/^tag:/).map { |name| name[4..-1] }.sort
31
+ end
32
+
33
+ def arrays
34
+ self.methods.grep(/^array:/).map { |name| name[6..-1] }.sort
35
+ end
36
+
37
+ def objects
38
+ self.methods.grep(/^object:/).map { |name| name[7..-1] }.sort
39
+ end
40
+
41
+ module ClassMethods
42
+ def register_tag(syntax, &block)
43
+ define_method("tag:#{syntax}", &block)
44
+ end
45
+
46
+ def register_array(key, &block)
47
+ key = key.gsub("_","-")
48
+ define_method("array:#{key}", &block)
49
+ end
50
+
51
+ def register_object(key, &block)
52
+ key = key.gsub("_","-")
53
+ define_method("object:#{key}", &block)
54
+ end
55
+ end
56
+ end
57
+
58
+ class Drop
59
+ include Radius::Taggable
60
+ include ActionView::Helpers::TagHelper
61
+ include ActionView::Helpers::AssetTagHelper
62
+
63
+ attr_accessor :template
64
+
65
+ def locals=(locals={})
66
+ locals.each do |key, value|
67
+ self.instance_variable_set(:"@#{key}", value)
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,172 @@
1
+ module Radius
2
+ class Template < Context
3
+ include ActionView::Helpers::TagHelper
4
+
5
+ def initialize(tag_prefix,locals = {}, drops = [])
6
+ super()
7
+ @tag_prefix = tag_prefix
8
+ @drops = {}
9
+ @locals = {}
10
+
11
+ self.locals = locals
12
+ self.drops = drops
13
+ end
14
+
15
+ def drops=(drops=[])
16
+ template = self
17
+
18
+ drops.each do |raw_drop_class|
19
+ drop_name = raw_drop_class.name
20
+ unless @drops.key?(drop_name)
21
+ @drops[drop_name] = raw_drop_class.new
22
+ @drops[drop_name].template = template
23
+ @drops[drop_name].tags.each do |name|
24
+ define_tag(name) { |tag_binding| @drops[drop_name].render_tag(name, tag_binding) }
25
+ end
26
+ @drops[drop_name].arrays.each do |name|
27
+ define_tag(name) do |tag_binding|
28
+ array = @drops[drop_name].return_array(name, tag_binding)
29
+ template.build_attributes_for_array(name, array)
30
+
31
+ main_klass = array.first.class.name
32
+ main_klass = main_klass.tableize.singularize unless main_klass.nil?
33
+ content = ''
34
+
35
+ array.each do |element|
36
+ tag_binding.locals.set(main_klass, element)
37
+ content << tag_binding.expand
38
+ end
39
+
40
+ content
41
+ end
42
+ end
43
+
44
+ @drops[drop_name].objects.each do |name|
45
+ define_tag(name) do |tag_binding|
46
+ object = @drops[drop_name].return_object(name, tag_binding)
47
+ if (object.kind_of?(String) || !object.respond_to?(:to_radius))
48
+ object
49
+ else
50
+ template.build_attributes_for_object(name, object)
51
+ wrap_inside_link(object, tag_binding)
52
+ end
53
+ end
54
+ end
55
+ end
56
+
57
+ @drops[drop_name].locals = @locals
58
+ end
59
+ end
60
+
61
+ def locals=(locals={})
62
+ @locals.merge!(locals)
63
+ @locals.each do |key, value|
64
+ if value.respond_to?(:to_radius)
65
+ build_object_tag(key.to_s, value)
66
+ elsif value.respond_to?(:each)
67
+ build_enum_tags(key,value)
68
+ else
69
+ define_tag key.to_s, :for => value
70
+ end
71
+ end
72
+
73
+ @drops.each { |name, drop| drop.locals = @locals }
74
+ end
75
+
76
+ def wrap_inside_link(main_obj, tag)
77
+ main_klass = main_obj.class.name.tableize.singularize
78
+ attributes = tag.attributes.clone
79
+ content = tag.expand
80
+
81
+ if content.nil? || content.empty?
82
+ content = main_obj.send(main_obj.to_radius.first)
83
+ linked = true
84
+ end
85
+
86
+ content
87
+ end
88
+ end
89
+
90
+ def build_object_tag(key, main_obj)
91
+ main_klass = main_obj.class.name.tableize.singularize
92
+ define_tag(key) do |tag|
93
+ tag.locals.set(main_klass, main_obj)
94
+ wrap_inside_link(main_obj, tag)
95
+ end
96
+
97
+ build_attributes_for_object(key, main_obj)
98
+ end
99
+
100
+ def build_attributes_for_object(key, object)
101
+ return unless object.respond_to?(:to_radius)
102
+ main_klass = object.class.name.tableize.singularize
103
+ object.to_radius.each do |attribute|
104
+ value = object.send(attribute)
105
+ if value.respond_to?(:to_radius)
106
+ build_object_tag(attribute.to_s, value)
107
+ elsif value.respond_to?(:each)
108
+ build_enum_tags(attribute,value)
109
+ else
110
+ define_tag ("#{key}:#{attribute.to_s}") do |tag|
111
+ obj = tag.locals.get(main_klass).send(attribute) rescue tag.missing!
112
+ end
113
+
114
+ end
115
+
116
+
117
+ end
118
+ end
119
+
120
+ def build_enum_tags(key, array=[])
121
+ key = key.to_s.tableize
122
+
123
+ main_klass = array.first.class.name
124
+ main_klass = main_klass.tableize.singularize unless main_klass.nil?
125
+
126
+ define_tag(key) do |tag|
127
+ content = ''
128
+
129
+ array.each do |element|
130
+ tag.locals.set(main_klass, element)
131
+ content << tag.expand
132
+ end
133
+
134
+ content
135
+ end
136
+
137
+ return if array.size == 0 || !array.first.respond_to?(:to_radius)
138
+
139
+ build_attributes_for_array(key, array)
140
+ end
141
+
142
+ def build_attributes_for_array(key, array=[])
143
+ main_klass = array.first.class.name
144
+ main_klass = main_klass.tableize.singularize unless main_klass.nil?
145
+
146
+ define_tag("#{key}:#{main_klass}") do |tag|
147
+ obj = tag.locals.get(main_klass) rescue tag.missing!
148
+ wrap_inside_link(obj, tag)
149
+ end
150
+
151
+ build_attributes_for_object(key, array.first)
152
+ end
153
+
154
+ def tag_missing(tag, attr, &block)
155
+ "<strong>ERROR: </strong> undefined tag: #{tag} attributes: #{attr.inspect}"
156
+ end
157
+
158
+ def locals
159
+ @locals
160
+ end
161
+
162
+ def parser
163
+ @parser ||= Radius::Parser.new(self, :tag_prefix => @tag_prefix)
164
+ @parser
165
+ end
166
+
167
+ def parse(input)
168
+ parser.parse(input)
169
+ end
170
+
171
+ end
172
+ end
@@ -0,0 +1,3 @@
1
+ module RadiusTemplate
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'radius/template/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "radius-template"
8
+ spec.version = RadiusTemplate::VERSION
9
+ spec.authors = ["Fourmach", 'José Galisteo Ruiz']
10
+ spec.email = ["ceritium@gmail.com"]
11
+ spec.description = %q{Simple Ruby on Rails wrapper around radius gem}
12
+ spec.summary = %q{Simple Ruby on Rails wrapper around radius gem}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: radius-template
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Fourmach
9
+ - José Galisteo Ruiz
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2013-05-09 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: bundler
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: '1.3'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ~>
29
+ - !ruby/object:Gem::Version
30
+ version: '1.3'
31
+ - !ruby/object:Gem::Dependency
32
+ name: rake
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ type: :development
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ description: Simple Ruby on Rails wrapper around radius gem
48
+ email:
49
+ - ceritium@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - Gemfile
56
+ - LICENSE.txt
57
+ - README.md
58
+ - Rakefile
59
+ - lib/radius/template.rb
60
+ - lib/radius/template/expose_to_radius.rb
61
+ - lib/radius/template/radius_delegation_open_struct_ext.rb
62
+ - lib/radius/template/radius_drop.rb
63
+ - lib/radius/template/radius_template.rb
64
+ - lib/radius/template/version.rb
65
+ - radius-template.gemspec
66
+ homepage: ''
67
+ licenses:
68
+ - MIT
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ segments:
80
+ - 0
81
+ hash: -4468472905617232333
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ segments:
89
+ - 0
90
+ hash: -4468472905617232333
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 1.8.25
94
+ signing_key:
95
+ specification_version: 3
96
+ summary: Simple Ruby on Rails wrapper around radius gem
97
+ test_files: []