liquify 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -3,3 +3,4 @@ pkg/*
3
3
  .bundle
4
4
  *.swp
5
5
  .rvmrc
6
+ spec/watchr.script
@@ -0,0 +1,73 @@
1
+ module Liquify
2
+ # Creating a block with Liquify is as simple as creating a Ruby
3
+ # class.
4
+ #
5
+ # class FooBlock < Liquify::Block
6
+ # def bar(params)
7
+ # ...
8
+ # end
9
+ #
10
+ # def baz
11
+ # ...
12
+ # end
13
+ # end
14
+ #
15
+ # Liquify.setup do |config|
16
+ # config.register_tag :foo_block, FooBlock
17
+ # end
18
+ #
19
+ # {% foo_block as: 'f' %}
20
+ # {{ f.bar 'arg1', key: 'value' }}
21
+ # {{ f.baz }}
22
+ # {% endfoo_block %}
23
+ #
24
+ # If you need wrapper content override the invoke method
25
+ # and call yield within it.
26
+ #
27
+ class Block < Liquid::Block
28
+ def initialize(tag_name, markup, tokens) # :nodoc:
29
+ @tokens = tokens.dup
30
+ super(tag_name, markup, tokens)
31
+ end
32
+
33
+ def render(context) # :nodoc:
34
+ params = Liquify::Parameter.new(@markup, context)
35
+ options = params.dup.extract_options!
36
+ drop_tokens = @tokens.grep /\{\{\s*#{options['as']}\..*\}\}/
37
+
38
+ context[options['as']] = Liquify::Block::Drop.new(self, drop_tokens, context)
39
+
40
+ args = []
41
+ args << params if method(:invoke).arity == 1
42
+
43
+ context.stack do
44
+ invoke(*args) do
45
+ render_all(@nodelist, context).join
46
+ end
47
+ end
48
+ end
49
+
50
+ class Drop < Liquid::Drop
51
+ def initialize(obj, tokens, context) # :nodoc:
52
+ @obj = obj
53
+ @params = {}
54
+
55
+ tokens.each do |t|
56
+ match, key, value = t.match(/\{\{\s*f\.(\w+)(.*)\}\}/).to_a
57
+
58
+ @params[key] ||= []
59
+ @params[key] << Liquify::Parameter.new(value, context)
60
+ end
61
+ end
62
+
63
+ def invoke_drop(key) # :nodoc:
64
+ allowed_methods = @obj.public_methods(false) - [:invoke]
65
+ return unless allowed_methods.include?(key.to_sym)
66
+
67
+ @obj.method(key).arity == 1 ? @obj.send(key, @params[key].pop) : @obj.send(key)
68
+ end
69
+
70
+ alias :[] :invoke_drop
71
+ end
72
+ end
73
+ end
@@ -1,3 +1,3 @@
1
1
  module Liquify
2
- VERSION = "0.2.2"
2
+ VERSION = "0.2.3"
3
3
  end
data/lib/liquify.rb CHANGED
@@ -3,6 +3,7 @@ require 'liquify/methods'
3
3
  require 'liquify/parameter'
4
4
  require 'liquify/drop'
5
5
  require 'liquify/tag'
6
+ require 'liquify/block'
6
7
 
7
8
  if defined?(Rails) && Rails.version >= '3'
8
9
  require File.expand_path(File.dirname(__FILE__) + '/rails/active_record_hook.rb')
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ describe FooBlock do
4
+ before(:each) do
5
+ Liquify.setup do |c|
6
+ c.register_tag :foo_block, FooBlock
7
+ end
8
+
9
+ @content = <<-STR
10
+ {% foo_block as: 'f' %}
11
+ <p>{{ f.name 'foo' }}</p>
12
+ <p>{{ f.name }}</p> <!-- intentionally blank -->
13
+ <p>{{ f.name 'bar' }}</p>
14
+ <p>{{ f.email }}</p>
15
+ <p>{{ f.foo with: 'foo' }}</p>
16
+ {% endfoo_block %}
17
+ STR
18
+ @output = Liquify.invoke(@content)
19
+ end
20
+
21
+ it 'has wrapper html' do
22
+ @output.should =~ /<div id="block-wrapper">.*<\/div>/m
23
+ end
24
+
25
+ it 'has an email field and does not take arguments' do
26
+ @output.should =~ /<p>foo\.bar@baz\.com<\/p>/m
27
+ end
28
+
29
+ it 'has a name field of "foo"' do
30
+ @output.should =~ /<p>foo<\/p>/
31
+ end
32
+
33
+ it 'has a second name field of "bar"' do
34
+ @output.should =~ /<p>bar<\/p>/
35
+ end
36
+
37
+ it 'has a span with the word "foo"' do
38
+ @output.should =~ /<p><span>foo<\/span><\/p>/
39
+ end
40
+ end
@@ -0,0 +1,18 @@
1
+ class FooBlock < Liquify::Block
2
+ def invoke
3
+ "<div id=\"block-wrapper\">#{yield}</div>"
4
+ end
5
+
6
+ def name(params)
7
+ "<p>#{params.first}</p>"
8
+ end
9
+
10
+ def email
11
+ "<p>foo.bar@baz.com</p>"
12
+ end
13
+
14
+ def foo(params)
15
+ options = params.extract_options!
16
+ "<span>#{options['with']}</span>"
17
+ end
18
+ 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.2
5
+ version: 0.2.3
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-31 00:00:00 -04:00
13
+ date: 2011-04-10 00:00:00 -04:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -54,6 +54,7 @@ files:
54
54
  - lib/generators/liquify/install_generator.rb
55
55
  - lib/generators/liquify/templates/liquify.rb
56
56
  - lib/liquify.rb
57
+ - lib/liquify/block.rb
57
58
  - lib/liquify/drop.rb
58
59
  - lib/liquify/methods.rb
59
60
  - lib/liquify/parameter.rb
@@ -63,6 +64,7 @@ files:
63
64
  - liquify.gemspec
64
65
  - spec/liquify/active_record_hook_spec.rb
65
66
  - spec/liquify/drop_spec.rb
67
+ - spec/liquify/foo_block_spec.rb
66
68
  - spec/liquify/liquify_methods_spec.rb
67
69
  - spec/liquify/liquify_methods_with_lambdas_spec.rb
68
70
  - spec/liquify/parameter_spec.rb
@@ -74,6 +76,7 @@ files:
74
76
  - spec/support/liquify/bar_drop.rb
75
77
  - spec/support/liquify/baz_drop.rb
76
78
  - spec/support/liquify/cux_drop.rb
79
+ - spec/support/liquify/foo_block.rb
77
80
  - spec/support/liquify/foo_tag.rb
78
81
  - spec/support/rails/foo.rb
79
82
  - spec/v0.1/liquify/active_record_hook_spec.rb
@@ -103,13 +106,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
103
106
  requirements: []
104
107
 
105
108
  rubyforge_project: liquify
106
- rubygems_version: 1.5.2
109
+ rubygems_version: 1.6.2
107
110
  signing_key:
108
111
  specification_version: 3
109
112
  summary: A wrapper for Liquid Markup to make it easier to use
110
113
  test_files:
111
114
  - spec/liquify/active_record_hook_spec.rb
112
115
  - spec/liquify/drop_spec.rb
116
+ - spec/liquify/foo_block_spec.rb
113
117
  - spec/liquify/liquify_methods_spec.rb
114
118
  - spec/liquify/liquify_methods_with_lambdas_spec.rb
115
119
  - spec/liquify/parameter_spec.rb
@@ -121,6 +125,7 @@ test_files:
121
125
  - spec/support/liquify/bar_drop.rb
122
126
  - spec/support/liquify/baz_drop.rb
123
127
  - spec/support/liquify/cux_drop.rb
128
+ - spec/support/liquify/foo_block.rb
124
129
  - spec/support/liquify/foo_tag.rb
125
130
  - spec/support/rails/foo.rb
126
131
  - spec/v0.1/liquify/active_record_hook_spec.rb