state_of_the_nation 1.0.1 → 1.1.0

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: 72a6818e2ace1bf60670b197c86a09d4dc925ed5
4
- data.tar.gz: bb28045766320c669452430634fe58cfda921304
3
+ metadata.gz: f860e2aab34bc7885c2de966a79665080b07179e
4
+ data.tar.gz: 349d751236999d5bc792d24a8f1ebc8b4cdace48
5
5
  SHA512:
6
- metadata.gz: ec3602725f892040ae4c022a68b85b0a35e8efc5c3126818769a390ce6fc253dd8d9c3bf7db2dbbed0668c38ff15f1044d139c1da26d4d15827a324bd11c963c
7
- data.tar.gz: a2afdc5264ef33b14dfe0a82f1c5ee03c260174bca002c634663ac885dbf0c60731d028757a3ce8be6b922ae341c2c7c610f998851be28bfe4a447d9b5056368
6
+ metadata.gz: 66d9caa5d36028657ef95c22e2eea1083d0f7750735c6763fca06e9fbd1143c2757a2dc30ae2950e89932d20e16f66b488ed787abc3fef1be028ca0c02689850
7
+ data.tar.gz: e68a543d34ee71d55167a9e1db1a7857a916194f9349fcbc21f5a7615d404458b48814cec0df528459b3ca11eb0dab15e89e3fed940af4409da077abdfc228c8
data/README.md CHANGED
@@ -52,6 +52,25 @@ usa.active_senators(Date.new(2015, 1, 1))
52
52
 
53
53
 
54
54
  ```
55
+ ## IdentityCache Support
56
+
57
+ StateOfTheNation optionally supports fetching child records out of an IdentityCache cache instead of reading directly from the SQL table.
58
+
59
+ For example if the Country model uses IdentityCache to cache the has_many relationship to President, you can make StateOfTheNation use the IdentityCache methods by calling `.with_identity_cache` on your `has_active` or `has_uniquely_active` definitions like so.
60
+
61
+ ```ruby
62
+ class Country
63
+ include IdentityCache
64
+ include StateOfTheNation
65
+
66
+ has_many(:presidents)
67
+ cache_has_many(:presidents)
68
+ has_uniquely_active(:president).with_identity_cache
69
+ end
70
+ ```
71
+
72
+ Now every time the `Country#active_president` method is called StateOfTheNation will read through the IdentityCache methods and avoid a SELECT operation if possible.
73
+
55
74
  ## Installation
56
75
 
57
76
  Add this line to your application's Gemfile:
@@ -76,5 +95,5 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
76
95
 
77
96
  ## Contributing
78
97
 
79
- Bug reports and pull requests are welcome on GitHub at https://github.com/intercom/state_of_the_nation. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
98
+ Bug reports and pull requests are welcome on GitHub at https://github.com/intercom/state_of_the_nation. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org/) code of conduct.
80
99
 
@@ -24,6 +24,7 @@ module StateOfTheNation
24
24
  @finish_key = finish_key
25
25
 
26
26
  define_method "active?" do |time = Time.now.utc|
27
+ time = should_round_timestamps? ? time.round : time
27
28
  (finish.blank? || finish > time) && start <= time
28
29
  end
29
30
 
@@ -36,17 +37,32 @@ module StateOfTheNation
36
37
  end
37
38
 
38
39
  def has_active(association_plural)
39
- add_child_methods(plural: association_plural, single: false)
40
+ @association_plural = association_plural
41
+ add_child_methods(plural: @association_plural, single: false, with_identity_cache: false)
42
+
43
+ def with_identity_cache
44
+ add_child_methods(plural: @association_plural, single: false, with_identity_cache: true)
45
+ self
46
+ end
47
+
48
+ self
40
49
  end
41
50
 
42
51
  def has_uniquely_active(association_singular)
43
- association_plural = association_singular.to_s.pluralize
44
- add_child_methods(plural: association_plural, single: true)
52
+ @association_plural = association_singular.to_s.pluralize
53
+ add_child_methods(plural: @association_plural, single: true, with_identity_cache: false)
54
+
55
+ def with_identity_cache
56
+ add_child_methods(plural: @association_plural, single: true, with_identity_cache: true)
57
+ self
58
+ end
59
+
60
+ self
45
61
  end
46
62
 
47
63
  private
48
64
 
49
- def add_child_methods(plural:, single:)
65
+ def add_child_methods(plural:, single:, with_identity_cache:)
50
66
  child_class = self.reflect_on_association(plural).klass
51
67
  name = self.name.demodulize.underscore.to_sym
52
68
  child_class.instance_variable_set(:@parent_association, name)
@@ -55,10 +71,25 @@ module StateOfTheNation
55
71
  association = single ? plural.singularize : plural
56
72
 
57
73
  define_method "active_#{association}" do |time = Time.now.utc|
58
- collection = send(plural.to_sym).select { |r| r.send("active?", time) }
74
+ time = should_round_timestamps? ? time.round : time
75
+ method_name = with_identity_cache ? "fetch_#{plural}" : plural.to_sym
76
+ collection = send(method_name).select { |r| r.send("active?", time) }
59
77
  single ? collection.first : collection
60
78
  end
61
79
  end
80
+
81
+ def should_round_timestamps?
82
+ # MySQL datetime fields do not support millisecond resolution while
83
+ # PostgreSQL's do. To prevent issues with near identical timestamps not
84
+ # comparing as expected in .active? methods we'll choose the resolution
85
+ # appropriate for the database adapter backing the model.
86
+ case self.connection.adapter_name
87
+ when /PostgreSQL/
88
+ false
89
+ else
90
+ true
91
+ end
92
+ end
62
93
  end
63
94
 
64
95
  private
@@ -131,4 +162,8 @@ module StateOfTheNation
131
162
  def finish_key
132
163
  self.class.finish_key
133
164
  end
165
+
166
+ def should_round_timestamps?
167
+ self.class.send(:should_round_timestamps?)
168
+ end
134
169
  end
@@ -1,3 +1,3 @@
1
1
  module StateOfTheNation
2
- VERSION = "1.0.1"
2
+ VERSION = "1.1.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: state_of_the_nation
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Patrick O'Doherty
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2016-01-07 00:00:00.000000000 Z
12
+ date: 2016-01-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler