neo-rails 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,5 @@
1
1
  require 'active_support/concern'
2
+ require 'neo/rails/exposure/exposures'
2
3
 
3
4
  module Neo
4
5
  module Rails
@@ -7,20 +8,30 @@ module Neo
7
8
  module Exposure
8
9
  extend ActiveSupport::Concern
9
10
 
10
- class UndeclaredVariableError < StandardError; end
11
-
12
11
  included do
13
- class_attribute :exposed_vars
14
- self.exposed_vars ||= Set.new
12
+ class_attribute :exposure_names
13
+ self.exposure_names ||= Set.new
14
+ end
15
+
16
+ def exposures
17
+ @exposures ||= Exposures.new(self.class.exposure_names)
15
18
  end
16
19
 
17
20
  module ClassMethods
18
21
  # Defines the variables to be exposed.
19
22
  def exposes(*names)
20
- exposures = names.map(&:to_sym)
21
- self.exposed_vars.merge exposures
22
- attr_reader *exposures
23
- helper_method *exposures
23
+ exposure_names = names.map(&:to_sym)
24
+ self.exposure_names.merge exposure_names
25
+
26
+ # Define a helper method for each exposure
27
+ exposure_names.each do |exposure_name|
28
+ # see Rails: /actionpack/lib/abstract_controller/helpers.rb
29
+ helper do
30
+ define_method exposure_name do
31
+ controller.exposures[exposure_name]
32
+ end
33
+ end
34
+ end
24
35
  end
25
36
  end
26
37
 
@@ -32,17 +43,14 @@ module Neo
32
43
  # Raise UndeclaredVariableError if access variable wasn't declared before.
33
44
  def expose(key, value=nil)
34
45
  name = key.to_sym
35
- raise UndeclaredVariableError unless self.class.exposed_vars.include?(name)
36
-
37
46
  value = yield if block_given?
38
-
39
- self.instance_variable_set("@#{name}", value)
47
+ self.exposures[key] = value
40
48
  end
41
49
 
42
50
  private
43
51
 
44
- def exposed?(name)
45
- instance_variable_defined?("@#{name}")
52
+ def exposed?(key)
53
+ self.exposures.exposed?(key)
46
54
  end
47
55
  end
48
56
  end
@@ -0,0 +1,7 @@
1
+ module Neo
2
+ module Rails
3
+ module Exposure
4
+ class UndeclaredVariableError < StandardError; end
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,34 @@
1
+ require 'neo/rails/exposure/errors'
2
+
3
+ module Neo
4
+ module Rails
5
+ module Exposure
6
+ # Stores the concrete exposures per request
7
+ class Exposures
8
+ extend Forwardable
9
+
10
+ def_delegator :@store, :[]
11
+ def_delegator :@store, :key?, :exposed?
12
+
13
+ def initialize(names)
14
+ @names = names
15
+ @store = Hash.new
16
+ end
17
+
18
+ def []=(key, value)
19
+ raise UndeclaredVariableError unless @names.include?(key)
20
+ @store[key] = value
21
+ end
22
+
23
+ def method_missing(method_name, *args)
24
+ if exposed?(method_name)
25
+ @store[method_name]
26
+ else
27
+ super
28
+ end
29
+ end
30
+
31
+ end
32
+ end
33
+ end
34
+ end
@@ -1,5 +1,5 @@
1
1
  module Neo
2
2
  module Rails
3
- VERSION = "0.1.3"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
@@ -18,13 +18,20 @@ class ExposureTestController < ActionController::Base
18
18
  def expose_via_value
19
19
  expose :a, "a"
20
20
  expose :b, "b"
21
- render :inline => %{ar=#{a},ah=<%= a %>,br=#{b},bh=<%= b %>}
21
+ render :inline => %{ar=#{exposures.a},ah=<%= a %>,br=#{exposures.b},bh=<%= b %>}
22
22
  end
23
23
 
24
24
  def expose_via_block
25
25
  expose(:a) { "a" }
26
26
  expose(:b) { "b" }
27
- render :inline => %{ar=#{a},ah=<%= a %>,br=#{b},bh=<%= b %>}
27
+ render :inline => %{ar=#{exposures.a},ah=<%= a %>,br=#{exposures.b},bh=<%= b %>}
28
+ end
29
+
30
+ def exposes_via_value_with_template
31
+ expose(:a, "a")
32
+ expose(:b) { "b" }
33
+ template = File.expand_path(File.join("..", "fixtures", "views", "exposes_via_value_with_template"), __FILE__)
34
+ render :file => template
28
35
  end
29
36
 
30
37
  def expose_with_error
@@ -103,6 +110,12 @@ class ExposureTest < NeoRailsCase
103
110
  assert_equal "ar=a,ah=a,br=b,bh=b", last_response.body
104
111
  end
105
112
 
113
+ test "should expose variable per value with template" do
114
+ get :exposes_via_value_with_template
115
+ assert_equal "ah=a,bh=b", last_response.body
116
+ end
117
+
118
+
106
119
  test "should raise an error when exposing an undeclared variable" do
107
120
  assert_raises Neo::Rails::Exposure::UndeclaredVariableError do
108
121
  get :expose_with_error
@@ -0,0 +1,29 @@
1
+ require 'helper'
2
+
3
+ require 'neo/rails/exposure/exposures'
4
+
5
+ class ExposuresTest < NeoRailsCase
6
+ let(:exposures){ Neo::Rails::Exposure::Exposures.new([:a])}
7
+
8
+ test "exposed?" do
9
+ refute exposures.exposed?(:a)
10
+ exposures[:a] = nil
11
+ assert exposures.exposed?(:a)
12
+ refute exposures.exposed?(:b)
13
+ exposures[:a] = 1
14
+ assert exposures.exposed?(:a)
15
+ refute exposures.exposed?(:b)
16
+ end
17
+
18
+ test "sets and gets a key" do
19
+ exposures[:a] = "foo"
20
+ assert_equal "foo", exposures[:a]
21
+ assert_equal "foo", exposures.a
22
+ end
23
+
24
+ test "raises if a undefined key is set" do
25
+ assert_raises Neo::Rails::Exposure::UndeclaredVariableError do
26
+ exposures[:c] = 1
27
+ end
28
+ end
29
+ end
@@ -0,0 +1 @@
1
+ ah=<%= a %>,bh=<%= b %>
metadata CHANGED
@@ -1,7 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: neo-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
+ prerelease:
5
6
  platform: ruby
6
7
  authors:
7
8
  - Jonas Thiel
@@ -9,62 +10,70 @@ authors:
9
10
  autorequire:
10
11
  bindir: bin
11
12
  cert_chain: []
12
- date: 2013-07-01 00:00:00.000000000 Z
13
+ date: 2013-07-02 00:00:00.000000000 Z
13
14
  dependencies:
14
15
  - !ruby/object:Gem::Dependency
15
16
  name: rake
16
17
  requirement: !ruby/object:Gem::Requirement
18
+ none: false
17
19
  requirements:
18
- - - '>='
20
+ - - ! '>='
19
21
  - !ruby/object:Gem::Version
20
22
  version: '0'
21
23
  type: :development
22
24
  prerelease: false
23
25
  version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
24
27
  requirements:
25
- - - '>='
28
+ - - ! '>='
26
29
  - !ruby/object:Gem::Version
27
30
  version: '0'
28
31
  - !ruby/object:Gem::Dependency
29
32
  name: minitest
30
33
  requirement: !ruby/object:Gem::Requirement
34
+ none: false
31
35
  requirements:
32
- - - '>='
36
+ - - ! '>='
33
37
  - !ruby/object:Gem::Version
34
38
  version: '0'
35
39
  type: :development
36
40
  prerelease: false
37
41
  version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
38
43
  requirements:
39
- - - '>='
44
+ - - ! '>='
40
45
  - !ruby/object:Gem::Version
41
46
  version: '0'
42
47
  - !ruby/object:Gem::Dependency
43
48
  name: rack-test
44
49
  requirement: !ruby/object:Gem::Requirement
50
+ none: false
45
51
  requirements:
46
- - - '>='
52
+ - - ! '>='
47
53
  - !ruby/object:Gem::Version
48
54
  version: '0'
49
55
  type: :development
50
56
  prerelease: false
51
57
  version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
52
59
  requirements:
53
- - - '>='
60
+ - - ! '>='
54
61
  - !ruby/object:Gem::Version
55
62
  version: '0'
56
63
  - !ruby/object:Gem::Dependency
57
64
  name: simplecov
58
65
  requirement: !ruby/object:Gem::Requirement
66
+ none: false
59
67
  requirements:
60
- - - '>='
68
+ - - ! '>='
61
69
  - !ruby/object:Gem::Version
62
70
  version: '0'
63
71
  type: :development
64
72
  prerelease: false
65
73
  version_requirements: !ruby/object:Gem::Requirement
74
+ none: false
66
75
  requirements:
67
- - - '>='
76
+ - - ! '>='
68
77
  - !ruby/object:Gem::Version
69
78
  version: '0'
70
79
  description: Some Rails helpers
@@ -90,6 +99,8 @@ files:
90
99
  - lib/neo/rails.rb
91
100
  - lib/neo/rails/engine.rb
92
101
  - lib/neo/rails/exposure.rb
102
+ - lib/neo/rails/exposure/errors.rb
103
+ - lib/neo/rails/exposure/exposures.rb
93
104
  - lib/neo/rails/mock.rb
94
105
  - lib/neo/rails/presenter.rb
95
106
  - lib/neo/rails/presenter/test_helper.rb
@@ -102,34 +113,45 @@ files:
102
113
  - lib/tasks/share/app/presenters/presenter.rb
103
114
  - neo-rails.gemspec
104
115
  - test/exposure_test.rb
116
+ - test/fixtures/exposure/exposures_test.rb
117
+ - test/fixtures/views/exposes_via_value_with_template.html.erb
105
118
  - test/helper.rb
106
119
  - test/mock_test.rb
107
120
  - test/presenter_test.rb
108
121
  homepage: ''
109
122
  licenses: []
110
- metadata: {}
111
123
  post_install_message:
112
124
  rdoc_options: []
113
125
  require_paths:
114
126
  - lib
115
127
  required_ruby_version: !ruby/object:Gem::Requirement
128
+ none: false
116
129
  requirements:
117
- - - '>='
130
+ - - ! '>='
118
131
  - !ruby/object:Gem::Version
119
132
  version: '0'
133
+ segments:
134
+ - 0
135
+ hash: 4337019360723826631
120
136
  required_rubygems_version: !ruby/object:Gem::Requirement
137
+ none: false
121
138
  requirements:
122
- - - '>='
139
+ - - ! '>='
123
140
  - !ruby/object:Gem::Version
124
141
  version: '0'
142
+ segments:
143
+ - 0
144
+ hash: 4337019360723826631
125
145
  requirements: []
126
146
  rubyforge_project:
127
- rubygems_version: 2.0.2
147
+ rubygems_version: 1.8.25
128
148
  signing_key:
129
- specification_version: 4
149
+ specification_version: 3
130
150
  summary: Rails mocks, presenters, exposure and scenarios
131
151
  test_files:
132
152
  - test/exposure_test.rb
153
+ - test/fixtures/exposure/exposures_test.rb
154
+ - test/fixtures/views/exposes_via_value_with_template.html.erb
133
155
  - test/helper.rb
134
156
  - test/mock_test.rb
135
157
  - test/presenter_test.rb
checksums.yaml DELETED
@@ -1,7 +0,0 @@
1
- ---
2
- SHA1:
3
- metadata.gz: 203a1b4ae42922e92a21ef75b9fc83c9bc16249f
4
- data.tar.gz: 0f46ab87f2419d3683018356fa7d0f7ba800c8be
5
- SHA512:
6
- metadata.gz: f3f2ca09a97985d7138aeba73a25398fd4dcfdefa8304107aeddf39bb7de338cb32d5539234cc7afcd679acd9abea78bcf631f00caa1dc3a45b4d43490aeee93
7
- data.tar.gz: 901b2b8ed25bfe002bc2a79614cfd73becf637fdc3e643bfb6c499f35572b04ffe4442e50bac1d476d0877b1adb61fc786ec991c4f7223f28916acafc46320e2