microscope 0.4 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,10 +1,16 @@
1
- # Microscope
2
-
3
- [![Gem Version](https://badge.fury.io/rb/microscope.png)](https://rubygems.org/gems/microscope)
4
- [![Code Climate](https://codeclimate.com/github/mirego/microscope.png)](https://codeclimate.com/github/mirego/microscope)
5
- [![Build Status](https://travis-ci.org/mirego/microscope.png?branch=master)](https://travis-ci.org/mirego/microscope)
6
-
7
- Microscope adds useful scopes targeting ActiveRecord boolean, date and datetime fields.
1
+ <p align="center">
2
+ <a href="https://github.com/mirego/microscope">
3
+ <img src="http://i.imgur.com/JMcAStM.png" alt="Microscope" />
4
+ </a>
5
+ <br />
6
+ Microscope adds useful scopes targeting ActiveRecord <code>boolean</code>, <code>date</code> and <code>datetime</code> fields.
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://travis-ci.org/mirego/microscope"><img src="https://travis-ci.org/mirego/microscope.png?branch=master" /></a>
11
+ </p>
12
+
13
+ ---
8
14
 
9
15
  ## Installation
10
16
 
@@ -96,6 +102,8 @@ User.updated_before(2.months.ago) # works!
96
102
 
97
103
  `Microscope` is © 2013 [Mirego](http://www.mirego.com) and may be freely distributed under the [New BSD license](http://opensource.org/licenses/BSD-3-Clause). See the [`LICENSE.md`](https://github.com/mirego/microscope/blob/master/LICENSE.md) file.
98
104
 
105
+ The microscope logo is based on [this lovely icon](http://thenounproject.com/noun/microscope/#icon-No9056) by [Scott Lewis](http://thenounproject.com/iconify), from The Noun Project. Used under a [Creative Commons BY 3.0](http://creativecommons.org/licenses/by/3.0/) license.
106
+
99
107
  ## About Mirego
100
108
 
101
109
  Mirego is a team of passionate people who believe that work is a place where you can innovate and have fun. We proudly build mobile applications for [iPhone](http://mirego.com/en/iphone-app-development/ "iPhone application development"), [iPad](http://mirego.com/en/ipad-app-development/ "iPad application development"), [Android](http://mirego.com/en/android-app-development/ "Android application development"), [Blackberry](http://mirego.com/en/blackberry-app-development/ "Blackberry application development"), [Windows Phone](http://mirego.com/en/windows-phone-app-development/ "Windows Phone application development") and [Windows 8](http://mirego.com/en/windows-8-app-development/ "Windows 8 application development") in beautiful Quebec City.
@@ -9,6 +9,10 @@ module Microscope
9
9
  value = send("#{field.name}")
10
10
  !value.nil? && value <= Date.today
11
11
  end
12
+
13
+ define_method "not_#{cropped_field}?" do
14
+ !#{cropped_field}?
15
+ end
12
16
  RUBY
13
17
  end
14
18
  end
@@ -9,6 +9,10 @@ module Microscope
9
9
  value = send("#{field.name}")
10
10
  !value.nil? && value <= Time.now
11
11
  end
12
+
13
+ define_method "not_#{cropped_field}?" do
14
+ !#{cropped_field}?
15
+ end
12
16
  RUBY
13
17
  end
14
18
  end
@@ -1,5 +1,12 @@
1
1
  module Microscope
2
2
  class Scope < Struct.new(:model, :field)
3
+ def initialize(*args)
4
+ super
5
+
6
+ @field_name = field.name
7
+ @table_name = model.name.tableize
8
+ end
9
+
3
10
  # Inject ActiveRecord scopes into a model
4
11
  def self.inject_scopes(model, fields, options = {})
5
12
  fields.each do |field|
@@ -3,8 +3,8 @@ module Microscope
3
3
  class BooleanScope < Scope
4
4
  def apply
5
5
  model.class_eval <<-RUBY, __FILE__, __LINE__ + 1
6
- scope "#{field.name}", lambda { where("#{field.name}" => true) }
7
- scope "not_#{field.name}", lambda { where("#{field.name}" => false) }
6
+ scope "#{@field_name}", lambda { where("#{@field_name}" => true) }
7
+ scope "not_#{@field_name}", lambda { where("#{@field_name}" => false) }
8
8
  RUBY
9
9
  end
10
10
  end
@@ -10,7 +10,8 @@ module Microscope
10
10
  end
11
11
 
12
12
  def apply
13
- @cropped_field = field.name.gsub(@cropped_field_regex, '')
13
+ return unless @field_name =~ @cropped_field_regex
14
+ @cropped_field = @field_name.gsub(@cropped_field_regex, '')
14
15
 
15
16
  model.class_eval <<-RUBY, __FILE__, __LINE__ + 1
16
17
  #{before_scopes}
@@ -24,30 +25,30 @@ module Microscope
24
25
 
25
26
  def before_scopes
26
27
  <<-RUBY
27
- scope "#{@cropped_field}_before", lambda { |time| where("#{field.name} < ?", time) }
28
- scope "#{@cropped_field}_before_or_at", lambda { |time| where("#{field.name} <= ?", time) }
29
- scope "#{@cropped_field}_before#{@now_suffix}", lambda { where("#{field.name} < ?", #{@now}) }
28
+ scope "#{@cropped_field}_before", lambda { |time| where("`#{@table_name}`.`#{@field_name}` < ?", time) }
29
+ scope "#{@cropped_field}_before_or_at", lambda { |time| where("`#{@table_name}`.`#{@field_name}` <= ?", time) }
30
+ scope "#{@cropped_field}_before#{@now_suffix}", lambda { where("`#{@table_name}`.`#{@field_name}` < ?", #{@now}) }
30
31
  RUBY
31
32
  end
32
33
 
33
34
  def after_scopes
34
35
  <<-RUBY
35
- scope "#{@cropped_field}_after", lambda { |time| where("#{field.name} > ?", time) }
36
- scope "#{@cropped_field}_after_or_at", lambda { |time| where("#{field.name} >= ?", time) }
37
- scope "#{@cropped_field}_after#{@now_suffix}", lambda { where("#{field.name} > ?", #{@now}) }
36
+ scope "#{@cropped_field}_after", lambda { |time| where("`#{@table_name}`.`#{@field_name}` > ?", time) }
37
+ scope "#{@cropped_field}_after_or_at", lambda { |time| where("`#{@table_name}`.`#{@field_name}` >= ?", time) }
38
+ scope "#{@cropped_field}_after#{@now_suffix}", lambda { where("`#{@table_name}`.`#{@field_name}` > ?", #{@now}) }
38
39
  RUBY
39
40
  end
40
41
 
41
42
  def between_scopes
42
43
  <<-RUBY
43
- scope "#{@cropped_field}_between", lambda { |range| where("#{field.name}" => range) }
44
+ scope "#{@cropped_field}_between", lambda { |range| where("#{@field_name}" => range) }
44
45
  RUBY
45
46
  end
46
47
 
47
48
  def boolean_scopes
48
49
  <<-RUBY
49
- scope "#{@cropped_field}", lambda { where("#{field.name} IS NOT NULL AND #{field.name} <= ?", #{@now}) }
50
- scope "not_#{@cropped_field}", lambda { where("#{field.name} IS NULL OR #{field.name} > ?", #{@now}) }
50
+ scope "#{@cropped_field}", lambda { where("`#{@table_name}`.`#{@field_name}` IS NOT NULL AND `#{@table_name}`.`#{@field_name}` <= ?", #{@now}) }
51
+ scope "not_#{@cropped_field}", lambda { where("`#{@table_name}`.`#{@field_name}` IS NULL OR `#{@table_name}`.`#{@field_name}` > ?", #{@now}) }
51
52
  RUBY
52
53
  end
53
54
  end
@@ -1,3 +1,3 @@
1
1
  module Microscope
2
- VERSION = "0.4"
2
+ VERSION = "0.4.1"
3
3
  end
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+ describe Microscope::InstanceMethod::DateInstanceMethod do
4
+ before do
5
+ run_migration do
6
+ create_table(:events, force: true) do |t|
7
+ t.date :started_on, default: nil
8
+ end
9
+ end
10
+
11
+ microscope 'Event'
12
+ end
13
+
14
+ describe '#started?' do
15
+ context 'with positive result' do
16
+ subject { Event.create(started_on: 2.months.ago) }
17
+ it { should be_started }
18
+ it { should_not be_not_started }
19
+ end
20
+
21
+ context 'with negative result' do
22
+ subject { Event.create(started_on: 1.month.from_now) }
23
+ it { should_not be_started }
24
+ it { should be_not_started }
25
+ end
26
+ end
27
+
28
+ describe '#not_started?' do
29
+ context 'with negative result' do
30
+ subject { Event.create(started_on: 2.months.ago) }
31
+ it { should_not be_not_started }
32
+ end
33
+
34
+ context 'with positive result' do
35
+ subject { Event.create(started_on: 1.month.from_now) }
36
+ it { should be_not_started }
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ describe Microscope::InstanceMethod::DatetimeInstanceMethod do
4
+ before do
5
+ run_migration do
6
+ create_table(:events, force: true) do |t|
7
+ t.datetime :started_at, default: nil
8
+ end
9
+ end
10
+
11
+ microscope 'Event'
12
+ end
13
+
14
+ describe '#started?' do
15
+ context 'with positive result' do
16
+ subject { Event.create(started_at: 2.months.ago) }
17
+ it { should be_started }
18
+ end
19
+
20
+ context 'with negative result' do
21
+ subject { Event.create(started_at: 1.month.from_now) }
22
+ it { should_not be_started }
23
+ end
24
+ end
25
+
26
+ describe '#not_started?' do
27
+ context 'with negative result' do
28
+ subject { Event.create(started_at: 2.months.ago) }
29
+ it { should_not be_not_started }
30
+ end
31
+
32
+ context 'with positive result' do
33
+ subject { Event.create(started_at: 1.month.from_now) }
34
+ it { should be_not_started }
35
+ end
36
+ end
37
+ end
@@ -14,18 +14,22 @@ describe Microscope::Scope::BooleanScope do
14
14
  end
15
15
 
16
16
  describe 'positive scope' do
17
- before { @user1 = User.create(active: true) }
17
+ before do
18
+ @user1 = User.create(active: true)
19
+ @user2 = User.create(active: false)
20
+ end
18
21
 
19
22
  its(:active) { should have(1).items }
20
23
  its(:active) { should include(@user1) }
21
- its(:not_active) { should be_empty }
22
24
  end
23
25
 
24
26
  describe 'negative scope' do
25
- before { @user1 = User.create(active: false) }
27
+ before do
28
+ @user1 = User.create(active: false)
29
+ @user2 = User.create(active: true)
30
+ end
26
31
 
27
32
  its(:not_active) { should have(1).items }
28
33
  its(:not_active) { should include(@user1) }
29
- its(:active) { should be_empty }
30
34
  end
31
35
  end
@@ -7,6 +7,7 @@ describe Microscope::Scope::DateScope do
7
7
  run_migration do
8
8
  create_table(:events, force: true) do |t|
9
9
  t.date :started_on, default: nil
10
+ t.date :published_date, default: nil
10
11
  end
11
12
  end
12
13
 
@@ -92,4 +93,11 @@ describe Microscope::Scope::DateScope do
92
93
  it { should_not be_started }
93
94
  end
94
95
  end
96
+
97
+ context 'for field that does not match the pattern' do
98
+ subject { Event }
99
+
100
+ it { should_not respond_to(:published_date) }
101
+ it { should_not respond_to(:not_published_date) }
102
+ end
95
103
  end
@@ -1,3 +1,5 @@
1
+ require 'spec_helper'
2
+
1
3
  describe Microscope::Scope::DatetimeScope do
2
4
  subject { Event }
3
5
 
@@ -5,6 +7,7 @@ describe Microscope::Scope::DatetimeScope do
5
7
  run_migration do
6
8
  create_table(:events, force: true) do |t|
7
9
  t.datetime :started_at, default: nil
10
+ t.datetime :published_date, default: nil
8
11
  end
9
12
  end
10
13
 
@@ -90,4 +93,11 @@ describe Microscope::Scope::DatetimeScope do
90
93
  it { should_not be_started }
91
94
  end
92
95
  end
96
+
97
+ context 'for field that does not match the pattern' do
98
+ subject { Event }
99
+
100
+ it { should_not respond_to(:published_date) }
101
+ it { should_not respond_to(:not_published_date) }
102
+ end
93
103
  end
@@ -0,0 +1,128 @@
1
+ require 'spec_helper'
2
+
3
+ describe Microscope do
4
+ describe :acts_as_microscope do
5
+ subject { User }
6
+
7
+ before do
8
+ run_migration do
9
+ create_table(:users, force: true) do |t|
10
+ # Boolean
11
+ t.boolean :active, default: false
12
+ t.boolean :admin, default: false
13
+ t.boolean :moderator, default: false
14
+
15
+ # DateTime
16
+ t.datetime :published_at, default: false
17
+ t.datetime :removed_at, default: false
18
+
19
+ # Date
20
+ t.date :started_on, default: false
21
+ t.date :ended_on, default: false
22
+ end
23
+ end
24
+ end
25
+
26
+ describe :except do
27
+ before do
28
+ microscope 'User', except: [:admin, :removed_at, :ended_on]
29
+ end
30
+
31
+ it { should respond_to :active }
32
+ it { should respond_to :not_active }
33
+ it { should respond_to :moderator }
34
+ it { should respond_to :not_moderator }
35
+ it { should_not respond_to :admin }
36
+ it { should_not respond_to :not_admin }
37
+ it { should respond_to :published }
38
+ it { should respond_to :not_published }
39
+ it { should_not respond_to :removed }
40
+ it { should_not respond_to :not_removed }
41
+ it { should respond_to :started }
42
+ it { should respond_to :not_started }
43
+ it { should_not respond_to :ended }
44
+ it { should_not respond_to :not_ended }
45
+
46
+ context 'for single instance' do
47
+ subject { User.new }
48
+
49
+ it { should respond_to :published? }
50
+ it { should respond_to :not_published? }
51
+ it { should_not respond_to :removed? }
52
+ it { should_not respond_to :not_removed? }
53
+ it { should respond_to :started? }
54
+ it { should respond_to :not_started? }
55
+ it { should_not respond_to :ended? }
56
+ it { should_not respond_to :not_ended? }
57
+ end
58
+ end
59
+
60
+ describe :only do
61
+ before do
62
+ microscope 'User', only: [:admin, :removed_at, :ended_on]
63
+ end
64
+
65
+ it { should_not respond_to :active }
66
+ it { should_not respond_to :not_active }
67
+ it { should_not respond_to :moderator }
68
+ it { should_not respond_to :not_moderator }
69
+ it { should respond_to :admin }
70
+ it { should respond_to :not_admin }
71
+ it { should_not respond_to :published }
72
+ it { should_not respond_to :not_published }
73
+ it { should respond_to :removed }
74
+ it { should respond_to :not_removed }
75
+ it { should_not respond_to :started }
76
+ it { should_not respond_to :not_started }
77
+ it { should respond_to :ended }
78
+ it { should respond_to :not_ended }
79
+
80
+ context 'for single instance' do
81
+ subject { User.new }
82
+
83
+ it { should respond_to :removed? }
84
+ it { should respond_to :not_removed? }
85
+ it { should_not respond_to :published? }
86
+ it { should_not respond_to :not_published? }
87
+ it { should respond_to :ended? }
88
+ it { should respond_to :not_ended? }
89
+ it { should_not respond_to :started? }
90
+ it { should_not respond_to :not_started? }
91
+ end
92
+ end
93
+
94
+ describe 'except and only' do
95
+ before do
96
+ microscope 'User', only: [:admin, :started_on], except: [:active]
97
+ end
98
+
99
+ it { should_not respond_to :active }
100
+ it { should_not respond_to :not_active }
101
+ it { should_not respond_to :moderator }
102
+ it { should_not respond_to :not_moderator }
103
+ it { should respond_to :admin }
104
+ it { should respond_to :not_admin }
105
+ it { should respond_to :started }
106
+ it { should respond_to :not_started }
107
+ it { should_not respond_to :published }
108
+ it { should_not respond_to :not_published }
109
+ it { should_not respond_to :removed }
110
+ it { should_not respond_to :not_removed }
111
+ it { should_not respond_to :ended }
112
+ it { should_not respond_to :not_ended }
113
+
114
+ context 'for single instance' do
115
+ subject { User.new }
116
+
117
+ it { should respond_to :started? }
118
+ it { should respond_to :not_started? }
119
+ it { should_not respond_to :removed? }
120
+ it { should_not respond_to :not_removed? }
121
+ it { should_not respond_to :published? }
122
+ it { should_not respond_to :not_published? }
123
+ it { should_not respond_to :ended? }
124
+ it { should_not respond_to :not_ended? }
125
+ end
126
+ end
127
+ end
128
+ end
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.4'
4
+ version: 0.4.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-08-13 00:00:00.000000000 Z
13
+ date: 2013-08-28 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport
@@ -136,10 +136,12 @@ files:
136
136
  - lib/microscope/scope/datetime_scope.rb
137
137
  - lib/microscope/version.rb
138
138
  - microscope.gemspec
139
+ - spec/microscope/instance_method/date_instance_method_spec.rb
140
+ - spec/microscope/instance_method/datetime_instance_method_spec.rb
139
141
  - spec/microscope/scope/boolean_scope_spec.rb
140
142
  - spec/microscope/scope/date_scope_spec.rb
141
143
  - spec/microscope/scope/datetime_scope_spec.rb
142
- - spec/microscope/scope_spec.rb
144
+ - spec/microscope_spec.rb
143
145
  - spec/spec_helper.rb
144
146
  - spec/support/macros/database_macros.rb
145
147
  - spec/support/macros/model_macros.rb
@@ -158,7 +160,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
158
160
  version: '0'
159
161
  segments:
160
162
  - 0
161
- hash: 2193329954126245792
163
+ hash: 3433673012719073524
162
164
  required_rubygems_version: !ruby/object:Gem::Requirement
163
165
  none: false
164
166
  requirements:
@@ -167,7 +169,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
167
169
  version: '0'
168
170
  segments:
169
171
  - 0
170
- hash: 2193329954126245792
172
+ hash: 3433673012719073524
171
173
  requirements: []
172
174
  rubyforge_project:
173
175
  rubygems_version: 1.8.23
@@ -176,10 +178,12 @@ specification_version: 3
176
178
  summary: Microscope adds useful scopes targeting ActiveRecord boolean and datetime
177
179
  fields.
178
180
  test_files:
181
+ - spec/microscope/instance_method/date_instance_method_spec.rb
182
+ - spec/microscope/instance_method/datetime_instance_method_spec.rb
179
183
  - spec/microscope/scope/boolean_scope_spec.rb
180
184
  - spec/microscope/scope/date_scope_spec.rb
181
185
  - spec/microscope/scope/datetime_scope_spec.rb
182
- - spec/microscope/scope_spec.rb
186
+ - spec/microscope_spec.rb
183
187
  - spec/spec_helper.rb
184
188
  - spec/support/macros/database_macros.rb
185
189
  - spec/support/macros/model_macros.rb
@@ -1,56 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Microscope::Scope do
4
- describe :inject_scopes do
5
- subject { User }
6
-
7
- before do
8
- run_migration do
9
- create_table(:users, force: true) do |t|
10
- t.boolean :active, default: false
11
- t.boolean :admin, default: false
12
- t.boolean :moderator, default: false
13
- end
14
- end
15
- end
16
-
17
- describe :except do
18
- before do
19
- microscope 'User', except: [:admin]
20
- end
21
-
22
- it { should respond_to :active }
23
- it { should respond_to :not_active }
24
- it { should respond_to :moderator }
25
- it { should respond_to :not_moderator }
26
- it { should_not respond_to :admin }
27
- it { should_not respond_to :not_admin }
28
- end
29
-
30
- describe :only do
31
- before do
32
- microscope 'User', only: [:admin]
33
- end
34
-
35
- it { should_not respond_to :active }
36
- it { should_not respond_to :not_active }
37
- it { should_not respond_to :moderator }
38
- it { should_not respond_to :not_moderator }
39
- it { should respond_to :admin }
40
- it { should respond_to :not_admin }
41
- end
42
-
43
- describe 'except and only' do
44
- before do
45
- microscope 'User', only: [:admin], except: [:active]
46
- end
47
-
48
- it { should_not respond_to :active }
49
- it { should_not respond_to :not_active }
50
- it { should_not respond_to :moderator }
51
- it { should_not respond_to :not_moderator }
52
- it { should respond_to :admin }
53
- it { should respond_to :not_admin }
54
- end
55
- end
56
- end