activerecord-shard_for 0.3.0 → 0.4.0

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: cad523e3ec520b0697848ece57f5703c5d049ea0
4
- data.tar.gz: cf3f36c9de0afd5f6f42addc95d9f29081a5d51a
3
+ metadata.gz: 7e7b2e6d1521f83ec993bccc75ed606b39792a9b
4
+ data.tar.gz: ba474a4e900eabfb0bd551da9514111b9322e6df
5
5
  SHA512:
6
- metadata.gz: 18da70aea7e423466d39cd5c677b469320cc18e57f475e60fa82bde5c4370de58bf3c000f8e91c38371ead208842ea91cc848fa3998d257b1bb48171bece222a
7
- data.tar.gz: 35261193e6150eefd81f8f323912254edc6448493a701dfaf952edfe3c75bdd8be1ba8c864d7d2e6cf57d415f98fb017de328dd8c545699920b419003d81302f
6
+ metadata.gz: 5d5fbe559b15b32b65313380b4aa3d433ffcd01379f19fb756612d7c0f831cd9046279c37e876e5edddac23ae13c5de3bd0d0608a89bd71ff8594b655173f3c6
7
+ data.tar.gz: 3df865e864a0e7c39faebb0907630353dce1f2d0663747893f111574ea74b63f1b2d1deac38b2f449ed6c4a53d31ef9ab9236fded931dae3acd163160b511c9c
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # CHANGELOG for activerecord-shard_for
2
2
 
3
+ ## 0.4.0
4
+
5
+ - Distkey support to instance method. [#9](https://github.com/yuemori/activerecord-shard_for/pull/9)
6
+
3
7
  ## 0.3.0
4
8
 
5
9
  - Add using syntax. [#8](https://github.com/yuemori/activerecord-shard_for/pull/8)
data/README.md CHANGED
@@ -12,19 +12,6 @@ This is Sharding Library for ActiveRecord, inspire and import codes from [mixed_
12
12
  - small
13
13
  - pluggable
14
14
 
15
- ### Simple
16
-
17
- This idea principal take over from `mixed_gauge`, and `activerecord-sharding`. `activerecord-shard_for` needs minimum and simply of settings.
18
-
19
- ### Small
20
-
21
- - Small Dependency: To facilitate version up (important!).
22
- - Small code: Easy to read code when cause trouble.
23
-
24
- ### pluggable
25
-
26
- Default sharding rule is [modulo with hashed key](https://github.com/yuemori/activerecord-shard_for/blob/master/lib/activerecord/shard_for/hash_modulo_router.rb). But, `activerecord-shard_for` adopt pluggable structer. This rule can be easy to change!
27
-
28
15
  ## Installation
29
16
 
30
17
  Add this line to your application's Gemfile:
@@ -43,8 +30,6 @@ Or install it yourself as:
43
30
 
44
31
  # Getting Started
45
32
 
46
- More example to see [wiki](https://github.com/yuemori/activerecord-shard_for/wiki).
47
-
48
33
  Add additional database connection config to database.yml.
49
34
 
50
35
  ```yaml
@@ -105,6 +90,11 @@ alice.save!
105
90
  User.all_shards.flat_map {|m| m.find_by(name: 'alice') }.compact
106
91
  ```
107
92
 
93
+ ## Wiki
94
+
95
+ More imformation and example to see [wiki](https://github.com/yuemori/activerecord-shard_for/wiki)!
96
+
97
+
108
98
  ## Contributing with ActiveRecord::ShardFor
109
99
 
110
100
  Contributors are welcome! This is what you need to setup your Octopus development environment:
@@ -50,11 +50,11 @@ module ActiveRecord
50
50
  # @raise [ActiveRecord::ShardFor::MissingDistkeyAttribute]
51
51
  def put!(attributes)
52
52
  raise '`distkey` is not defined. Use `def_distkey`.' unless distkey
53
-
54
53
  @before_put_callback.call(attributes) if defined?(@before_put_callback) && @before_put_callback
55
- key = attributes[distkey]
56
54
 
57
- raise ActiveRecord::ShardFor::MissingDistkeyAttribute unless key || attributes[distkey.to_s]
55
+ key = fetch_distkey_from_attributes(attributes)
56
+
57
+ raise ActiveRecord::ShardFor::MissingDistkeyAttribute unless key
58
58
 
59
59
  shard_for(key).create!(attributes)
60
60
  end
@@ -139,6 +139,20 @@ module ActiveRecord
139
139
  def switch(role_name, &block)
140
140
  replication_mapping.switch(self, role_name, &block)
141
141
  end
142
+
143
+ private
144
+
145
+ # @param [Hash] attributes
146
+ # @return [Object or nil] distkey
147
+ def fetch_distkey_from_attributes(attributes)
148
+ key = attributes[distkey] || attributes[distkey.to_s]
149
+ return key if key
150
+
151
+ instance = all_shards.first.new(attributes)
152
+ return unless instance.respond_to?(distkey)
153
+
154
+ instance.send(distkey)
155
+ end
142
156
  end
143
157
  end
144
158
  end
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module ShardFor
3
- VERSION = '0.3.0'.freeze
3
+ VERSION = '0.4.0'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-shard_for
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - yuemori