commutator 0.1.0 → 0.1.1

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: 1a212280a2711c6edae1077d491db74fb9186104
4
- data.tar.gz: ea5708c8299dd8cc5446e90417c3e6824b2b70b1
3
+ metadata.gz: 48ccc3f1fbd5002ddd24c28e9ac78c1e9768c123
4
+ data.tar.gz: 60eac8308078a21d24cc45112c3093ced30116f8
5
5
  SHA512:
6
- metadata.gz: bdbe600527356e277e86af158e387d0c78250254ee7a07af6e92d2a91a663e44340c0c9f442049174aaa9123ab921bc9df7cc103e11e0a363ee2bb242ce7d778
7
- data.tar.gz: 1dd47ee873c1fe1447eeac8ab25a5c99ae4cf51bd257c065a1537caf77ca8756aa1add7a385cdd98656683fedb40c6b65e2cf9b88c83a0cb1175ca82b5bf76fb
6
+ metadata.gz: f1e28a291d82044addad086e6715819711c0a1c01e57ebabdd56b4d88a2daa32d38ffcbc375611cea8dd1c5f95e7db3a5eee52f4ea0c79cfd22cb3b0edbd76b6
7
+ data.tar.gz: d77480d5eec6b8be9be9e77636a83422bbe81ad6011a94c6d9e9671baf2d04e673e0a56ad626c675e13121e245f7878c9be9951a5831766219faff41dd3e1180
data/Changelog.md CHANGED
@@ -1,6 +1,17 @@
1
1
  ### Development
2
- [Full Changelog](http://github.com/tablexi/commutator/compare/v0.1.0...master)
2
+ [Full Changelog](http://github.com/tablexi/commutator/compare/v0.1.1...master)
3
3
 
4
+ ### Development
5
+ [Full Changelog](http://github.com/tablexi/commutator/compare/v0.1.0...v0.1.1)
6
+
7
+ Features:
8
+
9
+ * Add `Commutator::Collection::CachedLookup` (Bradley Schaefer)
10
+
11
+ Bug fixes:
12
+
13
+ * Change lazy-loading of Commutator::Model.client to be eager-loaded
14
+ to facilitate the ability to override the client. (Bradley Schaefer)
4
15
 
5
16
  ### 0.1.0 / 2016-01-11
6
17
 
@@ -0,0 +1,34 @@
1
+ module Commutator
2
+ class Collection
3
+ # Implemention of a per-collection cache to avoid repeating expensive
4
+ # lookups. E.g. if several items in the collection refer to the same User
5
+ # object, the following pattern would ensure the User is only looked up
6
+ # once:
7
+ #
8
+ # # (inside your model)
9
+ # CachedUserLookup = Proc.new do
10
+ # Commutator::Collection::CachedLookup.new(:user, :user_id) do |user_id|
11
+ # User.find(user_id)
12
+ # end
13
+ # end
14
+ #
15
+ # modify_collection_items_with CachedUserLookup, factory: true
16
+ #
17
+ # Note: The cache itself is not accessible since it's hidden by the closure
18
+ # scope. Additionally, this returns the same instance of the object for
19
+ # every matching lookup - so be mindful when modifying that object.
20
+ module CachedLookup
21
+ def self.new(attr_name, item_key, &block)
22
+ cache = Hash.new do |h, k|
23
+ h[k] = block.call(k)
24
+ end
25
+
26
+ Proc.new do |item|
27
+ item.define_singleton_method(attr_name) do
28
+ cache[item.send(item_key)]
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -5,6 +5,8 @@ module Commutator
5
5
  # NOTE: This can't use SimpleDelegator because `Seahorse::Client::Response` does
6
6
  # not implement `#respond_to?` as needed.
7
7
  class Collection
8
+ autoload :CachedLookup, "commutator/collection/cached_lookup"
9
+
8
10
  delegate :count,
9
11
  :scanned_count,
10
12
  :last_evaluated_key,
@@ -48,9 +48,11 @@ module Commutator
48
48
  before_get_item :configure_default_get_item
49
49
 
50
50
  class_attribute :collection_item_modifiers, instance_accessor: false
51
+ class_attribute :client
52
+ self.client = ::Commutator::SimpleClient.new
51
53
  end
52
54
 
53
- delegate :client, :options_class, to: 'self.class'
55
+ delegate :options_class, to: 'self.class'
54
56
 
55
57
  def initialize(attrs = {})
56
58
  assign_attributes(attrs.symbolize_keys)
@@ -130,8 +132,6 @@ module Commutator
130
132
 
131
133
  # :nodoc:
132
134
  module ClassMethods
133
- attr_writer :client
134
-
135
135
  def inherited(subclass)
136
136
  subclass.attribute_names.merge(attribute_names)
137
137
  before_hooks.each { |k, v| subclass.before_hooks[k] = v.dup }
@@ -144,16 +144,14 @@ module Commutator
144
144
  subclass.const_set("Scopes", Module.new { include scopes }) if scopes
145
145
  end
146
146
 
147
- def client
148
- @client ||= ::Commutator::SimpleClient.new
149
- end
150
-
151
147
  def create(attrs)
152
148
  new(attrs).tap { |dp| dp.put_item_options.execute }
153
149
  end
154
150
 
155
151
  def modify_collection_items_with(*modifiers, factory: false)
156
- self.collection_item_modifiers = [ItemModifiers.new(modifiers, factory: factory)].unshift(*collection_item_modifiers)
152
+ self.collection_item_modifiers = [
153
+ ItemModifiers.new(modifiers, factory: factory)
154
+ ].unshift(*collection_item_modifiers)
157
155
  end
158
156
 
159
157
  def get_item_options
@@ -1,3 +1,3 @@
1
1
  module Commutator
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: commutator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bradley Schaefer
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2016-01-11 00:00:00.000000000 Z
12
+ date: 2016-01-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -129,6 +129,7 @@ files:
129
129
  - commutator.gemspec
130
130
  - lib/commutator.rb
131
131
  - lib/commutator/collection.rb
132
+ - lib/commutator/collection/cached_lookup.rb
132
133
  - lib/commutator/expressions/attribute_names.rb
133
134
  - lib/commutator/expressions/attribute_values.rb
134
135
  - lib/commutator/expressions/condition_expression.rb