nyanko 0.0.7 → 0.0.8
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/README.md +4 -3
- data/lib/generators/nyanko/unit/templates/unit.rb.erb +88 -0
- data/lib/generators/nyanko/unit/unit_generator.rb +45 -0
- data/lib/nyanko/config.rb +2 -0
- data/lib/nyanko/exception_handler.rb +2 -2
- data/lib/nyanko/function.rb +25 -5
- data/lib/nyanko/invoker.rb +64 -37
- data/lib/nyanko/invoker/function_finder.rb +14 -24
- data/lib/nyanko/invoker/options.rb +4 -9
- data/lib/nyanko/logger.rb +10 -3
- data/lib/nyanko/test.rb +38 -0
- data/lib/nyanko/unit.rb +10 -1
- data/lib/nyanko/unit_proxy_provider.rb +2 -0
- data/lib/nyanko/version.rb +1 -1
- data/spec/fixtures/units/example_unit/example_unit.rb +8 -0
- data/spec/fixtures/units/example_unit/views/_test.html.erb +1 -0
- data/spec/fixtures/units/insensitive_unit/insensitive_unit.rb +3 -0
- data/spec/fixtures/units/sensitive_unit/sensitive_unit.rb +4 -0
- data/spec/nyanko/exception_handler_spec.rb +26 -2
- data/spec/nyanko/function_spec.rb +6 -2
- data/spec/nyanko/invoker_spec.rb +12 -13
- data/spec/nyanko/test_spec.rb +28 -0
- data/spec/spec_helper.rb +2 -0
- metadata +13 -2
data/README.md
CHANGED
@@ -0,0 +1,88 @@
|
|
1
|
+
module <%= class_name %>
|
2
|
+
include Nyanko::Unit
|
3
|
+
|
4
|
+
# ## active_if
|
5
|
+
# This block is used to decide if this unit is active or not.
|
6
|
+
# `context` is the receiver object of `invoke`.
|
7
|
+
# `options` is passed via `invoke(:foo, :bar, :active_if_options => { ... })`.
|
8
|
+
# By default, this is set as `active_if { true }`.
|
9
|
+
#
|
10
|
+
# ```
|
11
|
+
# active_if do |context, options|
|
12
|
+
# true
|
13
|
+
# end
|
14
|
+
# ```
|
15
|
+
|
16
|
+
# ## raise_error
|
17
|
+
# `raise_error` is used to force an unit to raise up errors occured in invoking.
|
18
|
+
# You can force to raise up errors also by `Config.raise_error`.
|
19
|
+
#
|
20
|
+
# ```
|
21
|
+
# raise_error
|
22
|
+
# ```
|
23
|
+
|
24
|
+
# ## function
|
25
|
+
# In controller or view context, you can call functions defined by `function`
|
26
|
+
# via `invoke(:<%= file_name %>, :function_name)`.
|
27
|
+
#
|
28
|
+
# scope(:controller) do
|
29
|
+
# function(:function_name) do
|
30
|
+
# "Nyanko!"
|
31
|
+
# end
|
32
|
+
# end
|
33
|
+
|
34
|
+
# ## render
|
35
|
+
# In addition, the view path "<%= "#{directory}/views" %>" is added into view_paths.
|
36
|
+
# So you can render <%= "#{directory}/views/example.html.erb" %> in invoking.
|
37
|
+
#
|
38
|
+
# ```
|
39
|
+
# scope(:controller) do
|
40
|
+
# function(:function_name) do
|
41
|
+
# render "/example", :foo => "bar"
|
42
|
+
# end
|
43
|
+
# end
|
44
|
+
# ```
|
45
|
+
|
46
|
+
# ## models
|
47
|
+
# In models block, you can expand model features by `expand` method.
|
48
|
+
# The expanded methods are available via unit proxy like `User.unit.active`,
|
49
|
+
# and `User.find(params[:id]).unit.active?`, and so on.
|
50
|
+
#
|
51
|
+
# ```
|
52
|
+
# models do
|
53
|
+
# expand(:User) do
|
54
|
+
# scope :active, lambda { where(:deleted_at => nil) }
|
55
|
+
#
|
56
|
+
# def active?
|
57
|
+
# deleted_at.nil?
|
58
|
+
# end
|
59
|
+
# end
|
60
|
+
# end
|
61
|
+
# ```
|
62
|
+
|
63
|
+
# ## shared
|
64
|
+
# You can call methods defined by `shared` in invoking.
|
65
|
+
#
|
66
|
+
# ```
|
67
|
+
# scope(:view) do
|
68
|
+
# function(:function_name) do
|
69
|
+
# hello
|
70
|
+
# end
|
71
|
+
# end
|
72
|
+
#
|
73
|
+
# shared(:hello) do |world|
|
74
|
+
# "Hello, #{world}"
|
75
|
+
# end
|
76
|
+
# ```
|
77
|
+
|
78
|
+
# ## helpers
|
79
|
+
# You can call helpers in view via unit proxy like `unit.helper_method`
|
80
|
+
#
|
81
|
+
# ```
|
82
|
+
# helpers do
|
83
|
+
# def helper_method
|
84
|
+
# "helper method"
|
85
|
+
# end
|
86
|
+
# end
|
87
|
+
# ```
|
88
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module Nyanko
|
2
|
+
module Generators
|
3
|
+
class UnitGenerator < Rails::Generators::NamedBase
|
4
|
+
ASSETS_TYPES = %w[images javascripts stylesheets]
|
5
|
+
|
6
|
+
source_root File.expand_path("../templates", __FILE__)
|
7
|
+
|
8
|
+
def create_unit_directory
|
9
|
+
empty_directory(directory)
|
10
|
+
end
|
11
|
+
|
12
|
+
def create_unit_file
|
13
|
+
template("unit.rb.erb", "#{directory}/#{file_name}.rb")
|
14
|
+
end
|
15
|
+
|
16
|
+
def create_views_directory
|
17
|
+
create_file("#{directory}/views/.gitkeep", "")
|
18
|
+
end
|
19
|
+
|
20
|
+
ASSETS_TYPES.each do |type|
|
21
|
+
define_method("create_#{type}_directory") do
|
22
|
+
create_file("#{directory}/#{type}/.gitkeep", "")
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
ASSETS_TYPES.each do |type|
|
27
|
+
define_method("create_#{type}_symlink") do
|
28
|
+
create_assets_symlink(type)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def create_assets_symlink(type)
|
35
|
+
from = "app/assets/#{type}/units/#{file_name}"
|
36
|
+
to = "../../../../#{directory}/#{type}"
|
37
|
+
create_link(from, to)
|
38
|
+
end
|
39
|
+
|
40
|
+
def directory
|
41
|
+
"#{Nyanko::Config.units_directory_path}/#{file_name}"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/lib/nyanko/config.rb
CHANGED
@@ -9,6 +9,7 @@ module Nyanko
|
|
9
9
|
:enable_logger,
|
10
10
|
:proxy_method_name,
|
11
11
|
:raise_error,
|
12
|
+
:resolver,
|
12
13
|
:units_directory_path
|
13
14
|
)
|
14
15
|
|
@@ -20,6 +21,7 @@ module Nyanko
|
|
20
21
|
self.enable_logger = true
|
21
22
|
self.proxy_method_name = :unit
|
22
23
|
self.raise_error = Rails.env.development?
|
24
|
+
self.resolver = ActionView::OptimizedFileSystemResolver
|
23
25
|
self.units_directory_path = "app/units"
|
24
26
|
end
|
25
27
|
end
|
@@ -1,9 +1,9 @@
|
|
1
1
|
module Nyanko
|
2
2
|
module ExceptionHandler
|
3
3
|
class << self
|
4
|
-
def handle(exception)
|
4
|
+
def handle(exception, unit = nil)
|
5
5
|
Logger.debug(exception)
|
6
|
-
raise exception if Config.raise_error
|
6
|
+
raise exception if unit.try(:raise_error?) || Config.raise_error
|
7
7
|
end
|
8
8
|
end
|
9
9
|
end
|
data/lib/nyanko/function.rb
CHANGED
@@ -21,7 +21,11 @@ module Nyanko
|
|
21
21
|
def invoke(context, options = {})
|
22
22
|
with_unit_stack(context) do
|
23
23
|
with_unit_view_path(context) do
|
24
|
-
context
|
24
|
+
capture_exception(context) do
|
25
|
+
result = context.instance_eval(&block)
|
26
|
+
result = decorate(result, context, options[:type]) if context.view?
|
27
|
+
result
|
28
|
+
end
|
25
29
|
end
|
26
30
|
end
|
27
31
|
end
|
@@ -54,12 +58,28 @@ module Nyanko
|
|
54
58
|
end
|
55
59
|
|
56
60
|
def with_unit_view_path(context)
|
57
|
-
|
58
|
-
context.view_paths.unshift unit.resolver
|
59
|
-
end
|
61
|
+
context.view_paths.unshift unit.resolver
|
60
62
|
yield
|
61
63
|
ensure
|
62
|
-
context.view_paths.paths.shift
|
64
|
+
context.view_paths.paths.shift
|
65
|
+
end
|
66
|
+
|
67
|
+
def capture_exception(context)
|
68
|
+
yield
|
69
|
+
rescue Exception => exception
|
70
|
+
ExceptionHandler.handle(exception, unit)
|
71
|
+
context.run_default
|
72
|
+
end
|
73
|
+
|
74
|
+
def decorate(str, context, type)
|
75
|
+
case type
|
76
|
+
when :plain
|
77
|
+
str
|
78
|
+
when :inline
|
79
|
+
context.content_tag(:span, str, :class => css_classes)
|
80
|
+
else
|
81
|
+
context.content_tag(:div, str, :class => css_classes)
|
82
|
+
end
|
63
83
|
end
|
64
84
|
end
|
65
85
|
end
|
data/lib/nyanko/invoker.rb
CHANGED
@@ -5,20 +5,15 @@ module Nyanko
|
|
5
5
|
module Invoker
|
6
6
|
def invoke(*args, &block)
|
7
7
|
options = Options.new(*args)
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
ExceptionHandler.handle(exception)
|
18
|
-
run_default
|
19
|
-
ensure
|
20
|
-
defaults_stack.pop
|
21
|
-
unit_locals_stack.pop
|
8
|
+
__with_default_stack(block) do
|
9
|
+
__with_unit_locals_stack(options.locals) do
|
10
|
+
if function = FunctionFinder.find(self, options)
|
11
|
+
function.invoke(self, options.invoke_options)
|
12
|
+
else
|
13
|
+
run_default
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
22
17
|
end
|
23
18
|
|
24
19
|
def units
|
@@ -30,45 +25,77 @@ module Nyanko
|
|
30
25
|
end
|
31
26
|
|
32
27
|
def run_default
|
33
|
-
if
|
34
|
-
if view?
|
35
|
-
capture(&block)
|
36
|
-
else
|
37
|
-
instance_exec(&block)
|
38
|
-
end
|
39
|
-
end
|
28
|
+
__invoke_default_block if __has_default_block?
|
40
29
|
end
|
41
30
|
|
42
31
|
private
|
43
32
|
|
44
|
-
# Search shared method or locals variable
|
45
33
|
def method_missing(method_name, *args, &block)
|
46
|
-
if
|
47
|
-
|
48
|
-
elsif args.empty? &&
|
49
|
-
|
34
|
+
if shared_method = __find_shared_method(method_name)
|
35
|
+
instance_exec(*args, &shared_method)
|
36
|
+
elsif args.empty? && local = __find_unit_local(method_name)
|
37
|
+
local
|
50
38
|
else
|
51
39
|
super
|
52
40
|
end
|
53
41
|
end
|
54
42
|
|
55
|
-
def
|
56
|
-
|
43
|
+
def __find_unit_local(method_name)
|
44
|
+
__current_unit_locals[method_name]
|
45
|
+
end
|
46
|
+
|
47
|
+
def __current_unit_locals
|
48
|
+
__unit_locals_stack.last || {}
|
49
|
+
end
|
50
|
+
|
51
|
+
def __unit_locals_stack
|
52
|
+
@__unit_locals_stack ||= []
|
53
|
+
end
|
54
|
+
|
55
|
+
def __find_shared_method(method_name)
|
56
|
+
__current_shared_methods[method_name]
|
57
|
+
end
|
58
|
+
|
59
|
+
def __current_shared_methods
|
60
|
+
__current_unit.try(:shared_methods) || {}
|
57
61
|
end
|
58
62
|
|
59
|
-
def
|
60
|
-
|
63
|
+
def __current_unit
|
64
|
+
units.last
|
61
65
|
end
|
62
66
|
|
63
|
-
def
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
67
|
+
def __defaults_stack
|
68
|
+
@__defaults_stack ||= []
|
69
|
+
end
|
70
|
+
|
71
|
+
def __default_block
|
72
|
+
__defaults_stack.last
|
73
|
+
end
|
74
|
+
|
75
|
+
def __has_default_block?
|
76
|
+
!!__default_block
|
77
|
+
end
|
78
|
+
|
79
|
+
def __invoke_default_block
|
80
|
+
if view?
|
81
|
+
capture(&__default_block)
|
69
82
|
else
|
70
|
-
|
83
|
+
instance_exec(&__default_block)
|
71
84
|
end
|
72
85
|
end
|
86
|
+
|
87
|
+
def __with_default_stack(default)
|
88
|
+
__defaults_stack << default
|
89
|
+
yield
|
90
|
+
ensure
|
91
|
+
__defaults_stack.pop
|
92
|
+
end
|
93
|
+
|
94
|
+
def __with_unit_locals_stack(locals)
|
95
|
+
__unit_locals_stack << locals
|
96
|
+
yield
|
97
|
+
ensure
|
98
|
+
__unit_locals_stack.pop
|
99
|
+
end
|
73
100
|
end
|
74
101
|
end
|
@@ -1,47 +1,37 @@
|
|
1
1
|
module Nyanko
|
2
2
|
module Invoker
|
3
3
|
class FunctionFinder
|
4
|
-
FunctionNotFound = Class.new(StandardError)
|
5
|
-
|
6
4
|
def self.find(context, options)
|
7
|
-
new(context, options).find
|
8
|
-
raise FunctionNotFound, "The function for #{options[:functions].inspect} is not found"
|
5
|
+
new(context, options).find
|
9
6
|
end
|
10
7
|
|
8
|
+
attr_reader :context, :options
|
9
|
+
|
10
|
+
delegate :active_if_options, :as, :label, :unit_name, :to => :options
|
11
|
+
|
11
12
|
def initialize(context, options)
|
12
13
|
@context = context
|
13
14
|
@options = options
|
14
15
|
end
|
15
16
|
|
16
17
|
def find
|
17
|
-
|
18
|
-
unit = find_unit(unit_name)
|
19
|
-
identifier = @options[:as] || @context.class
|
20
|
-
next unless unit.try(:active?, @context, @options[:active_if_options])
|
21
|
-
function = unit.find_function(identifier, label)
|
22
|
-
break function if function
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
def find_with_dependencies
|
27
|
-
find_without_dependencies unless has_inactive_dependent_unit?
|
18
|
+
active? && find_function
|
28
19
|
end
|
29
|
-
alias_method_chain :find, :dependencies
|
30
20
|
|
31
|
-
def
|
32
|
-
|
21
|
+
def scope
|
22
|
+
as || context.class
|
33
23
|
end
|
34
24
|
|
35
|
-
def
|
36
|
-
|
25
|
+
def unit
|
26
|
+
Loader.load(unit_name)
|
37
27
|
end
|
38
28
|
|
39
|
-
def
|
40
|
-
|
29
|
+
def find_function
|
30
|
+
unit.find_function(scope, label)
|
41
31
|
end
|
42
32
|
|
43
|
-
def
|
44
|
-
|
33
|
+
def active?
|
34
|
+
unit.try(:active?, context, active_if_options)
|
45
35
|
end
|
46
36
|
end
|
47
37
|
end
|
@@ -5,7 +5,6 @@ module Nyanko
|
|
5
5
|
:active_if_options => true,
|
6
6
|
:as => true,
|
7
7
|
:capture => true,
|
8
|
-
:if => true,
|
9
8
|
:locals => true,
|
10
9
|
:type => true,
|
11
10
|
}
|
@@ -15,12 +14,12 @@ module Nyanko
|
|
15
14
|
@args = args
|
16
15
|
end
|
17
16
|
|
18
|
-
def
|
19
|
-
|
17
|
+
def unit_name
|
18
|
+
@args[0]
|
20
19
|
end
|
21
20
|
|
22
|
-
def
|
23
|
-
@args
|
21
|
+
def label
|
22
|
+
@args[1]
|
24
23
|
end
|
25
24
|
|
26
25
|
def locals
|
@@ -35,10 +34,6 @@ module Nyanko
|
|
35
34
|
options[:as]
|
36
35
|
end
|
37
36
|
|
38
|
-
def dependencies
|
39
|
-
options[:if]
|
40
|
-
end
|
41
|
-
|
42
37
|
def capture
|
43
38
|
options.has_key?(:capture) ? capture[:capture] : true
|
44
39
|
end
|
data/lib/nyanko/logger.rb
CHANGED
@@ -35,7 +35,7 @@ module Nyanko
|
|
35
35
|
|
36
36
|
def content
|
37
37
|
if @object.is_a?(Exception)
|
38
|
-
"#{klass}#{body}
|
38
|
+
"#{klass}#{body}#{backtrace}"
|
39
39
|
else
|
40
40
|
@object.to_s
|
41
41
|
end
|
@@ -52,8 +52,15 @@ module Nyanko
|
|
52
52
|
end
|
53
53
|
|
54
54
|
def backtrace
|
55
|
-
|
56
|
-
|
55
|
+
if has_backtrace?
|
56
|
+
lines = @object.backtrace[0...Config.backtrace_limit]
|
57
|
+
str = lines.map {|line| " #{line}" }.join("\n")
|
58
|
+
"\n#{str}"
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def has_backtrace?
|
63
|
+
@object.backtrace
|
57
64
|
end
|
58
65
|
end
|
59
66
|
end
|
data/lib/nyanko/test.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
module Nyanko
|
2
|
+
module Test
|
3
|
+
def self.activations
|
4
|
+
@activations ||= {}
|
5
|
+
end
|
6
|
+
|
7
|
+
def enable_unit(unit_name)
|
8
|
+
Test.activations[unit_name] = true
|
9
|
+
end
|
10
|
+
alias_method :enable_ext, :enable_unit
|
11
|
+
|
12
|
+
def disable_unit(unit_name)
|
13
|
+
Test.activations[unit_name] = false
|
14
|
+
end
|
15
|
+
alias_method :disable_ext, :disable_unit
|
16
|
+
end
|
17
|
+
|
18
|
+
module Unit
|
19
|
+
module ClassMethods
|
20
|
+
def active_with_activations?(*args)
|
21
|
+
case Test.activations[unit_name]
|
22
|
+
when true
|
23
|
+
true
|
24
|
+
when false
|
25
|
+
false
|
26
|
+
else
|
27
|
+
active_without_activations?(*args)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
alias_method_chain :active?, :activations
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
RSpec.configure do |config|
|
36
|
+
config.include Nyanko::Test
|
37
|
+
config.after { Nyanko::Test.activations.clear }
|
38
|
+
end
|
data/lib/nyanko/unit.rb
CHANGED
@@ -49,6 +49,15 @@ module Nyanko
|
|
49
49
|
ActiveIf::Any.new(*labels)
|
50
50
|
end
|
51
51
|
|
52
|
+
def raise_error
|
53
|
+
@raise_error = true
|
54
|
+
end
|
55
|
+
alias_method :propagates_errors, :raise_error
|
56
|
+
|
57
|
+
def raise_error?
|
58
|
+
@raise_error
|
59
|
+
end
|
60
|
+
|
52
61
|
def unit_name
|
53
62
|
@unit_name ||= name.underscore.to_sym
|
54
63
|
end
|
@@ -81,7 +90,7 @@ module Nyanko
|
|
81
90
|
end
|
82
91
|
|
83
92
|
def resolver
|
84
|
-
@resolver ||=
|
93
|
+
@resolver ||= Config.resolver.new(view_path)
|
85
94
|
end
|
86
95
|
|
87
96
|
def extender
|
@@ -7,6 +7,8 @@ module Nyanko
|
|
7
7
|
include Helper
|
8
8
|
end
|
9
9
|
|
10
|
+
# Define #unit method in this class when #unit is called in first time.
|
11
|
+
# Change Config.proxy_method_name if you want to change this method name.
|
10
12
|
def method_missing(method_name, *args, &block)
|
11
13
|
if Array.wrap(Config.proxy_method_name).include?(method_name)
|
12
14
|
UnitProxyProvider.class_eval do
|
data/lib/nyanko/version.rb
CHANGED
@@ -25,6 +25,10 @@ module ExampleUnit
|
|
25
25
|
function(:default) do
|
26
26
|
run_default
|
27
27
|
end
|
28
|
+
|
29
|
+
function(:render) do
|
30
|
+
render_to_string :partial => "/test", :locals => { :local => "test" }
|
31
|
+
end
|
28
32
|
end
|
29
33
|
|
30
34
|
scope(:view) do
|
@@ -51,6 +55,10 @@ module ExampleUnit
|
|
51
55
|
function(:helper) do
|
52
56
|
unit.helper
|
53
57
|
end
|
58
|
+
|
59
|
+
function(:render) do
|
60
|
+
render "/test", :local => "test"
|
61
|
+
end
|
54
62
|
end
|
55
63
|
|
56
64
|
helpers do
|
@@ -0,0 +1 @@
|
|
1
|
+
<%= local %>
|
@@ -2,13 +2,37 @@ require "spec_helper"
|
|
2
2
|
|
3
3
|
module Nyanko
|
4
4
|
describe ExceptionHandler do
|
5
|
-
|
5
|
+
let(:sensitive_unit) do
|
6
|
+
Loader.load(:sensitive_unit)
|
7
|
+
end
|
8
|
+
|
9
|
+
let(:insensitive_unit) do
|
10
|
+
Loader.load(:insensitive_unit)
|
11
|
+
end
|
12
|
+
|
13
|
+
let(:error) do
|
14
|
+
Exception.new
|
15
|
+
end
|
16
|
+
|
17
|
+
context "when Config.raise_error is false" do
|
18
|
+
it "raises up no error" do
|
19
|
+
expect { described_class.handle(error, insensitive_unit) }.not_to raise_error
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
context "when Config.raise_error is true" do
|
6
24
|
before do
|
7
25
|
Config.raise_error = true
|
8
26
|
end
|
9
27
|
|
10
28
|
it "raises up error" do
|
11
|
-
expect { described_class.handle(
|
29
|
+
expect { described_class.handle(error, insensitive_unit) }.to raise_error
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
context "when unit.raise_error is configured" do
|
34
|
+
it "raises up error" do
|
35
|
+
expect { described_class.handle(error, sensitive_unit) }.to raise_error
|
12
36
|
end
|
13
37
|
end
|
14
38
|
end
|
@@ -24,15 +24,19 @@ module Nyanko
|
|
24
24
|
end.new
|
25
25
|
end
|
26
26
|
|
27
|
+
let(:options) do
|
28
|
+
{ :type => :plain }
|
29
|
+
end
|
30
|
+
|
27
31
|
describe ".invoke" do
|
28
32
|
it "invokes block with given context and stacked unit" do
|
29
|
-
described_class.new(unit, :label) { current_unit }.invoke(context).should == unit
|
33
|
+
described_class.new(unit, :label) { current_unit }.invoke(context, options).should == unit
|
30
34
|
end
|
31
35
|
|
32
36
|
|
33
37
|
context "when context is a view" do
|
34
38
|
it "invokes with unit's view path" do
|
35
|
-
described_class.new(unit, :label) { path }.invoke(context).should == unit.view_path
|
39
|
+
described_class.new(unit, :label) { path }.invoke(context, options).should == unit.view_path
|
36
40
|
end
|
37
41
|
end
|
38
42
|
end
|
data/spec/nyanko/invoker_spec.rb
CHANGED
@@ -36,6 +36,18 @@ module Nyanko
|
|
36
36
|
view.invoke(:example_unit, :helper, :type => :plain).should == "helper"
|
37
37
|
end
|
38
38
|
|
39
|
+
context "when invoked in view" do
|
40
|
+
it "invokes with partial view" do
|
41
|
+
view.invoke(:example_unit, :render, :type => :plain).should == "test\n"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
context "when invoked in controller" do
|
46
|
+
it "invokes with unit views path" do
|
47
|
+
controller.invoke(:example_unit, :render, :type => :plain).should == "test\n"
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
39
51
|
context "when short-hand style args is passed" do
|
40
52
|
it "recognizes args as locals option" do
|
41
53
|
view.invoke(:example_unit, :locals, :key => "value").should ==
|
@@ -98,19 +110,6 @@ module Nyanko
|
|
98
110
|
end
|
99
111
|
end
|
100
112
|
|
101
|
-
context "when dependent unit is inactive" do
|
102
|
-
it "does nothing" do
|
103
|
-
view.invoke(:example_unit, :test, :if => :inactive_unit).should == nil
|
104
|
-
end
|
105
|
-
end
|
106
|
-
|
107
|
-
context "when 2 functions are specified" do
|
108
|
-
it "invokes first active function" do
|
109
|
-
view.invoke([:inactive_unit, :inactive], [:example_unit, :test], :type => :plain).
|
110
|
-
should == "test"
|
111
|
-
end
|
112
|
-
end
|
113
|
-
|
114
113
|
context "when function is not found" do
|
115
114
|
it "runs default but not handled by ExceptionHandler" do
|
116
115
|
ExceptionHandler.should_not_receive(:handle)
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "nyanko/test"
|
3
|
+
|
4
|
+
module Nyanko
|
5
|
+
describe Test do
|
6
|
+
let(:view) do
|
7
|
+
Class.new(ActionView::Base) do
|
8
|
+
include Nyanko::Invoker
|
9
|
+
include Nyanko::Helper
|
10
|
+
include Nyanko::UnitProxyProvider
|
11
|
+
end.new
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "#enable_unit" do
|
15
|
+
it "forces to enable specified unit" do
|
16
|
+
enable_unit(:inactive_unit)
|
17
|
+
view.invoke(:inactive_unit, :inactive, :type => :plain).should == "inactive"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "#disable_unit" do
|
22
|
+
it "forces to disable specified unit" do
|
23
|
+
disable_unit(:example_unit)
|
24
|
+
view.invoke(:example_unit, :test).should == nil
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/spec/spec_helper.rb
CHANGED
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
|
+
version: 0.0.8
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-02-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -199,6 +199,8 @@ files:
|
|
199
199
|
- MIT-LICENSE
|
200
200
|
- README.md
|
201
201
|
- Rakefile
|
202
|
+
- lib/generators/nyanko/unit/templates/unit.rb.erb
|
203
|
+
- lib/generators/nyanko/unit/unit_generator.rb
|
202
204
|
- lib/nyanko.rb
|
203
205
|
- lib/nyanko/active_if.rb
|
204
206
|
- lib/nyanko/config.rb
|
@@ -212,6 +214,7 @@ files:
|
|
212
214
|
- lib/nyanko/loader.rb
|
213
215
|
- lib/nyanko/logger.rb
|
214
216
|
- lib/nyanko/railtie.rb
|
217
|
+
- lib/nyanko/test.rb
|
215
218
|
- lib/nyanko/unit.rb
|
216
219
|
- lib/nyanko/unit/extender.rb
|
217
220
|
- lib/nyanko/unit/extender/active_record_class_methods.rb
|
@@ -266,7 +269,10 @@ files:
|
|
266
269
|
- spec/dummy/public/favicon.ico
|
267
270
|
- spec/dummy/script/rails
|
268
271
|
- spec/fixtures/units/example_unit/example_unit.rb
|
272
|
+
- spec/fixtures/units/example_unit/views/_test.html.erb
|
269
273
|
- spec/fixtures/units/inactive_unit/inactive_unit.rb
|
274
|
+
- spec/fixtures/units/insensitive_unit/insensitive_unit.rb
|
275
|
+
- spec/fixtures/units/sensitive_unit/sensitive_unit.rb
|
270
276
|
- spec/nyanko/controller_spec.rb
|
271
277
|
- spec/nyanko/exception_handler_spec.rb
|
272
278
|
- spec/nyanko/function_spec.rb
|
@@ -274,6 +280,7 @@ files:
|
|
274
280
|
- spec/nyanko/invoker_spec.rb
|
275
281
|
- spec/nyanko/loader_spec.rb
|
276
282
|
- spec/nyanko/logger_spec.rb
|
283
|
+
- spec/nyanko/test_spec.rb
|
277
284
|
- spec/nyanko/unit/extender_spec.rb
|
278
285
|
- spec/nyanko/unit/scope_finder_spec.rb
|
279
286
|
- spec/nyanko/unit_proxy_provider_spec.rb
|
@@ -350,7 +357,10 @@ test_files:
|
|
350
357
|
- spec/dummy/public/favicon.ico
|
351
358
|
- spec/dummy/script/rails
|
352
359
|
- spec/fixtures/units/example_unit/example_unit.rb
|
360
|
+
- spec/fixtures/units/example_unit/views/_test.html.erb
|
353
361
|
- spec/fixtures/units/inactive_unit/inactive_unit.rb
|
362
|
+
- spec/fixtures/units/insensitive_unit/insensitive_unit.rb
|
363
|
+
- spec/fixtures/units/sensitive_unit/sensitive_unit.rb
|
354
364
|
- spec/nyanko/controller_spec.rb
|
355
365
|
- spec/nyanko/exception_handler_spec.rb
|
356
366
|
- spec/nyanko/function_spec.rb
|
@@ -358,6 +368,7 @@ test_files:
|
|
358
368
|
- spec/nyanko/invoker_spec.rb
|
359
369
|
- spec/nyanko/loader_spec.rb
|
360
370
|
- spec/nyanko/logger_spec.rb
|
371
|
+
- spec/nyanko/test_spec.rb
|
361
372
|
- spec/nyanko/unit/extender_spec.rb
|
362
373
|
- spec/nyanko/unit/scope_finder_spec.rb
|
363
374
|
- spec/nyanko/unit_proxy_provider_spec.rb
|