microscope 0.6.1 → 0.6.2

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: e1d8b3f9466f8a555fcd4133c7cc541bfbe2e437
4
- data.tar.gz: 0370c547769ffd68344c9a5746e0ab2f02d2ba54
3
+ metadata.gz: 33dac82bbfb0ee0add9e83ad93ebea6525f4f427
4
+ data.tar.gz: bbdc35dd0547369edac2fbb0877b42c970042584
5
5
  SHA512:
6
- metadata.gz: 4328b4effc602616f1c6c2e80cf31213beb914a810a373d81f7c1142552fafa77bb8c457f286bd7359d5bdd0d702d80f39614dda799c61033ca00e9742e29026
7
- data.tar.gz: 6da051c7e8ea9c78a947bb5eb6938ab57c61640afeb7d11bf323a2f1d0a10f24e1414d6ffc30d68a4cc4713d520052705e325c700c49746a7eabdb115a8c2cf9
6
+ metadata.gz: 3ec8faf2b9878a7fef29fce73a13720b54a6befff22580959252506240851549f05e79bd009c4487cf29b200f43825c9f1f44ae9c77f93a1db29423fc4aa8f1b
7
+ data.tar.gz: 5e36fb8f4f6819d721cb442cec88ec4ceffb7e430df68631f0cb59e8bc6069d9fb1d5975bdae1bd812f684b294fa1b3416e2f65fa7433c65bd9add25829dec2f
data/.rubocop.yml ADDED
@@ -0,0 +1,50 @@
1
+ AllCops:
2
+ Include:
3
+ - microscope.gemspec
4
+
5
+ Documentation:
6
+ Enabled: false
7
+
8
+ Encoding:
9
+ Enabled: false
10
+
11
+ LineLength:
12
+ Max: 200
13
+
14
+ ClassLength:
15
+ Max: 200
16
+
17
+ AccessModifierIndentation:
18
+ EnforcedStyle: outdent
19
+
20
+ IfUnlessModifier:
21
+ Enabled: false
22
+
23
+ CaseIndentation:
24
+ IndentWhenRelativeTo: case
25
+ IndentOneStep: true
26
+
27
+ MethodLength:
28
+ CountComments: false
29
+ Max: 20
30
+
31
+ SignalException:
32
+ Enabled: false
33
+
34
+ ColonMethodCall:
35
+ Enabled: false
36
+
37
+ AsciiComments:
38
+ Enabled: false
39
+
40
+ Lambda:
41
+ Enabled: false
42
+
43
+ RegexpLiteral:
44
+ Enabled: false
45
+
46
+ AssignmentInCondition:
47
+ Enabled: false
48
+
49
+ ClassAndModuleChildren:
50
+ Enabled: false
data/.travis.yml CHANGED
@@ -2,11 +2,9 @@ language: ruby
2
2
 
3
3
  rvm:
4
4
  - 2.0.0
5
- - 1.9.3
6
5
 
7
6
  gemfile:
8
- - gemfiles/Gemfile.activerecord-4.0
9
- - gemfiles/Gemfile.activerecord-3.2.x
7
+ - gemfiles/Gemfile.activerecord-4.1
10
8
 
11
9
  env:
12
10
  - DB_ADAPTER=sqlite3
data/README.md CHANGED
@@ -5,11 +5,11 @@
5
5
  <br />
6
6
  Microscope adds useful scopes targeting ActiveRecord <code>boolean</code>, <code>date</code> and <code>datetime</code> fields.
7
7
  <br /><br />
8
- <a href="https://rubygems.org/gems/microscope"><img src="https://badge.fury.io/rb/microscope.png" /></a>
9
- <a href="https://codeclimate.com/github/mirego/microscope"><img src="https://codeclimate.com/github/mirego/microscope.png" /></a>
10
- <a href='https://coveralls.io/r/mirego/microscope?branch=master'><img src='https://coveralls.io/repos/mirego/microscope/badge.png?branch=master' /></a>
11
- <a href='https://gemnasium.com/mirego/microscope'><img src="https://gemnasium.com/mirego/microscope.png" /></a>
12
- <a href="https://travis-ci.org/mirego/microscope"><img src="https://travis-ci.org/mirego/microscope.png?branch=master" /></a>
8
+ <a href="https://rubygems.org/gems/microscope"><img src="http://img.shields.io/gem/v/microscope.svg" /></a>
9
+ <a href="https://codeclimate.com/github/mirego/microscope"><img src="http://img.shields.io/codeclimate/github/mirego/microscope.svg" /></a>
10
+ <a href='https://coveralls.io/r/mirego/microscope?branch=master'><img src="http://img.shields.io/coveralls/mirego/microscope.svg" /></a>
11
+ <a href='https://gemnasium.com/mirego/microscope'><img src="http://img.shields.io/gemnasium/mirego/microscope.svg" /></a>
12
+ <a href="https://travis-ci.org/mirego/microscope"><img src="http://img.shields.io/travis/mirego/microscope.svg" /></a>
13
13
  </p>
14
14
 
15
15
  ---
@@ -51,6 +51,9 @@ Event.expired_before(2.months.ago)
51
51
  Event.expired_before_now
52
52
  # SELECT * FROM `events` where `events`.`expired_at` < '2013-07-05 15:43:42'
53
53
 
54
+ Event.expired_after_or_now
55
+ # SELECT * FROM `events` where `events`.`expired_at` >= '2013-07-05 15:43:42'
56
+
54
57
  Event.expired_after_or_at(2.months.from_now)
55
58
  # SELECT * FROM `events` where `events`.`expired_at` >= '2013-09-05 15:43:42'
56
59
 
@@ -82,9 +85,19 @@ event.not_archived? # => false
82
85
  event = Event.unarchived.first
83
86
  event.archived? # => false
84
87
 
85
- event.archive!
88
+ event.mark_as_archived
89
+ event.archived? # => true
90
+ event.archived_at # => 2013-07-05 15:43:42
91
+ event.reload
92
+ event.archived? # => false
93
+ event.archived_at # => nil
94
+
95
+ event.archive! # (same as #mark_as_archived but save the record immediately)
96
+ event.archived? # => true
97
+ event.archived_at # => 2013-07-05 15:43:42
98
+ event.reload
86
99
  event.archived? # => true
87
- event.archived_at # => 2013-07-05 15:43:44 (Time.now)
100
+ event.archived_at # => 2013-07-05 15:43:42
88
101
  ```
89
102
 
90
103
  ### Options
@@ -2,4 +2,4 @@ source 'https://rubygems.org'
2
2
 
3
3
  gemspec path: '../'
4
4
 
5
- gem 'activerecord', '~> 3.2.0'
5
+ gem 'activerecord', '~> 4.1.0'
data/lib/microscope.rb CHANGED
@@ -1,18 +1,20 @@
1
- require "microscope/version"
1
+ require 'microscope/version'
2
2
 
3
3
  require 'ostruct'
4
4
  require 'active_record'
5
5
  require 'active_support'
6
6
 
7
- require "microscope/scope"
8
- require "microscope/scope/boolean_scope"
9
- require "microscope/scope/datetime_scope"
10
- require "microscope/scope/date_scope"
7
+ require 'microscope/utils'
11
8
 
12
- require "microscope/instance_method"
13
- require "microscope/instance_method/boolean_instance_method"
14
- require "microscope/instance_method/datetime_instance_method"
15
- require "microscope/instance_method/date_instance_method"
9
+ require 'microscope/scope'
10
+ require 'microscope/scope/boolean_scope'
11
+ require 'microscope/scope/datetime_scope'
12
+ require 'microscope/scope/date_scope'
13
+
14
+ require 'microscope/instance_method'
15
+ require 'microscope/instance_method/boolean_instance_method'
16
+ require 'microscope/instance_method/datetime_instance_method'
17
+ require 'microscope/instance_method/date_instance_method'
16
18
 
17
19
  module Microscope
18
20
  IRREGULAR_VERBS_FILE = File.expand_path('../../data/irregular_verbs.yml', __FILE__)
@@ -13,31 +13,8 @@ module Microscope
13
13
  def self.inject_instance_methods(model, fields, options = {})
14
14
  fields.each do |field|
15
15
  scope = "#{field.type.to_s.camelize}InstanceMethod"
16
-
17
- if Microscope::InstanceMethod.const_defined?(scope)
18
- "Microscope::InstanceMethod::#{scope}".constantize.new(model, field).apply
19
- end
20
- end
21
- end
22
-
23
- # Convert a past participle to its infinitive form
24
- def self.past_participle_to_infinitive(key)
25
- *key, participle = key.split('_')
26
-
27
- infinitive = if Microscope.special_verbs.include?(participle)
28
- Microscope.special_verbs[participle]
29
- elsif participle =~ /ed$/
30
- participle.sub(/d$/, '')
31
- else
32
- participle
16
+ "Microscope::InstanceMethod::#{scope}".constantize.new(model, field).apply if const_defined?(scope)
33
17
  end
34
-
35
- (key << infinitive).join('_')
36
- end
37
-
38
- # Convert a value to its boolean representation
39
- def self.value_to_boolean(value)
40
- ![nil, false, 0, '0', 'f', 'F', 'false', 'FALSE'].include?(value.presence)
41
18
  end
42
19
  end
43
20
  end
@@ -2,7 +2,7 @@ module Microscope
2
2
  class InstanceMethod
3
3
  class BooleanInstanceMethod < InstanceMethod
4
4
  def apply
5
- infinitive_verb = self.class.past_participle_to_infinitive(field.name)
5
+ infinitive_verb = Microscope::Utils.past_participle_to_infinitive(field.name)
6
6
 
7
7
  model.class_eval <<-RUBY, __FILE__, __LINE__ + 1
8
8
  define_method "#{infinitive_verb}!" do
@@ -9,49 +9,63 @@ module Microscope
9
9
  end
10
10
 
11
11
  def apply
12
- return unless @field_name =~ @cropped_field_regex
12
+ @cropped_field = field.name.gsub(@cropped_field_regex, '')
13
+ @infinitive_verb = Microscope::Utils.past_participle_to_infinitive(cropped_field)
13
14
 
14
- cropped_field = field.name.gsub(@cropped_field_regex, '')
15
- infinitive_verb = self.class.past_participle_to_infinitive(cropped_field)
15
+ model.class_eval(apply_methods) if @field_name =~ @cropped_field_regex
16
+ end
16
17
 
17
- model.class_eval <<-RUBY, __FILE__, __LINE__ + 1
18
- define_method "#{cropped_field}?" do
19
- value = send("#{field.name}")
20
- !value.nil? && value <= #{@now}
21
- end
18
+ protected
22
19
 
23
- define_method "#{cropped_field}=" do |value|
24
- if Microscope::InstanceMethod.value_to_boolean(value)
20
+ def apply_methods
21
+ apply_assignment_methods + apply_bang_methods + apply_predicate_methods + apply_aliases
22
+ end
23
+
24
+ def apply_assignment_methods
25
+ <<-RUBY
26
+ define_method '#{@cropped_field}=' do |value|
27
+ if Microscope::Utils.value_to_boolean(value)
25
28
  self.#{field.name} ||= #{@now}
26
29
  else
27
30
  self.#{field.name} = nil
28
31
  end
29
32
  end
30
33
 
31
- define_method "not_#{cropped_field}?" do
32
- !#{cropped_field}?
33
- end
34
- alias_method 'un#{cropped_field}?', 'not_#{cropped_field}?'
34
+ define_method('mark_as_#{@cropped_field}') { self.#{@cropped_field}= true }
35
+ define_method('mark_as_not_#{@cropped_field}') { self.#{@cropped_field}= false }
36
+ RUBY
37
+ end
35
38
 
36
- define_method "#{infinitive_verb}!" do
37
- send("#{field.name}=", #{@now})
39
+ def apply_bang_methods
40
+ <<-RUBY
41
+ define_method '#{@infinitive_verb}!' do
42
+ send('#{field.name}=', #{@now})
38
43
  save!
39
44
  end
40
45
 
41
- define_method "not_#{infinitive_verb}!" do
42
- send("#{field.name}=", nil)
46
+ define_method 'not_#{@infinitive_verb}!' do
47
+ send('#{field.name}=', nil)
43
48
  save!
44
49
  end
45
- alias_method 'un#{infinitive_verb}!', 'not_#{infinitive_verb}!'
50
+ RUBY
51
+ end
46
52
 
47
- define_method "mark_as_#{cropped_field}" do
48
- self.#{cropped_field}= true
53
+ def apply_predicate_methods
54
+ <<-RUBY
55
+ define_method '#{@cropped_field}?' do
56
+ value = send('#{field.name}')
57
+ !value.nil? && value <= #{@now}
49
58
  end
50
59
 
51
- define_method "mark_as_not_#{cropped_field}" do
52
- self.#{cropped_field}= false
53
- end
54
- alias_method 'mark_as_un#{cropped_field}', 'mark_as_not_#{cropped_field}'
60
+ define_method('not_#{@cropped_field}?') { !#{@cropped_field}? }
61
+ RUBY
62
+ end
63
+
64
+ def apply_aliases
65
+ <<-RUBY
66
+ alias_method 'un#{@infinitive_verb}!', 'not_#{@infinitive_verb}!'
67
+ alias_method 'mark_as_un#{@cropped_field}', 'mark_as_not_#{@cropped_field}'
68
+ alias_method 'un#{@cropped_field}?', 'not_#{@cropped_field}?'
55
69
  RUBY
56
70
  end
57
71
  end
@@ -19,10 +19,7 @@ module Microscope
19
19
  def self.inject_scopes(model, fields, options = {})
20
20
  fields.each do |field|
21
21
  scope = "#{field.type.to_s.camelize}Scope"
22
-
23
- if Microscope::Scope.const_defined?(scope)
24
- "Microscope::Scope::#{scope}".constantize.new(model, field).apply
25
- end
22
+ "Microscope::Scope::#{scope}".constantize.new(model, field).apply if const_defined?(scope)
26
23
  end
27
24
  end
28
25
  end
@@ -6,7 +6,9 @@ module Microscope
6
6
 
7
7
  @now = 'Date.today'
8
8
  @now_suffix = '_today'
9
+ @specific_suffix = '_on'
9
10
  @cropped_field_regex = /_on$/
11
+ @formatted_time = 'time.try(:strftime, \'%Y-%m-%d\')'
10
12
  end
11
13
  end
12
14
  end
@@ -6,46 +6,31 @@ module Microscope
6
6
 
7
7
  @now = 'Time.now'
8
8
  @now_suffix = '_now'
9
+ @specific_suffix = '_at'
9
10
  @cropped_field_regex = /_at$/
11
+ @formatted_time = 'time'
10
12
  end
11
13
 
12
14
  def apply
13
- return unless @field_name =~ @cropped_field_regex
14
-
15
- model.class_eval <<-RUBY, __FILE__, __LINE__ + 1
16
- #{before_scopes}
17
- #{after_scopes}
18
- #{between_scopes}
19
- #{boolean_scopes}
20
- RUBY
15
+ model.class_eval(apply_scopes) if @field_name =~ @cropped_field_regex
21
16
  end
22
17
 
23
18
  private
24
19
 
25
- def before_scopes
20
+ def apply_scopes
26
21
  <<-RUBY
27
- scope "#{cropped_field}_before", lambda { |time| where('#{quoted_field} < ?', time) }
28
- scope "#{cropped_field}_before_or_at", lambda { |time| where('#{quoted_field} <= ?', time) }
22
+ scope "#{cropped_field}_before", lambda { |time| where('#{quoted_field} < ?', #{@formatted_time}) }
23
+ scope "#{cropped_field}_before_or#{@specific_suffix}", lambda { |time| where('#{quoted_field} <= ?', #{@formatted_time}) }
29
24
  scope "#{cropped_field}_before#{@now_suffix}", lambda { where('#{quoted_field} < ?', #{@now}) }
30
- RUBY
31
- end
25
+ scope "#{cropped_field}_before_or#{@now_suffix}", lambda { where('#{quoted_field} <= ?', #{@now}) }
32
26
 
33
- def after_scopes
34
- <<-RUBY
35
- scope "#{cropped_field}_after", lambda { |time| where('#{quoted_field} > ?', time) }
36
- scope "#{cropped_field}_after_or_at", lambda { |time| where('#{quoted_field} >= ?', time) }
27
+ scope "#{cropped_field}_after", lambda { |time| where('#{quoted_field} > ?', #{@formatted_time}) }
28
+ scope "#{cropped_field}_after_or#{@specific_suffix}", lambda { |time| where('#{quoted_field} >= ?', #{@formatted_time}) }
37
29
  scope "#{cropped_field}_after#{@now_suffix}", lambda { where('#{quoted_field} > ?', #{@now}) }
38
- RUBY
39
- end
30
+ scope "#{cropped_field}_after_or#{@now_suffix}", lambda { where('#{quoted_field} >= ?', #{@now}) }
40
31
 
41
- def between_scopes
42
- <<-RUBY
43
32
  scope "#{cropped_field}_between", lambda { |range| where("#{@field_name}" => range) }
44
- RUBY
45
- end
46
33
 
47
- def boolean_scopes
48
- <<-RUBY
49
34
  scope "#{cropped_field}", lambda { where('#{quoted_field} IS NOT NULL AND #{quoted_field} <= ?', #{@now}) }
50
35
  scope "not_#{cropped_field}", lambda { where('#{quoted_field} IS NULL OR #{quoted_field} > ?', #{@now}) }
51
36
  scope "un#{cropped_field}", lambda { not_#{cropped_field} }
@@ -0,0 +1,23 @@
1
+ module Microscope
2
+ module Utils
3
+ # Convert a past participle to its infinitive form
4
+ def self.past_participle_to_infinitive(key)
5
+ *key, participle = key.split('_')
6
+
7
+ infinitive = participle
8
+
9
+ if Microscope.special_verbs.include?(participle)
10
+ infinitive = Microscope.special_verbs[participle]
11
+ elsif participle =~ /ed$/
12
+ infinitive = participle.sub(/d$/, '')
13
+ end
14
+
15
+ (key << infinitive).join('_')
16
+ end
17
+
18
+ # Convert a value to its boolean representation
19
+ def self.value_to_boolean(value)
20
+ ![nil, false, 0, '0', 'f', 'F', 'false', 'FALSE'].include?(value.presence)
21
+ end
22
+ end
23
+ end
@@ -1,3 +1,3 @@
1
1
  module Microscope
2
- VERSION = '0.6.1'
2
+ VERSION = '0.6.2'
3
3
  end
data/microscope.gemspec CHANGED
@@ -6,26 +6,28 @@ require 'microscope/version'
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = 'microscope'
8
8
  spec.version = Microscope::VERSION
9
- spec.authors = ["Simon Prévost", "Rémi Prévost"]
10
- spec.email = ["sprevost@mirego.com", "rprevost@mirego.com"]
9
+ spec.authors = ['Simon Prévost', 'Rémi Prévost']
10
+ spec.email = ['sprevost@mirego.com', 'rprevost@mirego.com']
11
11
  spec.description = 'Microscope adds useful scopes targeting ActiveRecord boolean and datetime fields.'
12
- spec.summary = 'Microscope adds useful scopes targeting ActiveRecord boolean and datetime fields.'
12
+ spec.summary = spec.description
13
13
  spec.homepage = 'https://github.com/mirego/microscope'
14
14
  spec.license = 'BSD 3-Clause'
15
15
 
16
- spec.files = `git ls-files`.split($/)
16
+ spec.files = `git ls-files`.split($RS)
17
17
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
- spec.require_paths = ["lib"]
19
+ spec.require_paths = ['lib']
20
20
 
21
21
  spec.add_dependency 'activesupport', '>= 3.0.0'
22
22
  spec.add_dependency 'activerecord', '>= 3.0.0'
23
23
 
24
24
  spec.add_development_dependency 'bundler', '~> 1.3'
25
25
  spec.add_development_dependency 'rake'
26
- spec.add_development_dependency 'rspec'
26
+ spec.add_development_dependency 'rspec', '3.0.0.rc1'
27
27
  spec.add_development_dependency 'sqlite3'
28
28
  spec.add_development_dependency 'pg'
29
29
  spec.add_development_dependency 'mysql2', '~> 0.3.13'
30
30
  spec.add_development_dependency 'coveralls'
31
+ spec.add_development_dependency 'rubocop', '0.20.1'
32
+ spec.add_development_dependency 'phare'
31
33
  end
@@ -79,7 +79,7 @@ describe Microscope::InstanceMethod::DateInstanceMethod do
79
79
 
80
80
  describe '#start!' do
81
81
  let(:stubbed_date) { Date.parse('2020-03-18 08:00:00') }
82
- before { Date.stub(:today).and_return(stubbed_date) }
82
+ before { allow(Date).to receive(:today).and_return(stubbed_date) }
83
83
 
84
84
  let(:event) { Event.create(started_on: nil) }
85
85
  it { expect { event.start! }.to change { event.reload.started_on }.from(nil).to(stubbed_date) }
@@ -95,7 +95,7 @@ describe Microscope::InstanceMethod::DateInstanceMethod do
95
95
 
96
96
  describe '#mark_as_started' do
97
97
  let(:stubbed_date) { Date.parse('2020-03-18 08:00:00') }
98
- before { Date.stub(:today).and_return(stubbed_date) }
98
+ before { allow(Date).to receive(:today).and_return(stubbed_date) }
99
99
 
100
100
  let(:event) { Event.create(started_on: nil) }
101
101
  it { expect { event.mark_as_started }.to_not change { event.reload.started_on } }
@@ -68,7 +68,7 @@ describe Microscope::InstanceMethod::DatetimeInstanceMethod do
68
68
 
69
69
  describe '#start!' do
70
70
  let(:stubbed_date) { Time.parse('2020-03-18 08:00:00') }
71
- before { Time.stub(:now).and_return(stubbed_date) }
71
+ before { allow(Time).to receive(:now).and_return(stubbed_date) }
72
72
 
73
73
  let(:event) { Event.create(started_at: nil) }
74
74
  it { expect { event.start! }.to change { event.reload.started_at }.from(nil).to(stubbed_date) }
@@ -84,7 +84,7 @@ describe Microscope::InstanceMethod::DatetimeInstanceMethod do
84
84
 
85
85
  describe '#mark_as_started' do
86
86
  let(:stubbed_date) { Time.parse('2020-03-18 08:00:00') }
87
- before { Time.stub(:now).and_return(stubbed_date) }
87
+ before { allow(Time).to receive(:now).and_return(stubbed_date) }
88
88
 
89
89
  let(:event) { Event.create(started_at: nil) }
90
90
  it { expect { event.mark_as_started }.to_not change { event.reload.started_at } }
@@ -17,7 +17,7 @@ describe Microscope::Scope::BooleanScope do
17
17
  @user2 = User.create(active: false)
18
18
  end
19
19
 
20
- it { expect(User.active).to have(1).items }
20
+ it { expect(User.active.length).to eql 1 }
21
21
  it { expect(User.active).to include(@user1) }
22
22
  end
23
23
 
@@ -27,7 +27,7 @@ describe Microscope::Scope::BooleanScope do
27
27
  @user2 = User.create(active: true)
28
28
  end
29
29
 
30
- it { expect(User.not_active).to have(1).items }
30
+ it { expect(User.not_active.length).to eql 1 }
31
31
  it { expect(User.not_active).to include(@user1) }
32
32
  it { expect(User.unactive.to_a).to eql User.not_active.to_a }
33
33
  end
@@ -21,6 +21,31 @@ describe Microscope::Scope::DateScope do
21
21
  it { expect(Event.started_before(1.month.ago).to_a).to eql [@event] }
22
22
  end
23
23
 
24
+ describe 'before_or_on scope' do
25
+ let(:date) { 2.months.ago }
26
+
27
+ before do
28
+ @event1 = Event.create(started_on: date)
29
+ @event2 = Event.create(started_on: date - 1.day)
30
+ Event.create(started_on: 1.month.from_now)
31
+ end
32
+
33
+ it { expect(Event.started_before_or_on(date).to_a).to eql [@event1, @event2] }
34
+ end
35
+
36
+ describe 'before_or_now scope' do
37
+ let(:stubbed_date) { Date.parse('2020-03-18 08:00:00') }
38
+ before { allow(Date).to receive(:today).and_return(stubbed_date) }
39
+
40
+ before do
41
+ @event1 = Event.create(started_on: stubbed_date)
42
+ @event2 = Event.create(started_on: stubbed_date - 1.day)
43
+ Event.create(started_on: stubbed_date + 1.week)
44
+ end
45
+
46
+ it { expect(Event.started_before_or_today.to_a).to eql [@event1, @event2] }
47
+ end
48
+
24
49
  describe 'before_today scope' do
25
50
  before do
26
51
  @event = Event.create(started_on: 2.months.ago)
@@ -39,6 +64,31 @@ describe Microscope::Scope::DateScope do
39
64
  it { expect(Event.started_after(1.month.from_now).to_a).to eql [@event] }
40
65
  end
41
66
 
67
+ describe 'after_or_on scope' do
68
+ let(:date) { 2.months.from_now }
69
+
70
+ before do
71
+ @event1 = Event.create(started_on: date)
72
+ @event2 = Event.create(started_on: date + 1.day)
73
+ Event.create(started_on: 1.month.ago)
74
+ end
75
+
76
+ it { expect(Event.started_after_or_on(date).to_a).to eql [@event1, @event2] }
77
+ end
78
+
79
+ describe 'after_or_now scope' do
80
+ let(:stubbed_date) { Date.parse('2020-03-18 08:00:00') }
81
+ before { allow(Date).to receive(:today).and_return(stubbed_date) }
82
+
83
+ before do
84
+ @event1 = Event.create(started_on: stubbed_date)
85
+ @event2 = Event.create(started_on: stubbed_date + 1.day)
86
+ Event.create(started_on: 1.month.ago)
87
+ end
88
+
89
+ it { expect(Event.started_after_or_today.to_a).to eql [@event1, @event2] }
90
+ end
91
+
42
92
  describe 'after_today scope' do
43
93
  before do
44
94
  @event = Event.create(started_on: 2.months.from_now)
@@ -21,6 +21,31 @@ describe Microscope::Scope::DatetimeScope do
21
21
  it { expect(Event.started_before(1.month.ago).to_a).to eql [@event] }
22
22
  end
23
23
 
24
+ describe 'before_or_at scope' do
25
+ let(:datetime) { 1.month.ago }
26
+
27
+ before do
28
+ @event1 = Event.create(started_at: datetime)
29
+ @event2 = Event.create(started_at: datetime - 1.second)
30
+ Event.create(started_at: 1.month.from_now)
31
+ end
32
+
33
+ it { expect(Event.started_before_or_at(datetime).to_a).to eql [@event1, @event2] }
34
+ end
35
+
36
+ describe 'before_or_now scope' do
37
+ let(:stubbed_date) { Time.parse('2020-03-18 08:00:00') }
38
+ before { allow(Time).to receive(:now).and_return(stubbed_date) }
39
+
40
+ before do
41
+ @event1 = Event.create(started_at: stubbed_date)
42
+ @event2 = Event.create(started_at: stubbed_date - 1.second)
43
+ Event.create(started_at: 1.month.from_now)
44
+ end
45
+
46
+ it { expect(Event.started_before_or_now.to_a).to eql [@event1, @event2] }
47
+ end
48
+
24
49
  describe 'before_now scope' do
25
50
  before do
26
51
  @event = Event.create(started_at: 2.months.ago)
@@ -39,6 +64,31 @@ describe Microscope::Scope::DatetimeScope do
39
64
  it { expect(Event.started_after(1.month.from_now).to_a).to eql [@event] }
40
65
  end
41
66
 
67
+ describe 'after_or_at scope' do
68
+ let(:datetime) { 1.month.from_now }
69
+
70
+ before do
71
+ @event1 = Event.create(started_at: datetime)
72
+ @event2 = Event.create(started_at: datetime + 1.second)
73
+ Event.create(started_at: 1.month.ago)
74
+ end
75
+
76
+ it { expect(Event.started_after_or_at(datetime).to_a).to eql [@event1, @event2] }
77
+ end
78
+
79
+ describe 'after_or_now scope' do
80
+ let(:stubbed_date) { Time.parse('2020-03-18 08:00:00') }
81
+ before { allow(Time).to receive(:now).and_return(stubbed_date) }
82
+
83
+ before do
84
+ @event1 = Event.create(started_at: stubbed_date)
85
+ @event2 = Event.create(started_at: stubbed_date + 1.second)
86
+ Event.create(started_at: 1.month.ago)
87
+ end
88
+
89
+ it { expect(Event.started_after_or_now.to_a).to eql [@event1, @event2] }
90
+ end
91
+
42
92
  describe 'after_now scope' do
43
93
  before do
44
94
  @event = Event.create(started_at: 2.months.from_now)
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Microscope::InstanceMethod do
3
+ describe Microscope::Utils do
4
4
  describe :ClassMethods do
5
5
  describe :past_participle_to_infinitive do
6
6
  before do
@@ -9,9 +9,9 @@ describe Microscope::InstanceMethod do
9
9
  end
10
10
  end
11
11
 
12
- let(:past_participles) { ['liked', 'loved', 'gateway_canceled', 'started', 'fed', 'foo'] }
13
- let(:infinitives) { ['like', 'love', 'gateway_cancel', 'start', 'feed', 'bar'] }
14
- let(:mapped_past_participles) { past_participles.map { |v| Microscope::InstanceMethod.past_participle_to_infinitive(v) } }
12
+ let(:past_participles) { %w(liked loved gateway_canceled started fed foo) }
13
+ let(:infinitives) { %w(like love gateway_cancel start feed bar) }
14
+ let(:mapped_past_participles) { past_participles.map { |v| Microscope::Utils.past_participle_to_infinitive(v) } }
15
15
 
16
16
  specify do
17
17
  expect(mapped_past_participles).to eql infinitives
data/spec/spec_helper.rb CHANGED
@@ -1,4 +1,4 @@
1
- $:.unshift File.expand_path('../lib', __FILE__)
1
+ $LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
2
2
 
3
3
  require 'coveralls'
4
4
  Coveralls.wear!
@@ -18,6 +18,11 @@ RSpec.configure do |config|
18
18
  config.include DatabaseMacros
19
19
  config.include ModelMacros
20
20
 
21
+ # Disable `should` syntax
22
+ config.expect_with :rspec do |c|
23
+ c.syntax = :expect
24
+ end
25
+
21
26
  config.before :each do
22
27
  # Establish ActiveRecord database connection
23
28
  adapter = ENV['DB_ADAPTER'] || 'sqlite3'
@@ -5,7 +5,7 @@ module DatabaseMacros
5
5
  klass = Class.new(ActiveRecord::Migration)
6
6
 
7
7
  # Create a new `up` that executes the argument
8
- klass.send(:define_method, :up) { self.instance_exec(&block) }
8
+ klass.send(:define_method, :up) { instance_exec(&block) }
9
9
 
10
10
  # Create a new instance of it and execute its `up` method
11
11
  klass.new.up
@@ -7,7 +7,7 @@ module ModelMacros
7
7
  end
8
8
  end
9
9
 
10
- protected
10
+ protected
11
11
 
12
12
  # Create a new model class
13
13
  def spawn_model(klass_name, parent_klass, &block)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: microscope
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.1
4
+ version: 0.6.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Simon Prévost
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-05-23 00:00:00.000000000 Z
12
+ date: 2014-08-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -71,16 +71,16 @@ dependencies:
71
71
  name: rspec
72
72
  requirement: !ruby/object:Gem::Requirement
73
73
  requirements:
74
- - - ">="
74
+ - - '='
75
75
  - !ruby/object:Gem::Version
76
- version: '0'
76
+ version: 3.0.0.rc1
77
77
  type: :development
78
78
  prerelease: false
79
79
  version_requirements: !ruby/object:Gem::Requirement
80
80
  requirements:
81
- - - ">="
81
+ - - '='
82
82
  - !ruby/object:Gem::Version
83
- version: '0'
83
+ version: 3.0.0.rc1
84
84
  - !ruby/object:Gem::Dependency
85
85
  name: sqlite3
86
86
  requirement: !ruby/object:Gem::Requirement
@@ -137,6 +137,34 @@ dependencies:
137
137
  - - ">="
138
138
  - !ruby/object:Gem::Version
139
139
  version: '0'
140
+ - !ruby/object:Gem::Dependency
141
+ name: rubocop
142
+ requirement: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - '='
145
+ - !ruby/object:Gem::Version
146
+ version: 0.20.1
147
+ type: :development
148
+ prerelease: false
149
+ version_requirements: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - '='
152
+ - !ruby/object:Gem::Version
153
+ version: 0.20.1
154
+ - !ruby/object:Gem::Dependency
155
+ name: phare
156
+ requirement: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - ">="
159
+ - !ruby/object:Gem::Version
160
+ version: '0'
161
+ type: :development
162
+ prerelease: false
163
+ version_requirements: !ruby/object:Gem::Requirement
164
+ requirements:
165
+ - - ">="
166
+ - !ruby/object:Gem::Version
167
+ version: '0'
140
168
  description: Microscope adds useful scopes targeting ActiveRecord boolean and datetime
141
169
  fields.
142
170
  email:
@@ -148,14 +176,14 @@ extra_rdoc_files: []
148
176
  files:
149
177
  - ".gitignore"
150
178
  - ".rspec"
179
+ - ".rubocop.yml"
151
180
  - ".travis.yml"
152
181
  - Gemfile
153
182
  - LICENSE.md
154
183
  - README.md
155
184
  - Rakefile
156
185
  - data/irregular_verbs.yml
157
- - gemfiles/Gemfile.activerecord-3.2.x
158
- - gemfiles/Gemfile.activerecord-4.0
186
+ - gemfiles/Gemfile.activerecord-4.1
159
187
  - lib/microscope.rb
160
188
  - lib/microscope/instance_method.rb
161
189
  - lib/microscope/instance_method/boolean_instance_method.rb
@@ -165,15 +193,16 @@ files:
165
193
  - lib/microscope/scope/boolean_scope.rb
166
194
  - lib/microscope/scope/date_scope.rb
167
195
  - lib/microscope/scope/datetime_scope.rb
196
+ - lib/microscope/utils.rb
168
197
  - lib/microscope/version.rb
169
198
  - microscope.gemspec
170
199
  - spec/microscope/instance_method/boolean_instance_method_spec.rb
171
200
  - spec/microscope/instance_method/date_instance_method_spec.rb
172
201
  - spec/microscope/instance_method/datetime_instance_method_spec.rb
173
- - spec/microscope/instance_method_spec.rb
174
202
  - spec/microscope/scope/boolean_scope_spec.rb
175
203
  - spec/microscope/scope/date_scope_spec.rb
176
204
  - spec/microscope/scope/datetime_scope_spec.rb
205
+ - spec/microscope/utils_spec.rb
177
206
  - spec/microscope_spec.rb
178
207
  - spec/spec_helper.rb
179
208
  - spec/support/macros/database/database_adapter.rb
@@ -211,10 +240,10 @@ test_files:
211
240
  - spec/microscope/instance_method/boolean_instance_method_spec.rb
212
241
  - spec/microscope/instance_method/date_instance_method_spec.rb
213
242
  - spec/microscope/instance_method/datetime_instance_method_spec.rb
214
- - spec/microscope/instance_method_spec.rb
215
243
  - spec/microscope/scope/boolean_scope_spec.rb
216
244
  - spec/microscope/scope/date_scope_spec.rb
217
245
  - spec/microscope/scope/datetime_scope_spec.rb
246
+ - spec/microscope/utils_spec.rb
218
247
  - spec/microscope_spec.rb
219
248
  - spec/spec_helper.rb
220
249
  - spec/support/macros/database/database_adapter.rb
@@ -1,5 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- gemspec path: '../'
4
-
5
- gem 'activerecord', '~> 4.0.0'