acts_as_shardable 0.1.0 → 0.2.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: ae15680670e93665402087c8a21b0e5f1d0f74cb
4
- data.tar.gz: 87b4bcfdc7b370e6b4a5612b48bb1b6645260871
3
+ metadata.gz: 8a9466ea6e8a41550e3841d5c102b3b4d0e8eedb
4
+ data.tar.gz: 9435090d2a582ed16af98ddefaebfd7e856ca8ce
5
5
  SHA512:
6
- metadata.gz: cb30b6784fc08f58e3a3b2f6bb55ea883c49ec6a06d6cf89b1b6652b5b907b3d0a9be3ab9d636a577bda21e540df79384069da4f5cc018997d1dd44c4abc3d58
7
- data.tar.gz: 15809aa7d444d35546fa70416415d83a34eeae3fcbd4ed1c787861de56b9df9cc9c8dfde64915f123b882eefc8254b066de326b0dc4997f20782c59e3adb1950
6
+ metadata.gz: cd4e8d246d4275f0de40694b306c6a1daaeec81a4f93ad6096dc8791efc9ca1db8d6fba65beeefe6a031fe9791f0029fcdcb9aa5b950ebcd31795b80b0ea90d9
7
+ data.tar.gz: 00ca76beffcd57f220e69409020bc5d3accfd6de3f4e1f317acdfc7717ce421b32826c8bd4ae05a6872e7bea39535f737f63bda026c96eafd14883dbe52fa176
data/README.md CHANGED
@@ -1,9 +1,5 @@
1
1
  # ActsAsShardable
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/acts_as_shardable`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
6
-
7
3
  ## Installation
8
4
 
9
5
  Add this line to your application's Gemfile:
@@ -22,13 +18,61 @@ Or install it yourself as:
22
18
 
23
19
  ## Usage
24
20
 
25
- TODO: Write usage instructions here
21
+ Example
22
+
23
+ In your model:
24
+
25
+ ```ruby
26
+ class Mod2Model < ActiveRecord::Base
27
+ acts_as_shardable by: :hash_id, mod: 2
28
+ end
29
+ ```
30
+
31
+ and migrations:
32
+
33
+ ```ruby
34
+ class CreateMod2Models < ActiveRecord::Migration
35
+ def self.up
36
+ shards.times do |i|
37
+ create_table("mod2_models_%04d" % i) do |t|
38
+ t.integer :hash_id
39
+ end
40
+ end
41
+ end
42
+
43
+ def self.down
44
+ shards.times do |i|
45
+ drop_table("cc_study_durations_%04d" % i)
46
+ end
47
+ end
48
+
49
+ def self.shards
50
+ 2
51
+ end
52
+ end
53
+ ```
54
+
55
+ Then if you call
56
+
57
+ ```ruby
58
+ Mod2Model.create(hash: 1)
59
+ ```
60
+
61
+ it will save the record into `mod2_models_0001`.
62
+
63
+ And
64
+
65
+ ```ruby
66
+ Mod2Model.create(hash: 2)
67
+ ```
68
+
69
+ will save the record into `mod2_models_0002`.
26
70
 
27
71
  ## Development
28
72
 
29
73
  After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
74
 
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
75
+ run `rake test` to run tests.
32
76
 
33
77
  ## Contributing
34
78
 
data/Rakefile CHANGED
@@ -1,2 +1,11 @@
1
1
  require "bundler/gem_tasks"
2
- task :default => :spec
2
+
3
+ require 'rake/testtask'
4
+
5
+ task :default => [:test]
6
+
7
+ Rake::TestTask.new do |test|
8
+ test.libs << 'test'
9
+ test.test_files = Dir[ 'test/**/test_*.rb' ]
10
+ test.verbose = true
11
+ end
@@ -10,16 +10,16 @@ module ActsAsShardable
10
10
  Mutex.new
11
11
  end
12
12
 
13
- def acts_as_shardable(column:, mod:)
14
- class_attribute :shard_column, :shard_mod, :module_name, :base_table_name
13
+ def acts_as_shardable(by:, mod:)
14
+ class_attribute :shard_method, :shard_mod, :module_name, :base_table_name
15
15
 
16
16
  mutex.synchronize do
17
- self.shard_column = column
17
+ self.shard_method = by
18
18
  self.shard_mod = mod
19
19
  self.module_name = self.name.deconstantize.safe_constantize || Object
20
20
  self.base_table_name = self.name.demodulize.pluralize.underscore
21
21
  self.table_name = "#{self.base_table_name}_0000"
22
- self.validates self.shard_column.to_sym, presence: true
22
+ self.validates self.shard_method.to_sym, presence: true
23
23
 
24
24
  # Updates the associated record with values matching those of the instance attributes.
25
25
  # Returns the number of affected rows.
@@ -28,7 +28,7 @@ module ActsAsShardable
28
28
  if attributes_values.empty?
29
29
  0
30
30
  else
31
- was, is = changes[self.class.base_class.shard_column]
31
+ was, is = changes[self.class.base_class.shard_method]
32
32
  if was
33
33
  transaction do
34
34
  self.class.base_class.sharding(is).unscoped.insert attributes_values
@@ -42,7 +42,7 @@ module ActsAsShardable
42
42
 
43
43
  1
44
44
  else
45
- self.class.base_class.sharding(self[self.class.base_class.shard_column]).unscoped._update_record attributes_values, id, id_was
45
+ self.class.base_class.sharding(public_send(self.class.base_class.shard_method)).unscoped._update_record attributes_values, id, id_was
46
46
  end
47
47
  end
48
48
  end
@@ -54,7 +54,7 @@ module ActsAsShardable
54
54
  define_method :_create_record do |attribute_names = self.attribute_names|
55
55
  attributes_values = arel_attributes_with_values_for_create(attribute_names)
56
56
 
57
- new_id = self.class.base_class.sharding(self[self.class.base_class.shard_column]).unscoped.insert attributes_values
57
+ new_id = self.class.base_class.sharding(public_send(self.class.base_class.shard_method)).unscoped.insert attributes_values
58
58
  self.id ||= new_id if self.class.base_class.primary_key
59
59
 
60
60
  @new_record = false
@@ -1,3 +1,3 @@
1
1
  module ActsAsShardable
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_shardable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - scorix
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-05-05 00:00:00.000000000 Z
11
+ date: 2016-05-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler