rory 0.4.0 → 0.4.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0e4715ee9ca93a684ebe09585ff35687f6f43538
4
- data.tar.gz: 5d81ed936318f64c428dd56483277f8233530012
3
+ metadata.gz: 73fd578d6e35268b6a7940ef886b86e1cd03f923
4
+ data.tar.gz: 51731d0b123a43034399bd0c27b409dcbc898b4c
5
5
  SHA512:
6
- metadata.gz: cfdb1ea2f198e736555a7ea8e9ed1123da944d11fa8a0d850d2be0b5b01dd580988caf0bd97f6adc59ffd04376043783aac551f3b2d5da35fadb4235f776688a
7
- data.tar.gz: 70b9cdd2237826aaf6f078f6399dad4e0f5267f71fff0c08140f514555f7a327ffd83e72c0f27c03725f43dcde43954b086d93b00931e7ec0ee3175204a40079
6
+ metadata.gz: 170ee5f9d5e4d1da5dc21c733d41cc26e1d8d0e12738c3edfb66739fcac09eca554019bb116b54d608f96658dcb4d3fd01b97d33acd7d9961309a1782eeb973a
7
+ data.tar.gz: f02a5ae7d109c79e84cf3e28205c6a42bdf8b321520adfa9868fe8489010b8caaf513b9734b5fe06b4dcd3fd53214796141e9786a4e2e3e9e90fbaec8011fd37
@@ -6,7 +6,6 @@ end
6
6
 
7
7
  require 'yaml'
8
8
  require 'sequel'
9
- require 'rory/hash_with_dubious_semantics'
10
9
  require 'rory/application'
11
10
  require 'rory/dispatcher'
12
11
  require 'rory/route'
@@ -50,7 +50,10 @@ module Rory
50
50
  end
51
51
 
52
52
  def params
53
- @converted_params ||= Rory::HashWithDubiousSemantics.new(@params)
53
+ @converted_params ||= @params.inject({}) { |memo, (key, value)|
54
+ memo[key.to_sym] = memo[key.to_s] = value
55
+ memo
56
+ }
54
57
  end
55
58
 
56
59
  def route_template
@@ -112,7 +115,29 @@ module Rory
112
115
 
113
116
  def call_filter_for_action?(filter, action)
114
117
  (filter[:only].nil? || filter[:only].include?(action)) &&
115
- (filter[:except].nil? || !filter[:except].include?(action))
118
+ (filter[:except].nil? || !filter[:except].include?(action)) &&
119
+ filter_conditions_pass?(filter, :if) &&
120
+ filter_conditions_pass?(filter, :unless)
121
+ end
122
+
123
+ def filter_conditions_pass?(filter, type)
124
+ filter_conditions = Array(filter[type])
125
+ filter_conditions.compact.all? { |condition|
126
+ result = assess_filter_condition(condition)
127
+ if type == :unless
128
+ result = !result
129
+ end
130
+ result
131
+ }
132
+ end
133
+
134
+ def assess_filter_condition(condition)
135
+ case condition
136
+ when Symbol
137
+ self.send(condition)
138
+ when Proc
139
+ instance_exec(&condition)
140
+ end
116
141
  end
117
142
 
118
143
  def get_relevant_filters(which_set, action)
@@ -1,3 +1,3 @@
1
1
  module Rory
2
- VERSION = '0.4.0'
2
+ VERSION = '0.4.1'
3
3
  end
@@ -1,5 +1,9 @@
1
1
  class BaseFilteredController < Rory::Controller
2
2
  before_action :pickle_something, :except => [:eat]
3
- after_action :rub_tummy
3
+ after_action :rub_tummy, :if => :horses_exist?
4
4
  after_action :smile, :only => [:eat]
5
+
6
+ def horses_exist?
7
+ params[:horses] != 'missing'
8
+ end
5
9
  end
@@ -1,7 +1,7 @@
1
1
  require_relative 'base_filtered_controller'
2
2
 
3
3
  class FilteredController < BaseFilteredController
4
- before_action :make_it_tasty
4
+ before_action :make_it_tasty, :unless => proc { !horses_exist? }
5
5
  before_action :make_it_nutritious, :only => [:eat]
6
6
  after_action :sleep, :except => [:eat]
7
7
 
@@ -26,11 +26,12 @@ describe Rory::Controller do
26
26
  describe '#params' do
27
27
  it 'returns params from request, converted for indifferent key access' do
28
28
  controller = Rory::Controller.new(@request, @routing)
29
-
30
- expect( controller.params[:violet] ) .to eq( 'invisibility' )
31
- expect( controller.params['violet'] ) .to eq( 'invisibility' )
32
- expect( controller.params[:dash] ) .to eq( 'superspeed' )
33
- expect( controller.params['dash'] ) .to eq( 'superspeed' )
29
+ expect(controller.params).to eq({
30
+ 'violet' => 'invisibility',
31
+ 'dash' => 'superspeed',
32
+ :violet => 'invisibility',
33
+ :dash => 'superspeed'
34
+ })
34
35
  end
35
36
  end
36
37
 
@@ -142,6 +143,23 @@ describe Rory::Controller do
142
143
  controller.present
143
144
  end
144
145
 
146
+ it "filters before and after actions on :if and :unless" do
147
+ @routing[:route] = Rory::Route.new('', :to => 'test#eat')
148
+ @request = double('Rack::Request', {
149
+ :params => { 'horses' => 'missing' },
150
+ :script_name => 'script_root'
151
+ })
152
+ controller = FilteredController.new(@request, @routing)
153
+ expect(controller).to receive(:make_it_tasty).never
154
+ expect(controller).to receive(:make_it_nutritious).ordered
155
+ expect(controller).to receive(:eat).ordered
156
+ expect(controller).to receive(:rub_tummy).never
157
+ expect(controller).to receive(:smile).ordered
158
+ expect(controller).to receive(:sleep).never
159
+ expect(controller).to receive(:render).ordered
160
+ controller.present
161
+ end
162
+
145
163
  it "just returns a response if @response exists" do
146
164
  controller = Rory::Controller.new(@request, @routing)
147
165
  controller.instance_variable_set(:@response, 'Forced response')
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rory
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ravi Gadad
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-04 00:00:00.000000000 Z
11
+ date: 2015-02-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -171,7 +171,6 @@ files:
171
171
  - lib/rory/application.rb
172
172
  - lib/rory/controller.rb
173
173
  - lib/rory/dispatcher.rb
174
- - lib/rory/hash_with_dubious_semantics.rb
175
174
  - lib/rory/path_generation.rb
176
175
  - lib/rory/renderer.rb
177
176
  - lib/rory/renderer/context.rb
@@ -204,7 +203,6 @@ files:
204
203
  - spec/lib/rory/application_spec.rb
205
204
  - spec/lib/rory/controller_spec.rb
206
205
  - spec/lib/rory/dispatcher_spec.rb
207
- - spec/lib/rory/hash_with_dubious_semantics_spec.rb
208
206
  - spec/lib/rory/renderer/context_spec.rb
209
207
  - spec/lib/rory/renderer_spec.rb
210
208
  - spec/lib/rory/route_spec.rb
@@ -1,58 +0,0 @@
1
- require 'delegate'
2
-
3
- module Rory
4
- # ActiveSupport's HashWithIndifferentAccess put it best:
5
- #
6
- # "This class has dubious semantics and we only have it so that people
7
- # can write params[:key] instead of params[‘key’] and they get the
8
- # same value for both keys."
9
- class HashWithDubiousSemantics < SimpleDelegator
10
- def initialize(hash)
11
- fail ArgumentError unless hash.is_a?(Hash)
12
- hash_with_no_symbols = convert_hash(hash)
13
- super( hash_with_no_symbols )
14
- end
15
-
16
- def [](key)
17
- actual_key = convert_key(key)
18
- __getobj__[actual_key]
19
- end
20
-
21
- def []=(key, value)
22
- actual_key = convert_key(key)
23
- new_value = convert_value(value)
24
- __getobj__[actual_key] = new_value
25
- end
26
-
27
- def inspect
28
- "#<#{self.class.name} @hash=#{super}>"
29
- end
30
-
31
- private
32
-
33
- def convert_hash(hash)
34
- return hash if hash.empty?
35
-
36
- hash.each_with_object({}) do |(key, value), converted_hash|
37
- new_key = convert_key(key)
38
- new_value = convert_value(value)
39
-
40
- converted_hash[new_key] = new_value
41
- end
42
- end
43
-
44
- def convert_key(key)
45
- case key
46
- when Symbol ; key.to_s
47
- else ; key
48
- end
49
- end
50
-
51
- def convert_value(value)
52
- case value
53
- when Hash ; HashWithDubiousSemantics.new(convert_hash(value))
54
- else ; value
55
- end
56
- end
57
- end
58
- end
@@ -1,64 +0,0 @@
1
- describe Rory::HashWithDubiousSemantics do
2
- subject {
3
- described_class.new(hash)
4
- }
5
-
6
- context "with a variety of keys" do
7
- let(:hash) { {
8
- 'violet' => 'invisibility',
9
- :dash => 'superspeed',
10
- 42 => 'meaning of life...'
11
- } }
12
-
13
- it 'allows indifferent access for a key that was originally a String' do
14
- expect( subject[:violet] ) .to eq( 'invisibility' )
15
- expect( subject['violet'] ) .to eq( 'invisibility' )
16
- end
17
-
18
- it 'allows indifferent access for a key that was originally a Symbol' do
19
- expect( subject[:dash] ) .to eq( 'superspeed' )
20
- expect( subject['dash'] ) .to eq( 'superspeed' )
21
- end
22
-
23
- it 'does not allow indifferent access for a key that was not a string or symbol' do
24
- expect( subject[42] ) .to eq( 'meaning of life...' )
25
- expect( subject[:'42'] ) .to be( nil )
26
- expect( subject['42'] ) .to be( nil )
27
- end
28
- end
29
-
30
- context 'nested hashes' do
31
- let(:hash) { {
32
- 'violet' => 'invisibility',
33
- 'spam' => {
34
- 'eggs' => 'breakfast'
35
- }
36
- } }
37
-
38
- it 'allows indifferent access into sub-hashes' do
39
- expect( subject[:spam][:eggs]).to eq('breakfast')
40
- expect( subject['spam']['eggs']).to eq('breakfast')
41
- expect( subject['spam'][:eggs]).to eq('breakfast')
42
- expect( subject[:spam]['eggs']).to eq('breakfast')
43
- end
44
- end
45
-
46
- context 'assignment' do
47
- let(:hash) { {} }
48
-
49
- it 'allows assignment by string and symbol' do
50
- subject[:unicorns] = 'rainbows'
51
- subject['vampires'] = 'sparkly'
52
-
53
- expect(subject['unicorns']).to eq('rainbows')
54
- expect(subject[:vampires]).to eq('sparkly')
55
- end
56
-
57
- it 'wraps hashes on assignment' do
58
- subject[:srsly] = { 'omg' => { 'wtf' => 'bbq' } }
59
-
60
- expect( subject[:srsly][:omg][:wtf] ).to eq( 'bbq' )
61
- expect( subject['srsly']['omg']['wtf'] ).to eq( 'bbq' )
62
- end
63
- end
64
- end