trice 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a8941c189a7c1758706c4f3b6b1a6c89b223e3ea
4
+ data.tar.gz: 980ad43b5c5df52c0ad1023623237cf268f10d88
5
+ SHA512:
6
+ metadata.gz: b74981f6f7da1ba1d29ff3b0e85148e5086d1ba03983cddcfeed7412b4bbf3094f387ffa0dbb265fe238f442949ee1d4b3ae0203654e5cbd61c3e277845064b0
7
+ data.tar.gz: 0b2c51d25d9927fbd22d141bb3da90e6e24f1c384e4fcf9aad1e79211ffcc0c0b3b9b43364b97182145d934b81e64ea825af56d2631a9a7a04705bb04052fdd0
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ spec/support/fake_rails_app/log/*
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format progress
2
+ --order random
3
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.4
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in trice.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Cookpad Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,177 @@
1
+ # Trice
2
+
3
+ Provides **reference time** concept to application. Use it instead of ad-hoc `Time.now`.
4
+
5
+ ### Setting consistent reference time
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'trice'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ ```
18
+ $ bundle
19
+ ```
20
+
21
+ ## Usage
22
+
23
+ ### With Rails controller
24
+
25
+ This gem aims to serve consistency of time handling using refrence time to Rails application.
26
+ The layer which should set reference time is controller layer, because reference time is one of external input.
27
+
28
+ Include `Trice::ControllerMethods` to your controller
29
+
30
+ ```ruby
31
+ class ApplicationController < AC::Base
32
+ include Trice::ControllerMethods
33
+ end
34
+ ```
35
+
36
+ Then your controller and view gets an accessor method to access consistent time object.
37
+
38
+ - `requested_at`: returns timestamp of action invoked (or stubbed timestamp, see below).
39
+
40
+ ### Include helper module outside of controller
41
+
42
+ Inlude `Trice::ReferenceTime` add `#reference_time` method to lookup current reference time.
43
+
44
+ Use it in Rails model.
45
+
46
+ ```ruby
47
+ class MyWork
48
+ include Trice::ReferenceTime
49
+
50
+ def do!(at: nil)
51
+ self.done_at = at || reference_time
52
+ end
53
+ end
54
+ ```
55
+
56
+ ### Setting consistent reference time
57
+
58
+ Set reference time with `Trice.refrence_time = _time_` or `Trice.with_refrence_time(_time_, &block)`.
59
+ Accessible by `Trice.reference_time`..
60
+
61
+ ```ruby
62
+ p Time.now
63
+ => 2016-02-01 11:25:37 +0900
64
+
65
+ Trice.with_refrence_time = Time.iso8601('2016-02-01T09:00:00Z')
66
+ p Trice.reference_time
67
+ # => 2016-02-01 09:00:00 UTC
68
+
69
+ Trice.with_refrence_time(Time.iso8601('2016-02-01T10:00:00Z')) do
70
+ p Trice.reference_time
71
+ # => 2016-02-01 10:00:00 UTC
72
+ end
73
+
74
+ Trice.with_refrence_time = nil
75
+ p Trice.reference_time
76
+ # => raise Trice::NoReferenceTime
77
+ ```
78
+
79
+ The time is stored in thread local variable.
80
+
81
+ ## Time Stubbing
82
+
83
+ Trice allows you to stub reference time to run travelled time-testing and / or previewing your app in future time.
84
+
85
+ Set `_requested_at=<timish>` query parameter like below
86
+ ```
87
+ $ curl https://example.com/campaigns/12345?_requested_at=20160215130
88
+ ```
89
+
90
+ Or can set HTTP header, useful for tests.
91
+ ```
92
+ X-REQUESTED-AT: 2016-02-15T13:00:00+09:00
93
+ ```
94
+
95
+ Value format, which specified both query parameter and header, should be `Time.parsse` parasable.
96
+
97
+ #### Enable/Disable stubbing
98
+
99
+ Toggle requested at stubbing in `config/initializers`. The default is below, enabled unelss `Rails.env.production?`.
100
+
101
+ ```ruby
102
+ Trice.support_performing_at_stubbing = !Rails.env.production?
103
+ ```
104
+
105
+ Setting callable object let you choice enable/disable dinamically by seeing request.
106
+
107
+ ```ruby
108
+ our_office_network = IPAddr.new('203.0.113.0/24')
109
+
110
+ Trice.support_performing_at_stubbing = ->(controller) {
111
+ next true unless Rails.env.production?
112
+
113
+ our_office_network.include?(controller.request.remote_ip)
114
+ }
115
+ ```
116
+
117
+ ## Test helpers
118
+
119
+ There is a test helper method for feature spec.
120
+
121
+ ```ruby
122
+ RSpec.configure do |config|
123
+ config.extend Trice::SpecHelper
124
+ end
125
+ ```
126
+
127
+ I recommend to pass reference time to a modle by method and/or constructor argument because reference time is an external input, should be handled controller layer.
128
+ But sometimes it is required from deep inside of model logics and tests for them.
129
+
130
+ Model unit spec has `with_refrence_time` and `set_now_to_reference_time` declarition method to set `Trice.reference_time` in an example.
131
+
132
+ ```ruby
133
+ describe MyModel do
134
+ let(:reference_time) { Time.zone.parse('2016/02/03 12:00') }
135
+ context do
136
+ set_reference_time { refrence_time }
137
+
138
+ let(:model) { MyModel.find_by_something(key) }
139
+
140
+ specify do
141
+ # can accessible `reference_time` in MyModel#do_something
142
+ expect { model.do_something }.not_to raise(Trice::NoReferenceTime)
143
+ end
144
+ end
145
+ end
146
+ ```
147
+
148
+ Feature specs (or othre Capybara based E2E tests) also has helper method using stubbing mechanism. `stub_requested_at <timish>` set `X-Trice-Requested-At` automatically.
149
+
150
+ ```ruby
151
+ context 'on ひな祭り day' do
152
+ stub_requested_at Time.zone.parse('2016-03-03 10:00')
153
+
154
+ scenario 'See Hinamatsuri special banner at 3/3 request' do
155
+ visit root_path
156
+ within '#custom-header' do
157
+ expect(page).to contain 'ひな祭り'
158
+ end
159
+ end
160
+ end
161
+ ```
162
+
163
+ ## Development
164
+
165
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
166
+
167
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
168
+
169
+ ## Contributing
170
+
171
+ Bug reports and pull requests are welcome on GitHub at https://github.com/cookpad/trice.
172
+
173
+
174
+ ## License
175
+
176
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
177
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "trice"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,19 @@
1
+ require 'active_support/all'
2
+
3
+ require 'trice/controller_methods'
4
+ require 'trice/errors'
5
+ require 'trice/reference_time'
6
+ require 'trice/reference_time_accessor'
7
+ require 'trice/version'
8
+
9
+ if defined?(Rails)
10
+ require 'trice/railtie'
11
+ end
12
+
13
+ module Trice
14
+ autoload 'SpecHelper', 'trice/spec_helper'
15
+
16
+ extend ReferenceTimeAccessor
17
+
18
+ mattr_accessor :support_requested_at_stubbing
19
+ end
@@ -0,0 +1,23 @@
1
+ require 'trice/controller_methods/reference_time_assignment'
2
+ require 'trice/controller_methods/stub_configuration'
3
+
4
+ module Trice
5
+ module ControllerMethods
6
+
7
+ extend ActiveSupport::Concern
8
+
9
+ included do |controller|
10
+ if controller.ancestors.include?(ActionController::Base)
11
+ config = StubConfiguration.new(Trice.support_requested_at_stubbing)
12
+ prepend_around_action ReferenceTimeAssignment.new(config)
13
+
14
+ helper_method :requested_at
15
+ hide_action :requested_at
16
+ end
17
+ end
18
+
19
+ def requested_at
20
+ Trice.reference_time
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,39 @@
1
+ module Trice
2
+ module ControllerMethods
3
+ class ReferenceTimeAssignment
4
+
5
+ QUERY_STUB_KEY = '_requested_at'.freeze
6
+ HEADER_STUB_KEY = 'X-REQUESTED-AT'.freeze
7
+
8
+ def initialize(config)
9
+ @stub_configuration = config
10
+ end
11
+
12
+ def around(controller, &action)
13
+ t = determine_requested_at(controller)
14
+
15
+ controller.request.env['trice.reference_time'] = t
16
+
17
+ Trice.with_reference_time(t, &action)
18
+ end
19
+
20
+ private
21
+
22
+ def determine_requested_at(controller)
23
+ if @stub_configuration.stubbable?(controller)
24
+ extract_requested_at(controller.request) || Time.now
25
+ else
26
+ Time.now
27
+ end
28
+ end
29
+
30
+ def extract_requested_at(request)
31
+ if request.params[QUERY_STUB_KEY]
32
+ Time.zone.parse(request.params[QUERY_STUB_KEY])
33
+ elsif request.headers[HEADER_STUB_KEY]
34
+ Time.zone.parse(request.headers[HEADER_STUB_KEY])
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,14 @@
1
+ module Trice
2
+ module ControllerMethods
3
+ class StubConfiguration
4
+ def initialize(condition)
5
+ @condition = condition
6
+ @is_callable = @condition.respond_to?(:call)
7
+ end
8
+
9
+ def stubbable?(request)
10
+ @is_callable ? @condition.call(request) : !!@condition
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,4 @@
1
+ module Trice
2
+ class Error < StandardError; end
3
+ class NoReferenceTime < Error; end
4
+ end
@@ -0,0 +1,7 @@
1
+ module Trice
2
+ class Railtie < ::Rails::Railtie
3
+ initializer 'trice' do |app|
4
+ Trice.support_requested_at_stubbing = !Rails.env.production?
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module Trice
2
+ module ReferenceTime
3
+ def reference_time
4
+ Trice.reference_time
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,11 @@
1
+ require 'trice/repository'
2
+
3
+ module Trice
4
+ module ReferenceTimeAccessor
5
+ def self.repository
6
+ @repository ||= Repository.new
7
+ end
8
+
9
+ delegate :reference_time=, :reference_time, :with_reference_time, to: 'Trice::ReferenceTimeAccessor.repository'
10
+ end
11
+ end
@@ -0,0 +1,43 @@
1
+ module Trice
2
+ class Repository
3
+ def initialize
4
+ @backend = ThreadLocalBackend.new('trice.reference_time'.freeze)
5
+ end
6
+
7
+ def reference_time=(time)
8
+ unless time.nil? || time.acts_like?(:time)
9
+ raise ArgumentError, "#{time.inspect} is not behave like time"
10
+ end
11
+ @backend.set(time)
12
+ end
13
+
14
+ def reference_time
15
+ @backend.get || raise(NoReferenceTime)
16
+ end
17
+
18
+ def with_reference_time(time, &block)
19
+ original = @backend.get
20
+
21
+ begin
22
+ self.reference_time = time
23
+ yield time
24
+ ensure
25
+ self.reference_time = original
26
+ end
27
+ end
28
+ end
29
+
30
+ class ThreadLocalBackend
31
+ def initialize(key)
32
+ @key = key
33
+ end
34
+
35
+ def set(time)
36
+ Thread.current[@key] = time
37
+ end
38
+
39
+ def get
40
+ Thread.current[@key]
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,36 @@
1
+ require 'trice/errors'
2
+
3
+ module Trice
4
+ module SpecHelper
5
+ def self.extract_time(runtime, static_time, block)
6
+ block ? runtime.instance_eval(&block) : static_time
7
+ end
8
+
9
+ def set_reference_time(static_time = nil, &block)
10
+ around(:each) do |ex|
11
+ time = SpecHelper.extract_time(self, static_time, block)
12
+
13
+ Trice.with_reference_time(time) { ex.run }
14
+ end
15
+ end
16
+
17
+ def set_now_as_reference_time
18
+ set_reference_time(Time.now)
19
+ end
20
+
21
+ def stub_requested_at(static_time = nil, &block)
22
+ before(:each) do |ex|
23
+ time = SpecHelper.extract_time(self, static_time, block)
24
+
25
+ case ex.metadata[:type]
26
+ when :controller
27
+ request.headers['X-Requested-At'] = time.iso8601
28
+ when :feature
29
+ page.driver.header 'X-Requested-At', time.iso8601
30
+ else
31
+ raise Trice::TestStubbingNotSupported, "Test stubbing for type: #{ex.metadata[:type]} is not supported"
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,3 @@
1
+ module Trice
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'trice/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "trice"
8
+ spec.version = Trice::VERSION
9
+ spec.authors = ["moro"]
10
+ spec.email = ["kyosuke-morohashi@cookpad.com"]
11
+
12
+ spec.summary = %q{Provides reference time concept to application. Use it instead of ad-hoc `Time.now`}
13
+ spec.homepage = "https://github.com/cookpad/trice"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "activesupport", "~> 4.0"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.11"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.0"
26
+ spec.add_development_dependency "rspec-rails", "~> 3.0"
27
+ spec.add_development_dependency "actionpack", "~> 4.0"
28
+ spec.add_development_dependency "pry"
29
+ end
metadata ADDED
@@ -0,0 +1,164 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: trice
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - moro
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-02-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.11'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.11'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec-rails
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: actionpack
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '4.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '4.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description:
112
+ email:
113
+ - kyosuke-morohashi@cookpad.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".travis.yml"
121
+ - Gemfile
122
+ - LICENSE.txt
123
+ - README.md
124
+ - Rakefile
125
+ - bin/console
126
+ - bin/setup
127
+ - lib/trice.rb
128
+ - lib/trice/controller_methods.rb
129
+ - lib/trice/controller_methods/reference_time_assignment.rb
130
+ - lib/trice/controller_methods/stub_configuration.rb
131
+ - lib/trice/errors.rb
132
+ - lib/trice/railtie.rb
133
+ - lib/trice/reference_time.rb
134
+ - lib/trice/reference_time_accessor.rb
135
+ - lib/trice/repository.rb
136
+ - lib/trice/spec_helper.rb
137
+ - lib/trice/version.rb
138
+ - trice.gemspec
139
+ homepage: https://github.com/cookpad/trice
140
+ licenses:
141
+ - MIT
142
+ metadata: {}
143
+ post_install_message:
144
+ rdoc_options: []
145
+ require_paths:
146
+ - lib
147
+ required_ruby_version: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ required_rubygems_version: !ruby/object:Gem::Requirement
153
+ requirements:
154
+ - - ">="
155
+ - !ruby/object:Gem::Version
156
+ version: '0'
157
+ requirements: []
158
+ rubyforge_project:
159
+ rubygems_version: 2.4.5.1
160
+ signing_key:
161
+ specification_version: 4
162
+ summary: Provides reference time concept to application. Use it instead of ad-hoc
163
+ `Time.now`
164
+ test_files: []