liquify 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -71
- data/lib/liquify/drop.rb +104 -1
- data/lib/liquify/methods.rb +5 -30
- data/lib/liquify/version.rb +1 -1
- data/spec/liquify/liquify_methods_spec.rb +34 -0
- data/spec/liquify/liquify_methods_with_lambdas_spec.rb +33 -0
- data/spec/support/liquify/baz_drop.rb +24 -0
- data/spec/support/liquify/cux_drop.rb +24 -0
- metadata +10 -2
data/Gemfile.lock
CHANGED
@@ -1,72 +1,14 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
liquify (0.2)
|
4
|
+
liquify (0.2.1)
|
5
5
|
liquid (>= 2.2.2)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: http://rubygems.org/
|
9
9
|
specs:
|
10
|
-
abstract (1.0.0)
|
11
|
-
actionmailer (3.0.3)
|
12
|
-
actionpack (= 3.0.3)
|
13
|
-
mail (~> 2.2.9)
|
14
|
-
actionpack (3.0.3)
|
15
|
-
activemodel (= 3.0.3)
|
16
|
-
activesupport (= 3.0.3)
|
17
|
-
builder (~> 2.1.2)
|
18
|
-
erubis (~> 2.6.6)
|
19
|
-
i18n (~> 0.4)
|
20
|
-
rack (~> 1.2.1)
|
21
|
-
rack-mount (~> 0.6.13)
|
22
|
-
rack-test (~> 0.5.6)
|
23
|
-
tzinfo (~> 0.3.23)
|
24
|
-
activemodel (3.0.3)
|
25
|
-
activesupport (= 3.0.3)
|
26
|
-
builder (~> 2.1.2)
|
27
|
-
i18n (~> 0.4)
|
28
|
-
activerecord (3.0.3)
|
29
|
-
activemodel (= 3.0.3)
|
30
|
-
activesupport (= 3.0.3)
|
31
|
-
arel (~> 2.0.2)
|
32
|
-
tzinfo (~> 0.3.23)
|
33
|
-
activeresource (3.0.3)
|
34
|
-
activemodel (= 3.0.3)
|
35
|
-
activesupport (= 3.0.3)
|
36
|
-
activesupport (3.0.3)
|
37
|
-
arel (2.0.6)
|
38
|
-
builder (2.1.2)
|
39
10
|
diff-lcs (1.1.2)
|
40
|
-
erubis (2.6.6)
|
41
|
-
abstract (>= 1.0.0)
|
42
|
-
i18n (0.5.0)
|
43
11
|
liquid (2.2.2)
|
44
|
-
mail (2.2.13)
|
45
|
-
activesupport (>= 2.3.6)
|
46
|
-
i18n (>= 0.4.0)
|
47
|
-
mime-types (~> 1.16)
|
48
|
-
treetop (~> 1.4.8)
|
49
|
-
mime-types (1.16)
|
50
|
-
polyglot (0.3.1)
|
51
|
-
rack (1.2.1)
|
52
|
-
rack-mount (0.6.13)
|
53
|
-
rack (>= 1.0.0)
|
54
|
-
rack-test (0.5.6)
|
55
|
-
rack (>= 1.0)
|
56
|
-
rails (3.0.3)
|
57
|
-
actionmailer (= 3.0.3)
|
58
|
-
actionpack (= 3.0.3)
|
59
|
-
activerecord (= 3.0.3)
|
60
|
-
activeresource (= 3.0.3)
|
61
|
-
activesupport (= 3.0.3)
|
62
|
-
bundler (~> 1.0)
|
63
|
-
railties (= 3.0.3)
|
64
|
-
railties (3.0.3)
|
65
|
-
actionpack (= 3.0.3)
|
66
|
-
activesupport (= 3.0.3)
|
67
|
-
rake (>= 0.8.7)
|
68
|
-
thor (~> 0.14.4)
|
69
|
-
rake (0.8.7)
|
70
12
|
rspec (2.3.0)
|
71
13
|
rspec-core (~> 2.3.0)
|
72
14
|
rspec-expectations (~> 2.3.0)
|
@@ -75,22 +17,10 @@ GEM
|
|
75
17
|
rspec-expectations (2.3.0)
|
76
18
|
diff-lcs (~> 1.1.2)
|
77
19
|
rspec-mocks (2.3.0)
|
78
|
-
rspec-rails (2.3.1)
|
79
|
-
actionpack (~> 3.0)
|
80
|
-
activesupport (~> 3.0)
|
81
|
-
railties (~> 3.0)
|
82
|
-
rspec (~> 2.3.0)
|
83
|
-
thor (0.14.6)
|
84
|
-
treetop (1.4.9)
|
85
|
-
polyglot (>= 0.3.1)
|
86
|
-
tzinfo (0.3.23)
|
87
20
|
|
88
21
|
PLATFORMS
|
89
22
|
ruby
|
90
23
|
|
91
24
|
DEPENDENCIES
|
92
|
-
liquid (>= 2.2.2)
|
93
25
|
liquify!
|
94
|
-
rails (~> 3.0.1)
|
95
26
|
rspec (~> 2.3.0)
|
96
|
-
rspec-rails (~> 2.3.0)
|
data/lib/liquify/drop.rb
CHANGED
@@ -1,5 +1,108 @@
|
|
1
1
|
module Liquify
|
2
2
|
class Drop < Liquid::Drop
|
3
|
-
|
3
|
+
class << self
|
4
|
+
def build(obj, values)
|
5
|
+
obj.class.class_eval <<-STR
|
6
|
+
class DynamicDrop < Liquify::Drop
|
7
|
+
def initialize(obj, values)
|
8
|
+
@object = obj
|
9
|
+
@blocks = {}
|
10
|
+
|
11
|
+
values.each do |value|
|
12
|
+
if value.is_a? Hash
|
13
|
+
value.each { |key, val| @blocks[key] = val }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
STR
|
19
|
+
|
20
|
+
obj.class::DynamicDrop.class_eval do
|
21
|
+
values.each do |item|
|
22
|
+
if item.is_a? Hash
|
23
|
+
item.each do |key, value|
|
24
|
+
define_method key do
|
25
|
+
block = @blocks[key]
|
26
|
+
block.arity.zero? ? block.call : block.call(@object)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
else
|
30
|
+
define_method item do
|
31
|
+
@object.send item
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
obj.class::DynamicDrop.new(obj, values)
|
38
|
+
end
|
39
|
+
end
|
4
40
|
end
|
5
41
|
end
|
42
|
+
=begin
|
43
|
+
private
|
44
|
+
def build_hash
|
45
|
+
@values.each do |key, value|
|
46
|
+
self.class.class_eval do
|
47
|
+
define_method(key) { @values[key] }
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def build_default
|
53
|
+
@values.each do |item|
|
54
|
+
if item.is_a? Hash
|
55
|
+
item.each do |key, value|
|
56
|
+
self.class.class_eval do
|
57
|
+
define_method key do
|
58
|
+
value.arity.zero? ? value.call : value.call(@obj)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
else
|
63
|
+
self.class.class_eval do
|
64
|
+
define_method(item) { @obj.send(item) }
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
=end
|
70
|
+
=begin
|
71
|
+
def build(obj, values, options={})
|
72
|
+
case options[:as]
|
73
|
+
when :hash
|
74
|
+
build_hash
|
75
|
+
else
|
76
|
+
build_default
|
77
|
+
end
|
78
|
+
self
|
79
|
+
end
|
80
|
+
|
81
|
+
private
|
82
|
+
def build_hash
|
83
|
+
@values.each do |key, value|
|
84
|
+
self.class.class_eval do
|
85
|
+
define_method(key) { @values[key] }
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def build_default
|
91
|
+
@values.each do |item|
|
92
|
+
if item.is_a? Hash
|
93
|
+
item.each do |key, value|
|
94
|
+
self.class.class_eval do
|
95
|
+
define_method key do
|
96
|
+
value.arity.zero? ? value.call : value.call(@obj)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
else
|
101
|
+
self.class.class_eval do
|
102
|
+
define_method(item) { @obj.send(item) }
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
=end
|
data/lib/liquify/methods.rb
CHANGED
@@ -12,40 +12,15 @@ module Liquify
|
|
12
12
|
#
|
13
13
|
#
|
14
14
|
def liquify_method(*args)
|
15
|
-
|
16
|
-
liquify_args ||= {}
|
17
|
-
|
18
|
-
args.each do |arg|
|
19
|
-
key, value = arg, :self
|
20
|
-
|
21
|
-
if Hash === arg
|
22
|
-
arg.each { |k, v| liquify_args[k.to_s] = v }
|
23
|
-
else
|
24
|
-
liquify_args[key.to_s] = value
|
25
|
-
end
|
26
|
-
end
|
27
|
-
instance_variable_set :@liquify_args, liquify_args
|
15
|
+
class_variable_set(:@@liquify_methods, args)
|
28
16
|
end
|
17
|
+
|
18
|
+
alias :liquify_methods :liquify_method
|
29
19
|
end
|
30
20
|
|
31
21
|
module InstanceMethods
|
32
|
-
|
33
|
-
|
34
|
-
liquify_args = self.class.instance_variable_get(:@liquify_args) || {}
|
35
|
-
liquify_output = instance_variable_get(:@liquify_output) || {}
|
36
|
-
|
37
|
-
if liquify_output.empty?
|
38
|
-
liquify_args.each do |key, value|
|
39
|
-
if value.respond_to?(:call)
|
40
|
-
liquify_output[key] = value.arity.zero? ? value.call : value.call(self)
|
41
|
-
else
|
42
|
-
liquify_output[key] = self.send(key)
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
liquify_output
|
48
|
-
end
|
22
|
+
def to_liquid
|
23
|
+
Liquify::Drop.build self, self.class.class_variable_get(:@@liquify_methods)
|
49
24
|
end
|
50
25
|
end
|
51
26
|
end
|
data/lib/liquify/version.rb
CHANGED
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Liquify::Methods do
|
4
|
+
before(:each) do
|
5
|
+
@baz_1 = Baz.new('baz -1')
|
6
|
+
@baz_2 = Baz.new('baz -2')
|
7
|
+
end
|
8
|
+
|
9
|
+
context 'when exposing a value as a lambda' do
|
10
|
+
it 'is scoped to that object instance' do
|
11
|
+
@baz_1.to_liquid['name']['value'].should == @baz_1.name
|
12
|
+
@baz_2.to_liquid['name']['value'].should == @baz_2.name
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
context 'when the template is rendered' do
|
17
|
+
it 'contains values for both baz_1 and baz_2' do
|
18
|
+
Liquify.setup do |c|
|
19
|
+
c.register_drop :baz, lambda { [@baz_1, @baz_2] }
|
20
|
+
end
|
21
|
+
|
22
|
+
content = <<-STR
|
23
|
+
{% for b in baz %}
|
24
|
+
<p>{{ b.name.value }}</p>
|
25
|
+
{% endfor %}
|
26
|
+
STR
|
27
|
+
|
28
|
+
output = Liquify.invoke(content)
|
29
|
+
|
30
|
+
output.should =~ /<p>#{@baz_1.name}<\/p>/
|
31
|
+
output.should =~ /<p>#{@baz_2.name}<\/p>/
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Liquify::Methods do
|
4
|
+
context 'when a hash with a lambda value is passed' do
|
5
|
+
before(:all) do
|
6
|
+
@cux_1 = Cux.new('John', 'Smith', {'age' => 25, 'gender' => 'M'})
|
7
|
+
@cux_2 = Cux.new('Jane', 'Doe', {'age' => 22, 'gender' => 'F'})
|
8
|
+
|
9
|
+
content = <<-STR
|
10
|
+
{% for item in items %}
|
11
|
+
<p>{{ item.full_name }}</p>
|
12
|
+
<p>{{ item.options.age }}</p>
|
13
|
+
{% endfor %}
|
14
|
+
STR
|
15
|
+
|
16
|
+
Liquify.setup do |c|
|
17
|
+
c.register_drop :items, lambda { [@cux_1, @cux_2] }
|
18
|
+
end
|
19
|
+
|
20
|
+
@output = Liquify.invoke(content)
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'returns values scoped to the first item' do
|
24
|
+
@output.should =~ /<p>#{@cux_1.first_name} #{@cux_1.last_name}<\/p>/
|
25
|
+
@output.should =~ /<p>#{@cux_1.options['age']}<\/p>/
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'returns values scoped to the second item' do
|
29
|
+
@output.should =~ /<p>#{@cux_2.first_name} #{@cux_2.last_name}<\/p>/
|
30
|
+
@output.should =~ /<p>#{@cux_2.options['age']}<\/p>/
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
class Baz
|
2
|
+
include Liquify::Methods
|
3
|
+
attr_reader :name
|
4
|
+
liquify_method :name => lambda { |b| NameDrop.new(b) }
|
5
|
+
|
6
|
+
def initialize(name)
|
7
|
+
@name = name
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class NameDrop < Liquify::Drop
|
12
|
+
def initialize(obj)
|
13
|
+
@obj = obj
|
14
|
+
@values = {'value' => @obj.name}
|
15
|
+
end
|
16
|
+
|
17
|
+
def [](key)
|
18
|
+
@values[key]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# liquify_method
|
23
|
+
# create drop
|
24
|
+
# create methods
|
@@ -0,0 +1,24 @@
|
|
1
|
+
class Cux
|
2
|
+
include Liquify::Methods
|
3
|
+
attr_reader :first_name, :last_name, :options
|
4
|
+
liquify_methods :full_name => lambda {|c| "#{c.first_name} #{c.last_name}"},
|
5
|
+
:options => lambda {|c| OptionDrop.new(c) }
|
6
|
+
|
7
|
+
def initialize(first_name, last_name, options)
|
8
|
+
@first_name = last_name
|
9
|
+
@last_name = first_name
|
10
|
+
@options = options
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class OptionDrop < Liquid::Drop
|
15
|
+
def initialize(cux)
|
16
|
+
@cux = cux
|
17
|
+
end
|
18
|
+
|
19
|
+
def invoke_drop(key)
|
20
|
+
@cux.options[key]
|
21
|
+
end
|
22
|
+
|
23
|
+
alias :[] :invoke_drop
|
24
|
+
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: liquify
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.2.
|
5
|
+
version: 0.2.2
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Dane Harrigan
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-03-
|
13
|
+
date: 2011-03-31 00:00:00 -04:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -63,6 +63,8 @@ files:
|
|
63
63
|
- liquify.gemspec
|
64
64
|
- spec/liquify/active_record_hook_spec.rb
|
65
65
|
- spec/liquify/drop_spec.rb
|
66
|
+
- spec/liquify/liquify_methods_spec.rb
|
67
|
+
- spec/liquify/liquify_methods_with_lambdas_spec.rb
|
66
68
|
- spec/liquify/parameter_spec.rb
|
67
69
|
- spec/liquify/tag_spec.rb
|
68
70
|
- spec/liquify_spec.rb
|
@@ -70,6 +72,8 @@ files:
|
|
70
72
|
- spec/support/liquid/foo_drop.rb
|
71
73
|
- spec/support/liquid/foo_filters.rb
|
72
74
|
- spec/support/liquify/bar_drop.rb
|
75
|
+
- spec/support/liquify/baz_drop.rb
|
76
|
+
- spec/support/liquify/cux_drop.rb
|
73
77
|
- spec/support/liquify/foo_tag.rb
|
74
78
|
- spec/support/rails/foo.rb
|
75
79
|
- spec/v0.1/liquify/active_record_hook_spec.rb
|
@@ -106,6 +110,8 @@ summary: A wrapper for Liquid Markup to make it easier to use
|
|
106
110
|
test_files:
|
107
111
|
- spec/liquify/active_record_hook_spec.rb
|
108
112
|
- spec/liquify/drop_spec.rb
|
113
|
+
- spec/liquify/liquify_methods_spec.rb
|
114
|
+
- spec/liquify/liquify_methods_with_lambdas_spec.rb
|
109
115
|
- spec/liquify/parameter_spec.rb
|
110
116
|
- spec/liquify/tag_spec.rb
|
111
117
|
- spec/liquify_spec.rb
|
@@ -113,6 +119,8 @@ test_files:
|
|
113
119
|
- spec/support/liquid/foo_drop.rb
|
114
120
|
- spec/support/liquid/foo_filters.rb
|
115
121
|
- spec/support/liquify/bar_drop.rb
|
122
|
+
- spec/support/liquify/baz_drop.rb
|
123
|
+
- spec/support/liquify/cux_drop.rb
|
116
124
|
- spec/support/liquify/foo_tag.rb
|
117
125
|
- spec/support/rails/foo.rb
|
118
126
|
- spec/v0.1/liquify/active_record_hook_spec.rb
|