sinatra_bicyclist 0.1.0 → 0.2.0.pre1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6dd623dcfeee2a86c6f472b1801b2c07f9e2c9f0
4
- data.tar.gz: 64954cb95a7eeb1493fc5b649585eff62100b3b1
3
+ metadata.gz: d5c2a1d3397cf39b0a76a57869621bcae9ef7449
4
+ data.tar.gz: 6d9f76a78fbf2eff1fcba489ce496452431c1b23
5
5
  SHA512:
6
- metadata.gz: 3d50bfcc4d50c04ab38d240144d64703ee8c02d1befdccf4c83980bbf041fcc24002cb566fd455844857a59c5e23f3f3dbe844baba713fc30480d0cea1d9bb96
7
- data.tar.gz: 33e8eddc21a9ae711176fff87920184e2d5cfce469001a46d696eda5dd417fa4cb9cefdb461a8389848889e56bd40e95e463eb48154dd5598da0db9954aa722f
6
+ metadata.gz: a0e5f2df39d9fc6c165569eb4e3d4ee6b0c7d8483839372f0eaa85a1a0712642f6df2d823051f03b5c75d33a1911e2557b11b59a6c563d8739518ad552ab2a12
7
+ data.tar.gz: 8974ef82cdfae4a3df2c8bd31c64c1289f8bdb2c8ae47335fc58c291aeb02e00324e521475be37a77cbd27eba26b30871c40038bc177a612a35b6f56d97b06d2
data/CHANGELOG.md CHANGED
@@ -1,6 +1,13 @@
1
1
  # Change Log
2
2
  All notable changes to this project will be documented in this file.
3
3
 
4
+ ## 0.2.0 - 2014-10-07
5
+ ### Added
6
+ - added `Sinatra::Bicyclist::Cycle` that handles cycling the session
7
+
8
+ ### Changed
9
+ - moved methods that require session from `Sinatra::Bicyclist::Cycler` to `Sinatra::Bicyclist::Cycle`
10
+
4
11
  ## 0.1.0 - 2014-09-30
5
12
  ### Added
6
13
  - support for more cycle groups
@@ -0,0 +1,40 @@
1
+ module Sinatra
2
+ module Bicyclist
3
+ class Cycle
4
+ def initialize(session)
5
+ @session = session
6
+ end
7
+
8
+ def page_index
9
+ @session[:_cycle_page_index] ||= -1
10
+ end
11
+
12
+ def next_index
13
+ page_index.succ
14
+ end
15
+
16
+ def page_index=(value)
17
+ @session[:_cycle_page_index] = value
18
+ end
19
+
20
+ def reset_index
21
+ @session[:_cycle_page_index] = -1
22
+ end
23
+
24
+ def last_cycle
25
+ @session[:_last_cycle].to_s
26
+ end
27
+
28
+ def page(pages)
29
+ index = next_route(pages)
30
+ pages[index]
31
+ end
32
+
33
+ def next_route(pages)
34
+ routes = pages.length
35
+
36
+ self.page_index = next_index % routes
37
+ end
38
+ end
39
+ end
40
+ end
@@ -1,10 +1,13 @@
1
+ require_relative 'cycle'
2
+
1
3
  module Sinatra
2
4
  module Bicyclist
3
5
  class Cycler
4
6
  attr_accessor :duration
5
7
 
6
- def initialize(settings)
8
+ def initialize(settings, cycle_class = Bicyclist::Cycle)
7
9
  @settings = settings
10
+ @cycle_class = cycle_class
8
11
  end
9
12
 
10
13
  def routes
@@ -32,23 +35,10 @@ module Sinatra
32
35
  has_groups?
33
36
  end
34
37
 
35
- def page_index(session)
36
- page_index = session[:_cycle_page_index] || -1
37
- session[:_cycle_page_index] = page_index + 1
38
- end
39
-
40
- def random_route(session, group)
41
- number_of_routes = pages(group).length
42
- pages(group)[page_index(session) % number_of_routes]
43
- end
44
-
45
- def reset_index(session)
46
- session[:_cycle_page_index] = -1
47
- end
48
-
49
38
  def page(session, group = nil)
50
- reset_index(session) if group.to_s != session[:_last_cycle].to_s
51
- page = random_route(session, group)
39
+ cycle = @cycle_class.new(session)
40
+ cycle.reset_index unless cycle.last_cycle == group.to_s
41
+ page = cycle.page pages(group)
52
42
  session[:_cycle] = group || (has_groups? && page)
53
43
  page
54
44
  end
@@ -1,5 +1,5 @@
1
1
  module Sinatra
2
2
  module Bicyclist
3
- VERSION = '0.1.0'
3
+ VERSION = '0.2.0.pre1'
4
4
  end
5
5
  end
@@ -1,6 +1,7 @@
1
1
  require 'sinatra'
2
2
  require 'sinatra/bicyclist'
3
3
 
4
+ set :environment, :test
4
5
  set :routes_to_cycle_through, {
5
6
  one: [:foo],
6
7
  two: [:bar, :baz]
@@ -15,7 +16,6 @@ RSpec.describe Sinatra::Application do
15
16
  it { expect(response.status).to eq(302) }
16
17
  it { expect(response.headers).to include('Location' => 'http://example.org/_cycle/one') }
17
18
 
18
-
19
19
  context 'two' do
20
20
  before { get '/_cycle/two' }
21
21
 
@@ -0,0 +1,45 @@
1
+ RSpec.describe Sinatra::Bicyclist::Cycle do
2
+ let(:routes) { ['a', 'b'] }
3
+ let(:session) { Hash.new }
4
+ subject(:cycle) { described_class.new(session) }
5
+
6
+ it 'cycles page' do
7
+ page = cycle.page(routes)
8
+ expect(page).to eq('a')
9
+ end
10
+
11
+ it 'cycles all page' do
12
+ pages = 2.times.map { cycle.page(routes) }
13
+ expect(pages).to eq(['a', 'b'])
14
+ end
15
+
16
+ it 'has -1 index' do
17
+ expect(cycle.page_index).to eq(-1)
18
+ end
19
+
20
+ context 'cycled page' do
21
+ let!(:page) { cycle.page(routes) }
22
+ before { page }
23
+
24
+ it 'changes the index' do
25
+ expect(cycle.page_index).to eq(0)
26
+ end
27
+ end
28
+
29
+ context 'looped page' do
30
+ let(:first) { cycle.page(routes) }
31
+ let(:second) { cycle.page(routes) }
32
+ let(:third) { cycle.page(routes) }
33
+
34
+ it 'loops index' do
35
+ expect(cycle.page_index).to eq(-1)
36
+ expect(first).to eq('a')
37
+ expect(cycle.page_index).to eq(0)
38
+ expect(second).to eq('b')
39
+ expect(cycle.page_index).to eq(1)
40
+ expect(third).to eq('a')
41
+ expect(cycle.page_index).to eq(0)
42
+ end
43
+ end
44
+
45
+ end
File without changes
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra_bicyclist
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0.pre1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Lavrisha
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-09-30 00:00:00.000000000 Z
12
+ date: 2014-10-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sinatra
@@ -87,10 +87,13 @@ files:
87
87
  - examples/config.ru
88
88
  - examples/modular_application.rb
89
89
  - lib/sinatra/bicyclist.rb
90
+ - lib/sinatra/bicyclist/cycle.rb
90
91
  - lib/sinatra/bicyclist/cycler.rb
91
92
  - lib/sinatra/bicyclist/version.rb
92
93
  - sinatra_cyclist.gemspec
93
94
  - spec/byciclist_spec.rb
95
+ - spec/cycle_spec.rb
96
+ - spec/cycler_spec.rb
94
97
  - spec/spec_helper.rb
95
98
  - spec/support/rack.rb
96
99
  homepage: https://github.com/mikz/sinatra_bicyclist
@@ -107,9 +110,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
107
110
  version: '0'
108
111
  required_rubygems_version: !ruby/object:Gem::Requirement
109
112
  requirements:
110
- - - ">="
113
+ - - ">"
111
114
  - !ruby/object:Gem::Version
112
- version: '0'
115
+ version: 1.3.1
113
116
  requirements: []
114
117
  rubyforge_project:
115
118
  rubygems_version: 2.2.2
@@ -118,5 +121,7 @@ specification_version: 4
118
121
  summary: Sinatra can ride a bicycle over and over and over again
119
122
  test_files:
120
123
  - spec/byciclist_spec.rb
124
+ - spec/cycle_spec.rb
125
+ - spec/cycler_spec.rb
121
126
  - spec/spec_helper.rb
122
127
  - spec/support/rack.rb