nyanko 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/lib/nyanko/config.rb CHANGED
@@ -2,9 +2,9 @@ module Nyanko
2
2
  module Config
3
3
  class << self
4
4
  attr_accessor(
5
+ :auto_reload,
5
6
  :backtrace_limit,
6
7
  :cache_units,
7
- :clear_units_cache,
8
8
  :compatible_css_class,
9
9
  :enable_logger,
10
10
  :proxy_method_name,
@@ -13,8 +13,10 @@ module Nyanko
13
13
  )
14
14
 
15
15
  def reset
16
+ self.auto_reload = Rails.env.development? || Rails.env.test?
16
17
  self.backtrace_limit = 10
17
- self.clear_units_cache = Rails.env.development? || Rails.env.test?
18
+ self.cache_units = false
19
+ self.compatible_css_class = false
18
20
  self.enable_logger = true
19
21
  self.proxy_method_name = :unit
20
22
  self.raise_error = Rails.env.development?
@@ -6,7 +6,7 @@ module Nyanko
6
6
  private
7
7
 
8
8
  def inherited(base)
9
- if Config.clear_units_cache && base.name == "ApplicationController"
9
+ if Config.auto_reload && base.name == "ApplicationController"
10
10
  base.class_eval do
11
11
  prepend_before_filter do
12
12
  Nyanko::Loader.cache.clear
@@ -30,14 +30,14 @@ module Nyanko
30
30
  if Config.compatible_css_class
31
31
  %W[
32
32
  extension
33
- ext_#{unit.name.underscore}
34
- ext_#{unit.name.underscore}-#{label}
33
+ ext_#{unit.unit_name}
34
+ ext_#{unit.unit_name}-#{label}
35
35
  ]
36
36
  else
37
37
  %W[
38
38
  unit
39
- unit__#{unit.name.underscore}
40
- unit__#{unit.name.underscore}__#{label}
39
+ unit__#{unit.unit_name}
40
+ unit__#{unit.unit_name}__#{label}
41
41
  ]
42
42
  end
43
43
  end
@@ -55,11 +55,11 @@ module Nyanko
55
55
 
56
56
  def with_unit_view_path(context)
57
57
  if context.view?
58
- context.view_paths.unshift unit.view_path
58
+ context.view_paths.unshift unit.resolver
59
59
  end
60
60
  yield
61
61
  ensure
62
- context.view_paths.paths.unshift if context.view?
62
+ context.view_paths.paths.shift if context.view?
63
63
  end
64
64
  end
65
65
  end
data/lib/nyanko/helper.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  module Nyanko
2
2
  module Helper
3
3
  class << self
4
- def define(class_name, &block)
5
- prefix = UnitProxy.generate_prefix(class_name)
4
+ def define(unit_name, &block)
5
+ prefix = UnitProxy.generate_prefix(unit_name)
6
6
  define_methods_with_prefix(prefix, &block)
7
7
  end
8
8
 
data/lib/nyanko/unit.rb CHANGED
@@ -30,7 +30,7 @@ module Nyanko
30
30
  end
31
31
 
32
32
  def helpers(&block)
33
- Helper.define(name, &block)
33
+ Helper.define(unit_name, &block)
34
34
  end
35
35
 
36
36
  def models(&block)
@@ -49,16 +49,16 @@ module Nyanko
49
49
  ActiveIf::Any.new(*labels)
50
50
  end
51
51
 
52
- def to_key
53
- name.underscore.to_sym
52
+ def unit_name
53
+ @unit_name ||= name.underscore.to_sym
54
54
  end
55
55
 
56
56
  def to_prefix
57
- UnitProxy.generate_prefix(name)
57
+ UnitProxy.generate_prefix(unit_name)
58
58
  end
59
59
 
60
60
  def view_path
61
- "#{Config.units_directory_path}/#{name.underscore}/views"
61
+ "#{Config.units_directory_path}/#{unit_name}/views"
62
62
  end
63
63
 
64
64
  def find_function(identifier, label)
@@ -80,6 +80,10 @@ module Nyanko
80
80
  @shared_methods ||= {}
81
81
  end
82
82
 
83
+ def resolver
84
+ @resolver ||= ActionView::OptimizedFileSystemResolver.new(view_path)
85
+ end
86
+
83
87
  def extender
84
88
  @extender ||= Extender.new(to_prefix)
85
89
  end
@@ -4,8 +4,8 @@ module Nyanko
4
4
  class UnitProxy
5
5
  attr_reader :unit
6
6
 
7
- def self.generate_prefix(name)
8
- %{__#{name.underscore.gsub("/", "_")}_}
7
+ def self.generate_prefix(unit_name)
8
+ "__#{unit_name}_"
9
9
  end
10
10
 
11
11
  def initialize(unit, context)
@@ -20,7 +20,7 @@ module Nyanko
20
20
  private
21
21
 
22
22
  def prefix
23
- self.class.generate_prefix(@unit.name)
23
+ self.class.generate_prefix(@unit.unit_name)
24
24
  end
25
25
 
26
26
  def method_missing(method_name, *args, &block)
@@ -8,10 +8,10 @@ module Nyanko
8
8
  end
9
9
 
10
10
  def method_missing(method_name, *args, &block)
11
- if method_name == Config.proxy_method_name.to_sym
12
- class_eval do
11
+ if Array.wrap(Config.proxy_method_name).include?(method_name)
12
+ UnitProxyProvider.class_eval do
13
13
  define_method(method_name) do |*_args|
14
- name = _args.first || Function.current_unit.try(:to_key)
14
+ name = _args.first || Function.current_unit.try(:unit_name)
15
15
  if name && unit = Loader.load(name)
16
16
  UnitProxy.new(unit, self)
17
17
  end
@@ -1,3 +1,3 @@
1
1
  module Nyanko
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
3
3
  end
@@ -3,10 +3,8 @@ require "spec_helper"
3
3
  module Nyanko
4
4
  describe ExceptionHandler do
5
5
  context "when Config.raise_error is truthy" do
6
- around do |example|
7
- origin, Config.raise_error = Config.raise_error, true
8
- example.run
9
- Config.raise_error = origin
6
+ before do
7
+ Config.raise_error = true
10
8
  end
11
9
 
12
10
  it "raises up error" do
@@ -14,7 +14,7 @@ module Nyanko
14
14
  end
15
15
 
16
16
  it "defines helper methods with special prefix" do
17
- described_class.define("ExampleUnit") do
17
+ described_class.define(:example_unit) do
18
18
  def test
19
19
  "test"
20
20
  end
@@ -50,11 +50,9 @@ 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
53
+ context "when Config.compatible_css_class is true" do
54
+ before do
55
+ Config.compatible_css_class = true
58
56
  end
59
57
 
60
58
  it "invokes and returns result surrounded by div" do
@@ -40,10 +40,8 @@ module Nyanko
40
40
  end
41
41
 
42
42
  context "when Config.enable_logger is true" do
43
- around do |example|
44
- origin, Config.enable_logger = Config.enable_logger, true
45
- example.run
46
- Config.enable_logger = origin
43
+ before do
44
+ Config.enable_logger = true
47
45
  end
48
46
 
49
47
  context "when given Exception" do
@@ -53,10 +51,8 @@ module Nyanko
53
51
  end
54
52
 
55
53
  context "when Config.backtrace_limit is configured" do
56
- around do |example|
57
- origin, Config.backtrace_limit = Config.backtrace_limit, 5
58
- example.run
59
- Config.backtrace_limit = origin
54
+ before do
55
+ Config.backtrace_limit = 5
60
56
  end
61
57
 
62
58
  it "prints backtrace up to configured depth" do
@@ -75,10 +71,8 @@ module Nyanko
75
71
  end
76
72
 
77
73
  context "when Config.enable_logger is false" do
78
- around do |example|
79
- origin, Config.enable_logger = Config.enable_logger, false
80
- example.run
81
- Config.enable_logger = origin
74
+ before do
75
+ Config.enable_logger = false
82
76
  end
83
77
 
84
78
  it "logs nothing" do
@@ -33,16 +33,36 @@ module Nyanko
33
33
  context "when Config.proxy_method_name is configured" do
34
34
  around do |example|
35
35
  origin, Config.proxy_method_name = Config.proxy_method_name, :proxy
36
+ described_class.class_eval { remove_method origin } if view.respond_to?(origin)
36
37
  example.run
37
- Config.proxy_method_name = origin
38
+ described_class.class_eval { remove_method :proxy }
38
39
  end
39
40
 
40
41
  it "change this method name with it" do
42
+ view.should_not be_respond_to(:proxy)
41
43
  proxy = view.proxy(:example_unit)
42
44
  proxy.should be_a UnitProxy
43
45
  view.should be_respond_to(:proxy)
44
46
  end
45
47
  end
48
+
49
+ context "when Config.proxy_method_name is configured as Array" do
50
+ around do |example|
51
+ origin, Config.proxy_method_name = Config.proxy_method_name, [:proxy1, :proxy2]
52
+ described_class.class_eval { remove_method origin } if view.respond_to?(origin)
53
+ example.run
54
+ described_class.class_eval { remove_method :proxy1, :proxy2 }
55
+ end
56
+
57
+ it "change this method name with it" do
58
+ view.should_not be_respond_to(:proxy1)
59
+ view.should_not be_respond_to(:proxy2)
60
+ view.proxy1(:example_unit).should be_a UnitProxy
61
+ view.proxy2(:example_unit).should be_a UnitProxy
62
+ view.should be_respond_to(:proxy1)
63
+ view.should be_respond_to(:proxy2)
64
+ end
65
+ end
46
66
  end
47
67
  end
48
68
  end
data/spec/spec_helper.rb CHANGED
@@ -6,7 +6,6 @@ end
6
6
 
7
7
  ENV["RAILS_ENV"] ||= "test"
8
8
  require "nyanko"
9
- Nyanko::Config.units_directory_path = File.expand_path("../fixtures/units", __FILE__)
10
9
 
11
10
  require File.expand_path("../dummy/config/environment", __FILE__)
12
11
  require "rspec/rails"
@@ -18,4 +17,9 @@ RSpec.configure do |config|
18
17
  config.treat_symbols_as_metadata_keys_with_true_values = true
19
18
  config.run_all_when_everything_filtered = true
20
19
  config.filter_run :focus
20
+
21
+ config.after do
22
+ Nyanko::Config.reset
23
+ Nyanko::Config.units_directory_path = File.expand_path("../fixtures/units", __FILE__)
24
+ end
21
25
  end
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.6
4
+ version: 0.0.7
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-01-29 00:00:00.000000000 Z
12
+ date: 2013-01-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails