nyanko 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/nyanko/config.rb +1 -0
- data/lib/nyanko/controller.rb +0 -4
- data/lib/nyanko/function.rb +16 -0
- data/lib/nyanko/invoker.rb +19 -15
- data/lib/nyanko/invoker/function_finder.rb +1 -1
- data/lib/nyanko/railtie.rb +2 -2
- data/lib/nyanko/unit.rb +5 -2
- data/lib/nyanko/version.rb +1 -1
- data/spec/fixtures/units/example_unit/example_unit.rb +4 -0
- data/spec/nyanko/invoker_spec.rb +25 -0
- metadata +1 -1
data/lib/nyanko/config.rb
CHANGED
data/lib/nyanko/controller.rb
CHANGED
data/lib/nyanko/function.rb
CHANGED
|
@@ -26,6 +26,22 @@ module Nyanko
|
|
|
26
26
|
end
|
|
27
27
|
end
|
|
28
28
|
|
|
29
|
+
def css_classes
|
|
30
|
+
if Config.compatible_css_class
|
|
31
|
+
%W[
|
|
32
|
+
extension
|
|
33
|
+
ext_#{unit.name.underscore}
|
|
34
|
+
ext_#{unit.name.underscore}-#{label}
|
|
35
|
+
]
|
|
36
|
+
else
|
|
37
|
+
%W[
|
|
38
|
+
unit
|
|
39
|
+
unit__#{unit.name.underscore}
|
|
40
|
+
unit__#{unit.name.underscore}__#{label}
|
|
41
|
+
]
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
29
45
|
private
|
|
30
46
|
|
|
31
47
|
def with_unit_stack(context)
|
data/lib/nyanko/invoker.rb
CHANGED
|
@@ -5,6 +5,7 @@ module Nyanko
|
|
|
5
5
|
module Invoker
|
|
6
6
|
def invoke(*args, &block)
|
|
7
7
|
options = Options.new(*args)
|
|
8
|
+
defaults_stack << block
|
|
8
9
|
unit_locals_stack << options.locals
|
|
9
10
|
function = FunctionFinder.find(self, options)
|
|
10
11
|
result = function.invoke(self, options.invoke_options)
|
|
@@ -12,15 +13,9 @@ module Nyanko
|
|
|
12
13
|
result
|
|
13
14
|
rescue Exception => exception
|
|
14
15
|
ExceptionHandler.handle(exception)
|
|
15
|
-
|
|
16
|
-
when !block
|
|
17
|
-
nil
|
|
18
|
-
when view?
|
|
19
|
-
capture(&block)
|
|
20
|
-
else
|
|
21
|
-
instance_exec(&block)
|
|
22
|
-
end
|
|
16
|
+
run_default
|
|
23
17
|
ensure
|
|
18
|
+
defaults_stack.pop
|
|
24
19
|
unit_locals_stack.pop
|
|
25
20
|
end
|
|
26
21
|
|
|
@@ -32,6 +27,16 @@ module Nyanko
|
|
|
32
27
|
is_a?(ActionView::Base)
|
|
33
28
|
end
|
|
34
29
|
|
|
30
|
+
def run_default
|
|
31
|
+
if block = defaults_stack.last
|
|
32
|
+
if view?
|
|
33
|
+
capture(&block)
|
|
34
|
+
else
|
|
35
|
+
instance_exec(&block)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
35
40
|
private
|
|
36
41
|
|
|
37
42
|
# Search shared method or locals variable
|
|
@@ -45,23 +50,22 @@ module Nyanko
|
|
|
45
50
|
end
|
|
46
51
|
end
|
|
47
52
|
|
|
53
|
+
def defaults_stack
|
|
54
|
+
@defaults_stack ||= []
|
|
55
|
+
end
|
|
56
|
+
|
|
48
57
|
def unit_locals_stack
|
|
49
58
|
@unit_locals_stack ||= []
|
|
50
59
|
end
|
|
51
60
|
|
|
52
61
|
def surround_with_html_tag(str, function, options)
|
|
53
|
-
classes = %W[
|
|
54
|
-
unit
|
|
55
|
-
unit__#{function.unit.name.underscore}
|
|
56
|
-
unit__#{function.unit.name.underscore}__#{function.label}
|
|
57
|
-
]
|
|
58
62
|
case options.type
|
|
59
63
|
when :plain
|
|
60
64
|
str
|
|
61
65
|
when :inline
|
|
62
|
-
content_tag(:span, str, :class =>
|
|
66
|
+
content_tag(:span, str, :class => function.css_classes)
|
|
63
67
|
else
|
|
64
|
-
content_tag(:div, str, :class =>
|
|
68
|
+
content_tag(:div, str, :class => function.css_classes)
|
|
65
69
|
end
|
|
66
70
|
end
|
|
67
71
|
end
|
|
@@ -17,7 +17,7 @@ module Nyanko
|
|
|
17
17
|
@options[:functions].find do |unit_name, label|
|
|
18
18
|
unit = find_unit(unit_name)
|
|
19
19
|
identifier = @options[:as] || @context.class
|
|
20
|
-
next unless unit.active
|
|
20
|
+
next unless unit.try(:active?, @context, @options[:active_if_options])
|
|
21
21
|
function = unit.find_function(identifier, label)
|
|
22
22
|
break function if function
|
|
23
23
|
end
|
data/lib/nyanko/railtie.rb
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
module Nyanko
|
|
2
2
|
class Railtie < Rails::Railtie
|
|
3
3
|
initializer "nyanko" do |app|
|
|
4
|
-
::ActionView::Base.send(:include,
|
|
5
|
-
::ActionController::Base.send(:include, Controller)
|
|
4
|
+
::ActionView::Base.send(:include, Helper, Invoker, UnitProxyProvider)
|
|
5
|
+
::ActionController::Base.send(:include, Controller, Invoker, UnitProxyProvider)
|
|
6
6
|
::ActiveRecord::Base.send(:include, UnitProxyProvider)
|
|
7
7
|
::ActiveRecord::Relation.send(:include, UnitProxyProvider)
|
|
8
8
|
::ActiveRecord::Associations::CollectionAssociation.send(:include, UnitProxyProvider)
|
data/lib/nyanko/unit.rb
CHANGED
|
@@ -12,8 +12,6 @@ module Nyanko
|
|
|
12
12
|
module ClassMethods
|
|
13
13
|
attr_accessor :current_scope
|
|
14
14
|
|
|
15
|
-
delegate :active?, :to => :@active_if
|
|
16
|
-
|
|
17
15
|
def scope(identifier)
|
|
18
16
|
self.current_scope = ScopeFinder.find(identifier)
|
|
19
17
|
scopes[current_scope] ||= {}
|
|
@@ -25,6 +23,7 @@ module Nyanko
|
|
|
25
23
|
def function(label, &block)
|
|
26
24
|
functions[label] = Function.new(self, label, &block)
|
|
27
25
|
end
|
|
26
|
+
alias_method :callback, :function
|
|
28
27
|
|
|
29
28
|
def shared(label, &block)
|
|
30
29
|
shared_methods[label] = block
|
|
@@ -42,6 +41,10 @@ module Nyanko
|
|
|
42
41
|
@active_if = ActiveIf.new(*conditions, &block)
|
|
43
42
|
end
|
|
44
43
|
|
|
44
|
+
def active?(context, options = {})
|
|
45
|
+
@active_if.active?(context, options.merge(:unit => self))
|
|
46
|
+
end
|
|
47
|
+
|
|
45
48
|
def any(*labels)
|
|
46
49
|
ActiveIf::Any.new(*labels)
|
|
47
50
|
end
|
data/lib/nyanko/version.rb
CHANGED
data/spec/nyanko/invoker_spec.rb
CHANGED
|
@@ -50,6 +50,19 @@ module Nyanko
|
|
|
50
50
|
end
|
|
51
51
|
end
|
|
52
52
|
|
|
53
|
+
context "when compatible css class option is specified" do
|
|
54
|
+
around do |example|
|
|
55
|
+
origin, Config.compatible_css_class = Config.compatible_css_class, true
|
|
56
|
+
example.run
|
|
57
|
+
Config.compatible_css_class = origin
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
it "invokes and returns result surrounded by div" do
|
|
61
|
+
view.invoke(:example_unit, :test).should ==
|
|
62
|
+
'<div class="extension ext_example_unit ext_example_unit-test">test</div>'
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
53
66
|
context "when type is :plain" do
|
|
54
67
|
it "invokes defined function for current context and return result" do
|
|
55
68
|
view.invoke(:example_unit, :test, :type => :plain).should == "test"
|
|
@@ -69,6 +82,18 @@ module Nyanko
|
|
|
69
82
|
end
|
|
70
83
|
end
|
|
71
84
|
|
|
85
|
+
context "when run_default is called in function" do
|
|
86
|
+
it "invokes given block as a fallback" do
|
|
87
|
+
controller.invoke(:example_unit, :default) { "default" }.should == "default"
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
context "when run_default is called but no block given" do
|
|
92
|
+
it "invokes given block as a fallback" do
|
|
93
|
+
controller.invoke(:example_unit, :default).should == nil
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
|
|
72
97
|
context "when non-existent unit is specified" do
|
|
73
98
|
it "does nothing" do
|
|
74
99
|
view.invoke(:non_existent_unit, :test, :type => :plain).should == nil
|