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 CHANGED
@@ -4,6 +4,7 @@ module Nyanko
4
4
  attr_accessor(
5
5
  :backtrace_limit,
6
6
  :cache_units,
7
+ :compatible_css_class,
7
8
  :raise_error,
8
9
  :units_directory_path
9
10
  )
@@ -2,10 +2,6 @@ module Nyanko
2
2
  module Controller
3
3
  extend ActiveSupport::Concern
4
4
 
5
- included do
6
- include Invoker
7
- end
8
-
9
5
  module ClassMethods
10
6
  private
11
7
 
@@ -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)
@@ -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
- case
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 => classes)
66
+ content_tag(:span, str, :class => function.css_classes)
63
67
  else
64
- content_tag(:div, str, :class => classes)
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?(@context, @options[:active_if_options])
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
@@ -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, Invoker, Helper, UnitProxyProvider)
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
@@ -1,3 +1,3 @@
1
1
  module Nyanko
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -21,6 +21,10 @@ module ExampleUnit
21
21
  function(:alias) do
22
22
  "alias"
23
23
  end
24
+
25
+ function(:default) do
26
+ run_default
27
+ end
24
28
  end
25
29
 
26
30
  scope(:view) do
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nyanko
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: