activerecord-pg_array 0.1.2 → 0.1.3

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: 5c96000d7b7ea7b00b06c661c7679210afa96e0e
4
- data.tar.gz: ba0cb25937aaadb12f1e39ad33ec74cd52495bb5
3
+ metadata.gz: ec6921049d3eebc0a2e71edb34f8ae34005e353c
4
+ data.tar.gz: 9982413e735a0f412c314227ba31c60503125298
5
5
  SHA512:
6
- metadata.gz: c35f4bce897bad15e220ecd60ad7a3b888d38e1616d6710b63d937e35fa4a821ad884ecda6a19b3c08fd4b6f683ee402599340c40cf3cc0becc7a54e674890a0
7
- data.tar.gz: 79e065f9592d795bc74b51bd53de7bb44d9fde17e3a8706fa3cdd5e951c267e0d250349c1b4db69e5ef72c7d4a67e12799e53cb89607b0aa9d862dedd37e5405
6
+ metadata.gz: a194a53424ff640c808d59208c20b8236342cf7692e0ae51abc83e73947ae7913cf3b51083c76be6afbb55f28e49d858436b93b12543515b420270ae1bc4e4b8
7
+ data.tar.gz: eed68329567993582c4c73bd0cb6831d8f4d1331889769367cd517e9348b7332052d39e8c3f3b8440e05bd913882443a4dcb4cd445580a8dc84cfb27eeed6b96
data/README.md CHANGED
@@ -21,7 +21,7 @@ Then in your classes that inherit from ActiveRecord `include ActiveRecord::PGArr
21
21
  Given the following migration:
22
22
 
23
23
  ```ruby
24
- class CreateWoldTracker < ActiveRecord::Migration
24
+ class CreateWolfTracker < ActiveRecord::Migration
25
25
  def change
26
26
  create_table :wolf_trackers do |t|
27
27
  t.string :name
@@ -48,8 +48,12 @@ The following methods are automatically defined for "wolf_ids":
48
48
  add_wolf(wolfy) # ActiveRecord object wolfy's id is appended to wolf_ids
49
49
  add_wolf!(son_of_wolfy) # wolf_ids appended with atomic update
50
50
  add_wolves([wolfia, 4]) # add multiple to wolf_ids. Note: irregular plural method name and mixed input
51
+ add_wolves!([wolfia, 4]) # add multiple to wolf_ids and persists. Note: irregular plural method name and mixed input. Can take multiple arguments or an array.
52
+ add_wolves!(wolfia, son_of_wolfy) # add multiple and persist. Can take multiple arguments or an array
51
53
  remove_wolf(wolfia) # wolf_ids is modified but not saved
52
54
  remove_wolf!(3) # wolf_ids atomic removal
55
+ remove_wolves(3, wolfia) # wolf_ids has two items removed but is not persisted. Can take multiple arguments or an array
56
+ remove_wolves!([3, wolfia]) # wolf_ids has two items removed and is persisted. Can take multiple arguments or an array
53
57
  wolves # looks up wolf objects with ids wolf_ids
54
58
  ```
55
59
 
@@ -1,5 +1,5 @@
1
1
  module Activerecord
2
2
  module PgArray
3
- VERSION = "0.1.2"
3
+ VERSION = "0.1.3"
4
4
  end
5
5
  end
@@ -48,12 +48,21 @@ module ActiveRecord
48
48
  self.update_attribute attr_name.to_sym, atr[self]
49
49
  end
50
50
 
51
- define_method :"add_#{friendly_attr_plural}" do |objs|
51
+ define_method :"add_#{friendly_attr_plural}" do |*objs|
52
52
  objs.each do |obj|
53
- self.send :"add_#{friendly_attr_singular}", obj
53
+ if obj.kind_of? Array
54
+ self.send :"add_#{friendly_attr_plural}", *obj
55
+ else
56
+ self.send :"add_#{friendly_attr_singular}", obj
57
+ end
54
58
  end
55
59
  end
56
60
 
61
+ define_method :"add_#{friendly_attr_plural}!" do |*objs|
62
+ self.send :"add_#{friendly_attr_plural}", *objs
63
+ self.save!
64
+ end
65
+
57
66
  define_method :"remove_#{friendly_attr_singular}" do |obj|
58
67
  obj = obj_convert.call(obj)
59
68
  if atr[self].include?(obj)
@@ -67,6 +76,20 @@ module ActiveRecord
67
76
  self.save!
68
77
  end
69
78
 
79
+ define_method :"remove_#{friendly_attr_plural}" do |*objs|
80
+ objs.each do |obj|
81
+ if obj.kind_of? Array
82
+ self.send :"remove_#{friendly_attr_plural}", *obj
83
+ else
84
+ self.send :"remove_#{friendly_attr_singular}", obj
85
+ end
86
+ end
87
+ end
88
+
89
+ define_method :"remove_#{friendly_attr_plural}!" do |*objs|
90
+ self.send :"remove_#{friendly_attr_plural}", *objs
91
+ self.save!
92
+ end
70
93
 
71
94
  # define basic relational lookup methods
72
95
  # example:
@@ -41,8 +41,11 @@ describe WolfTracker do
41
41
  it { should respond_to(:add_pack_name) }
42
42
  it { should respond_to(:add_pack_name!) }
43
43
  it { should respond_to(:add_pack_names) }
44
+ it { should respond_to(:add_pack_names!) }
44
45
  it { should respond_to(:remove_pack_name) }
45
46
  it { should respond_to(:remove_pack_name!) }
47
+ it { should respond_to(:remove_pack_names) }
48
+ it { should respond_to(:remove_pack_names!) }
46
49
  end
47
50
 
48
51
  context 'persistence' do
@@ -69,6 +72,18 @@ describe WolfTracker do
69
72
  expect(wolf_tracker.name).to eq 'Wolfram'
70
73
  end
71
74
 
75
+ it "should add multiple add_<atts>! with multiple arguments" do
76
+ wolf_tracker.add_pack_names!('red fang', 'midnight howlers')
77
+ wolf_tracker.reload
78
+ wolf_tracker.pack_names.length.should be 2
79
+ end
80
+
81
+ it "should add multiple add_<atts>! with an array argument" do
82
+ wolf_tracker.add_pack_names!(['red fang', 'midnight howlers'])
83
+ wolf_tracker.reload
84
+ wolf_tracker.pack_names.length.should be 2
85
+ end
86
+
72
87
  it "should add with regular integer value" do
73
88
  wolf_tracker.add_wolf(1)
74
89
  wolf_tracker.save!
@@ -101,6 +116,31 @@ describe WolfTracker do
101
116
  wolf_tracker.remove_wolf!(wolfy.id)
102
117
  expect(wolf_tracker.reload.wolf_ids).to be_empty
103
118
  end
119
+
120
+ context "multiple" do
121
+ before do
122
+ wolf_tracker.add_wolf!(son_of_wolfy)
123
+ expect(wolf_tracker.wolf_ids.length).to eq 2
124
+ end
125
+
126
+ it "should be called with an array argument" do
127
+ wolf_tracker.remove_wolves([wolfy, son_of_wolfy])
128
+ expect(wolf_tracker.wolf_ids.length).to eq 0
129
+ expect(wolf_tracker.reload.wolf_ids.length).to eq 2
130
+ end
131
+
132
+ it "should be called with multiple arguments" do
133
+ wolf_tracker.remove_wolves(wolfy, son_of_wolfy)
134
+ expect(wolf_tracker.wolf_ids.length).to eq 0
135
+ expect(wolf_tracker.reload.wolf_ids.length).to eq 2
136
+ end
137
+
138
+ it "should remove multiple and persist with !" do
139
+ wolf_tracker.remove_wolves!(wolfy, son_of_wolfy)
140
+ expect(wolf_tracker.reload.wolf_ids.length).to eq 0
141
+ end
142
+
143
+ end
104
144
  end # remove
105
145
 
106
146
  it 'should have more specs' do
@@ -111,7 +151,7 @@ describe WolfTracker do
111
151
 
112
152
  describe "finder methods" do
113
153
  before do
114
- wolf_tracker.add_wolves([wolfy, son_of_wolfy])
154
+ wolf_tracker.add_wolves(wolfy, son_of_wolfy)
115
155
  wolf_tracker.save!
116
156
  end
117
157
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-pg_array
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sean McCleary
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-27 00:00:00.000000000 Z
11
+ date: 2014-06-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -173,4 +173,3 @@ test_files:
173
173
  - spec/schema.rb
174
174
  - spec/spec_helper.rb
175
175
  - spec/support/active_record.rb
176
- has_rdoc: