atomic_arrays 1.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7f109dc67250882c4425341e1c12ae2abd8d9b80
4
+ data.tar.gz: fce753b3c188ca2a2ae971e68af482e2bce202b3
5
+ SHA512:
6
+ metadata.gz: 8e3d5194b39f156b5035e671846f1714a1a4ad85b4b3ee921ebddb2c030aae715381dd24b0bb61aff0858dc09ab38fe04feaf669dc7f23d67a26fc25cd841c97
7
+ data.tar.gz: 57fdedc6dbf69f9145c522b9c237ea328c84b845fc83efe87592c6f4e28264497986fd3450c5d4c7507acdddda83f26ac5993471128e65c5ca696a5b8a691283
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in atomic_arrays.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Joey
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # AtomicArrays
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'atomic_arrays'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install atomic_arrays
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( https://github.com/[my-github-username]/atomic_arrays/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,38 @@
1
+ require "bundler/gem_tasks"
2
+ require "rubygems"
3
+ require "bundler/setup"
4
+
5
+ require "pg"
6
+ require "active_record"
7
+ require "yaml"
8
+ require 'rspec/core/rake_task'
9
+
10
+ namespace :db do
11
+
12
+ desc "Migrate the db"
13
+ task :migrate do
14
+ connection_details = YAML.load_file(File.expand_path("../spec/db/database.yml", __FILE__))["test"]
15
+ ActiveRecord::Base.establish_connection(connection_details)
16
+ ActiveRecord::Migrator.migrate("spec/db/")
17
+ end
18
+
19
+
20
+ desc "Create the db"
21
+ task :create do
22
+ connection_details = YAML.load_file(File.expand_path("../spec/db/database.yml", __FILE__))["test"]
23
+ ActiveRecord::Base.establish_connection(connection_details.merge({'database' => 'postgres', 'schema_search_path' => 'public'}))
24
+ ActiveRecord::Base.connection.create_database(connection_details.fetch('database'))
25
+ end
26
+
27
+ desc "drop the db"
28
+ task :drop do
29
+ connection_details = YAML.load_file(File.expand_path("../spec/db/database.yml", __FILE__))["test"]
30
+ ActiveRecord::Base.establish_connection(connection_details.merge({'database' => 'postgres', 'schema_search_path' => 'public'}))
31
+ ActiveRecord::Base.connection.drop_database(connection_details.fetch('database'))
32
+ end
33
+ end
34
+
35
+ RSpec::Core::RakeTask.new(:spec)
36
+ Dir.glob('tasks/**/*.rake').each(&method(:import))
37
+
38
+ task default: :spec
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'atomic_arrays/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "atomic_arrays"
8
+ spec.version = AtomicArrays::VERSION
9
+ spec.authors = ["Joseph"]
10
+ spec.email = ["joseph.cs.ritchie@gmail.com"]
11
+ spec.summary = %q{ActiveRecord extension for atomically updating PostgreSQL arrays.}
12
+ spec.description = %q{AtomicArrays aims to assist ActiveRecord with updating Postgres arrays
13
+ by offering a couple simple methods to change arrays in both the database
14
+ and the instance it is called on. These methods are atomic in nature
15
+ because they update the arrays in the database without relying on the current
16
+ object's instantiated arrays.}
17
+ spec.homepage = "https://github.com/twincharged/atomic_arrays"
18
+ spec.license = "MIT"
19
+
20
+ spec.files = `git ls-files -z`.split("\x0")
21
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
22
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.6"
26
+ spec.add_development_dependency "rake", ">= 10.3.2"
27
+ spec.add_development_dependency "rspec", ">= 3.0.0"
28
+ spec.add_development_dependency "pg", ">= 0.17.1"
29
+
30
+ spec.add_runtime_dependency "activerecord", ">= 4.0"
31
+ end
@@ -0,0 +1,3 @@
1
+ module AtomicArrays
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,63 @@
1
+ require "atomic_arrays/version"
2
+
3
+ module AtomicArrays
4
+
5
+ def self.included(klass)
6
+ klass.class_eval do
7
+ extend AtomicClassMethods
8
+ end
9
+ end
10
+
11
+ def atomic_append(field, value)
12
+ raise "Cannot append multiple values." if value.is_a?(Array)
13
+ value = prepare_array_vals(value)
14
+ return self.execute_array_query(field, value, "array_append")
15
+ end
16
+
17
+ def atomic_remove(field, value)
18
+ raise "Cannot remove multiple values." if value.is_a?(Array)
19
+ value = prepare_array_vals(value)
20
+ return self.execute_array_query(field, value, "array_remove")
21
+ end
22
+
23
+ def atomic_cat(field, values)
24
+ raise "Cannot cat strings or integers" if (values.is_a?(Integer) || values.is_a?(String))
25
+ values = prepare_array_vals(values)
26
+ return self.execute_array_query(field, "ARRAY[#{values}]", "array_cat")
27
+ end
28
+
29
+ def atomic_relate(field, related_class, limit=100)
30
+ raise "Relates to a class, not a string or integer." if (related_class.is_a?(Integer) || related_class.is_a?(String))
31
+ (table, field) = self.prepare_array_query(field)
32
+ related_table = related_class.table_name.inspect
33
+ return result = related_class.execute_and_wrap(%Q{SELECT #{related_table}.* FROM #{related_table} WHERE #{related_table}.id IN (SELECT unnest(#{table}.#{field}) FROM #{table} WHERE #{table}.id = #{self.id}) LIMIT #{limit}})
34
+ end
35
+
36
+ def execute_array_query(field, value, array_method)
37
+ (table, field) = self.prepare_array_query(field)
38
+ result = self.class.execute_and_wrap(%Q{UPDATE #{table} SET #{field} = #{array_method}(#{field}, #{value}) WHERE #{table}.id = #{self.id} RETURNING #{table}.*})
39
+ return result[0]
40
+ end
41
+
42
+ def prepare_array_query(field)
43
+ table = self.class.table_name.inspect
44
+ field = field.to_s.inspect
45
+ return [table, field]
46
+ end
47
+
48
+ def prepare_array_vals(value)
49
+ prep_array = []
50
+ [*value].map {|val| val = "\'#{val}\'" if val.class == String; prep_array.push(val)}
51
+ return prep_array.join(", ")
52
+ end
53
+
54
+ module AtomicClassMethods
55
+ def execute_and_wrap(sql, binds=[])
56
+ result_set = self.connection.select_all(self.sanitize_sql(sql), "#{self.name} Load", binds)
57
+ column_types = {}
58
+ column_types = result_set.column_types if result_set.respond_to?(:column_types)
59
+ return result_set.map { |record| self.instantiate(record, column_types) }
60
+ end
61
+ end
62
+
63
+ end
@@ -0,0 +1,135 @@
1
+ require 'spec_helper'
2
+
3
+ describe AtomicArrays do
4
+
5
+ before(:all) do
6
+ @user1 = User.find(1)
7
+ @user2 = User.find(2)
8
+ @user3 = User.find(3)
9
+ @user4 = User.find(4)
10
+ @user5 = User.find(5)
11
+ @user6 = User.find(6)
12
+ @comment1 = Comment.find(1)
13
+ @comment2 = Comment.find(2)
14
+ @comment3 = Comment.find(3)
15
+ @comment4 = Comment.find(4)
16
+ @comment5 = Comment.find(5)
17
+ @comment6 = Comment.find(6)
18
+ end
19
+
20
+ describe "atomic appending" do
21
+
22
+ it "should append string to user" do
23
+ hob = "NewHobby"
24
+ user = @user1.atomic_append(:hobbies, hob)
25
+ expect(user.hobbies).to include hob
26
+ expect(User.find(1).hobbies).to include hob
27
+ end
28
+
29
+ it "should append string to comment" do
30
+ tag = "NewTag"
31
+ comm = @comment1.atomic_append(:tags, tag)
32
+ expect(comm.tags).to include tag
33
+ expect(Comment.find(1).tags).to include tag
34
+ end
35
+
36
+ it "should append integer to user" do
37
+ id = 9
38
+ user = @user2.atomic_append(:comment_ids, id)
39
+ expect(user.comment_ids).to include id
40
+ expect(User.find(2).comment_ids).to include id
41
+ end
42
+
43
+ it "should append string to comment" do
44
+ id = 8
45
+ comm = @comment2.atomic_append(:liker_ids, id)
46
+ expect(comm.liker_ids).to include id
47
+ expect(Comment.find(2).liker_ids).to include id
48
+ end
49
+ end
50
+
51
+
52
+ describe "atomic removing" do
53
+
54
+ it "should remove string to user" do
55
+ hob = "NewHobby"
56
+ user = @user1.atomic_remove(:hobbies, hob)
57
+ expect(user.hobbies).to_not include hob
58
+ expect(User.find(1).hobbies).to_not include hob
59
+ end
60
+
61
+ it "should remove string to comment" do
62
+ tag = "NewTag"
63
+ comm = @comment1.atomic_remove(:tags, tag)
64
+ expect(comm.tags).to_not include tag
65
+ expect(Comment.find(1).tags).to_not include tag
66
+ end
67
+
68
+ it "should remove integer to user" do
69
+ id = 9
70
+ user = @user2.atomic_remove(:comment_ids, id)
71
+ expect(user.comment_ids).to_not include id
72
+ expect(User.find(2).comment_ids).to_not include id
73
+ end
74
+
75
+ it "should remove string to comment" do
76
+ id = 8
77
+ comm = @comment2.atomic_remove(:liker_ids, id)
78
+ expect(comm.liker_ids).to_not include id
79
+ expect(Comment.find(2).liker_ids).to_not include id
80
+ end
81
+ end
82
+
83
+
84
+
85
+ describe "atomic concatenating" do
86
+
87
+ it "should cat strings to user" do
88
+ hobs = ["NewHobby", "Anothernewhobby", "Hobby3"]
89
+ user = @user3.atomic_cat(:hobbies, hobs)
90
+ expect(user.hobbies).to include *hobs
91
+ expect(User.find(3).hobbies).to include *hobs
92
+ end
93
+
94
+ it "should cat strings to comment" do
95
+ tags = ["NewTag", "#sick", "#iheartfood"]
96
+ comm = @comment3.atomic_cat(:tags, tags)
97
+ expect(comm.tags).to include *tags
98
+ expect(Comment.find(3).tags).to include *tags
99
+ end
100
+
101
+ it "should cat integers to user" do
102
+ ids = [9,234,12,7]
103
+ user = @user3.atomic_cat(:comment_ids, ids)
104
+ expect(user.comment_ids).to include *ids
105
+ expect(User.find(3).comment_ids).to include *ids
106
+ end
107
+
108
+ it "should cat strings to comment" do
109
+ ids = [8,98,43,22]
110
+ comm = @comment3.atomic_cat(:liker_ids, ids)
111
+ expect(comm.liker_ids).to include *ids
112
+ expect(Comment.find(3).liker_ids).to include *ids
113
+ end
114
+ end
115
+
116
+
117
+
118
+ describe "atomic relating" do
119
+
120
+ it "should relate comment id array" do
121
+ comm = [@comment1,@comment2,@comment4]
122
+ @user3.update({comment_ids: comm.map(&:id)})
123
+ comments = @user3.atomic_relate(:comment_ids, Comment)
124
+ expect(comments).to include *comm
125
+ end
126
+
127
+ it "should relate user id array" do
128
+ us = [@user1,@user2,@user4]
129
+ @comment5.update({liker_ids: us.map(&:id)})
130
+ users = @comment5.atomic_relate(:liker_ids, User)
131
+ expect(users).to include *us
132
+ end
133
+ end
134
+
135
+ end
@@ -0,0 +1,12 @@
1
+ class CreateUsers < ActiveRecord::Migration
2
+ def change
3
+ create_table :users, force: true do |t|
4
+ t.string :name
5
+ t.integer :age
6
+ t.text :hobbies, array: true, default: []
7
+ t.integer :comment_ids, array: true, default: []
8
+
9
+ t.timestamps
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ class CreateComments < ActiveRecord::Migration
2
+ def change
3
+ create_table :comments, force: true do |t|
4
+ t.integer :user_id
5
+ t.text :body
6
+ t.text :tags, array: true, default: []
7
+ t.integer :liker_ids, array: true, default: []
8
+
9
+ t.timestamps
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,6 @@
1
+ test:
2
+ adapter: postgresql
3
+ encoding: unicode
4
+ database: atomictest
5
+ pool: 5
6
+ host: localhost
@@ -0,0 +1,31 @@
1
+ require "atomic_arrays"
2
+ require "active_record"
3
+ require "rspec"
4
+ require "yaml"
5
+
6
+ ActiveRecord::Base.establish_connection(YAML.load_file(File.expand_path("../db/database.yml", __FILE__))["test"])
7
+ # load File.dirname(__FILE__) + '/migrations.rb'
8
+
9
+
10
+ class User < ActiveRecord::Base
11
+ include AtomicArrays
12
+ end
13
+
14
+
15
+ class Comment < ActiveRecord::Base
16
+ include AtomicArrays
17
+ end
18
+
19
+ Comment.create({id: 1, user_id: 1, body: "from user 1!!!", tags: ["#fun"]})
20
+ Comment.create({id: 2, user_id: 2, body: "from user 2!!!", liker_ids: [4,5]})
21
+ Comment.create({id: 3, user_id: 3, body: "from user 2!!!", tags: ["#fun", "#coolpostbro"]})
22
+ Comment.create({id: 4, user_id: 3, body: "from user 3!!!", liker_ids: [2,3]})
23
+ Comment.create({id: 5, user_id: 5, body: "from user 4!!!", tags: ["#yay"], liker_ids: [1,2]})
24
+ Comment.create({id: 6, user_id: 5, body: "from user 5!!!", liker_ids: [1]})
25
+
26
+ User.create({id: 1, name: "John", age: 34, hobbies: ["skateboarding"], comment_ids: [1]})
27
+ User.create({id: 2, name: "James", age: 25, hobbies: ["eating", "basketball"], comment_ids: [2]})
28
+ User.create({id: 3, name: "Jane", age: 27, hobbies: ["hanging out"], comment_ids: [3, 4]})
29
+ User.create({id: 4, name: "Bill", age: 22, hobbies: ["Studying", "Online chatting"]})
30
+ User.create({id: 5, name: "Natasha", age: 41, hobbies: ["Nothing"], comment_ids: [5, 6]})
31
+ User.create({id: 6, name: "Courtney", age: 32})
metadata ADDED
@@ -0,0 +1,137 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: atomic_arrays
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Joseph
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 10.3.2
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 10.3.2
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 3.0.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 3.0.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: pg
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 0.17.1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 0.17.1
69
+ - !ruby/object:Gem::Dependency
70
+ name: activerecord
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '4.0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '4.0'
83
+ description: |-
84
+ AtomicArrays aims to assist ActiveRecord with updating Postgres arrays
85
+ by offering a couple simple methods to change arrays in both the database
86
+ and the instance it is called on. These methods are atomic in nature
87
+ because they update the arrays in the database without relying on the current
88
+ object's instantiated arrays.
89
+ email:
90
+ - joseph.cs.ritchie@gmail.com
91
+ executables: []
92
+ extensions: []
93
+ extra_rdoc_files: []
94
+ files:
95
+ - ".gitignore"
96
+ - Gemfile
97
+ - LICENSE.txt
98
+ - README.md
99
+ - Rakefile
100
+ - atomic_arrays.gemspec
101
+ - lib/atomic_arrays.rb
102
+ - lib/atomic_arrays/version.rb
103
+ - spec/atomic_arrays_spec.rb
104
+ - spec/db/001_create_users.rb
105
+ - spec/db/002_create_comments.rb
106
+ - spec/db/database.yml
107
+ - spec/spec_helper.rb
108
+ homepage: https://github.com/twincharged/atomic_arrays
109
+ licenses:
110
+ - MIT
111
+ metadata: {}
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubyforge_project:
128
+ rubygems_version: 2.2.2
129
+ signing_key:
130
+ specification_version: 4
131
+ summary: ActiveRecord extension for atomically updating PostgreSQL arrays.
132
+ test_files:
133
+ - spec/atomic_arrays_spec.rb
134
+ - spec/db/001_create_users.rb
135
+ - spec/db/002_create_comments.rb
136
+ - spec/db/database.yml
137
+ - spec/spec_helper.rb