cassandro 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- MGY2MDljZDAzYjFhOGZmYzZhZjM4MjE0YTcyOTIwNDYxZTIwNTQ4OA==
5
- data.tar.gz: !binary |-
6
- Y2M3MTA2ZTVjMGJkOTZjNTdiNWZmNGY0NzE0YzU4ZjMyNTM3NmFmNw==
2
+ SHA1:
3
+ metadata.gz: 0120288029f77b9b0c1f5decbf83dd3aa28ee3f6
4
+ data.tar.gz: 3f50c226fb664ccbd79eb2e95b6c638b0a0caaf0
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- MjNjYjQ5MjA5MmJmZGNmMjVlZWE3NWRkNzdlYWQ5OTg1NWVhZjlhMmU0OTE1
10
- N2E5NjNjZGVlNTFjZDE2NDAwNTJmMWY4ZmIyMzRkMWMyN2M1ODU1OTE0ODBi
11
- MDhkODlhN2FmMjhkNWM3NjJlYWRiNzBhNmRlZDU5MjFlZTNlYWE=
12
- data.tar.gz: !binary |-
13
- MDc2OTk4YTA4NDc3MzI1MGZmMmViMWYxYjdjMjBkNGFiYTIyNjI4MmVlMDIz
14
- ZGM5N2EzZDk1Y2ZlYjM0N2IwMmJmODM1NWU4ZWNkNDkxMmM3NDFjYmUzMTIy
15
- ZTdlZDQ4MmRhMGRlOTM1YzAwMGNjYTZmYmMwZTM0YTU4MjY2ZGY=
6
+ metadata.gz: 734ad77b4020a932713e66d2618815798c14b850a0243fd3f36c2d621dcaaec4b4d937f7c4a66c624dc13c653618039c55534bb2d904c7c28569bd6587aac153
7
+ data.tar.gz: 97d3d021c5808aa343988e8781beaee4327756fe52a7a68304caed7e1b8657dc0446b2981a42de502a9b8b8e8a6329bf0f398ab487b26857cf303c529d4a7b8b
data/.gitignore CHANGED
@@ -1 +1,2 @@
1
1
  *.gem
2
+ .ruby-version
data/README.md CHANGED
@@ -119,6 +119,32 @@ class User < Cassandro::Model
119
119
  end
120
120
  ```
121
121
 
122
+
123
+ ### Setting TTL to a model
124
+
125
+ ```Ruby
126
+ class Person < Cassandro::Model
127
+ table :people
128
+ ttl 60
129
+ end
130
+ ```
131
+ This will make all the instances of the People class will have a Time To Live of `60` seconds in the database.
132
+
133
+ Creating a single record with a given TTL:
134
+
135
+ ```Ruby
136
+ class Person < Cassandro::Model
137
+ table :people
138
+ attribute :first_name, :text
139
+ attribute :last_name, :text
140
+ end
141
+
142
+ Person.create_with_ttl(20, :first_name => "Eddie", :last_name => "Vedder")
143
+ ```
144
+
145
+ This will create a record in the `people` table with a TTL of `20` seconds. It doesn't matter if the model has a different TTL set, this will override that TTL for _this record only_
146
+
147
+
122
148
  __A complete example__
123
149
 
124
150
  ```ruby
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "cassandro"
5
- s.version = "1.1.0"
5
+ s.version = "1.2.0"
6
6
  s.summary = "Ruby ORM for Apache Cassandra"
7
7
  s.license = "MIT"
8
8
  s.description = "Lightweight Apache Cassandra ORM for Ruby"
@@ -10,6 +10,7 @@ module Cassandro
10
10
  @persisted = persisted
11
11
 
12
12
  attrs.each do |att, val|
13
+ next unless self.respond_to?(:"#{att}=")
13
14
  case val.class.name
14
15
  when "Set"
15
16
  send(:"#{att}=", val.to_a)
@@ -173,7 +174,7 @@ module Cassandro
173
174
 
174
175
  return nil unless result.any?
175
176
 
176
- self.new(result.first, true)
177
+ self.new(self.parse_row(result.first), true)
177
178
  end
178
179
 
179
180
  def self.create(attrs = {})
@@ -189,7 +190,7 @@ module Cassandro
189
190
  rows = Cassandro.execute(query)
190
191
  all = []
191
192
  rows.each do |row|
192
- all << new(row, true)
193
+ all << new(self.parse_row(row), true)
193
194
  end
194
195
  all
195
196
  end
@@ -289,6 +290,24 @@ module Cassandro
289
290
  pk.first
290
291
  end
291
292
 
293
+ def self.options
294
+ @options ||= {}
295
+ end
296
+
297
+ def self.ttl(seconds)
298
+ self.options[:ttl] = seconds.to_i
299
+ end
300
+
301
+ def self.create_with_ttl(seconds, attrs = {})
302
+ old_ttl = self.options[:ttl]
303
+ self.options[:ttl] = seconds.to_i
304
+
305
+ result = self.create(attrs)
306
+
307
+ old_ttl ? self.options[:ttl] = old_ttl : self.options.delete(:ttl)
308
+ result
309
+ end
310
+
292
311
  def statement_for(operation, options = {})
293
312
  case operation
294
313
  when :insert
@@ -297,6 +316,7 @@ module Cassandro
297
316
  INSERT INTO #{self.class.table_name}(#{@attributes.keys.map { |x| x.to_s}.join(',')})
298
317
  VALUES(#{@attributes.keys.map { |x| '?' }.join(",")})
299
318
  #{options[:insert_check] ? 'IF NOT EXISTS' : ''}
319
+ #{self.class.options[:ttl] && self.class.options[:ttl] > 0 ? "USING TTL #{self.class.options[:ttl]}" : ''}
300
320
  QUERY
301
321
  if @insert_statement.nil? ||
302
322
  @insert_statement.params_metadata.count != @attributes.count
@@ -319,6 +339,8 @@ module Cassandro
319
339
  end
320
340
 
321
341
  case self.class.casts[k]
342
+ when :set
343
+ n_attrs << Set.new(attrs[k])
322
344
  when :uuid
323
345
  n_attrs << Cassandra::Uuid.new(attrs[k])
324
346
  when :integer
@@ -333,5 +355,9 @@ module Cassandro
333
355
  end
334
356
  n_attrs
335
357
  end
358
+
359
+ def self.parse_row(row)
360
+ row.each{ |k,v| row[k] = [] if casts[k.to_sym] == :set && v.nil? }
361
+ end
336
362
  end
337
363
  end
@@ -153,6 +153,14 @@ Protest.describe "Cassandro Model" do
153
153
 
154
154
  assert_equal 1, Test.count(:test_col_1, Cassandra::Uuid.new(uuid))
155
155
  end
156
+
157
+ test "ignore columns not defined in model" do
158
+ uuid = SecureRandom.uuid
159
+ Test.create(test_col_1: uuid, test_col_2: 'test_value_2')
160
+
161
+ tests = Test[uuid]
162
+ assert_equal false, tests.respond_to?("test_col_3")
163
+ end
156
164
  end
157
165
 
158
166
  context 'Updating' do
@@ -184,4 +192,60 @@ Protest.describe "Cassandro Model" do
184
192
  assert_equal "PRIMARY KEY part name found in SET part", patient.errors[:update_error]
185
193
  end
186
194
  end
195
+
196
+ context 'TTL' do
197
+ setup do
198
+ class Test < Cassandro::Model
199
+ table 'tests'
200
+
201
+ ttl 20
202
+
203
+ attribute :test_col_1, :uuid
204
+ attribute :test_col_2, :text
205
+
206
+ primary_key :test_col_1
207
+ unique :test_col_1
208
+ end
209
+
210
+ class Patient < Cassandro::Model
211
+ table :patients
212
+
213
+ attribute :name, :text
214
+ attribute :address, :text
215
+ attribute :age, :int
216
+
217
+ primary_key [:name]
218
+ end
219
+ Cassandro.truncate_table('patients')
220
+ end
221
+
222
+ test "should create with TTL defined by the model" do
223
+ volatile_record = Test.create(:test_col_1 => SecureRandom.uuid, :test_col_2 => "Test Text")
224
+ results = Cassandro.execute "SELECT TTL(test_col_2) FROM tests WHERE test_col_1 = #{volatile_record.test_col_1}"
225
+
226
+ assert results.first["ttl(test_col_2)"] > 0
227
+ assert results.first["ttl(test_col_2)"] <= 20
228
+ end
229
+
230
+ test "should override TTL defined by the model" do
231
+ volatile_record = Test.create_with_ttl(10, :test_col_1 => SecureRandom.uuid, :test_col_2 => "Test Text")
232
+ results = Cassandro.execute "SELECT TTL(test_col_2) FROM tests WHERE test_col_1 = #{volatile_record.test_col_1}"
233
+
234
+ assert results.first["ttl(test_col_2)"] > 0
235
+ assert results.first["ttl(test_col_2)"] <= 10
236
+ end
237
+
238
+ test "should be able to create with TTL if not specified in the model" do
239
+ dog = Patient.create(:name => "Cassandro", :address => "cassandstreet", :age => 1)
240
+ results = Cassandro.execute "SELECT TTL(address) FROM patients WHERE name = 'Cassandro'"
241
+
242
+ assert !results.first["ttl(address)"]
243
+
244
+ cat = Patient.create_with_ttl(30, :name => "Cassandra", :address => "cassandstreet 123", :age => 2)
245
+ results = Cassandro.execute "SELECT TTL(address) FROM patients WHERE name = 'Cassandra'"
246
+
247
+ assert results.first["ttl(address)"] > 0
248
+ assert results.first["ttl(address)"] <= 30
249
+ end
250
+ end
187
251
  end
@@ -3,6 +3,7 @@ table = <<-TABLEDEF
3
3
  CREATE TABLE IF NOT EXISTS tests (
4
4
  test_col_1 UUID,
5
5
  test_col_2 VARCHAR,
6
+ test_col_3 VARCHAR,
6
7
  PRIMARY KEY(test_col_1)
7
8
  )
8
9
  TABLEDEF
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cassandro
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lautaro Orazi
@@ -9,66 +9,66 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-03-17 00:00:00.000000000 Z
12
+ date: 2015-03-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: cassandra-driver
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
- - - ~>
18
+ - - "~>"
19
19
  - !ruby/object:Gem::Version
20
20
  version: '1.2'
21
- - - ! '>='
21
+ - - ">="
22
22
  - !ruby/object:Gem::Version
23
23
  version: 1.0.0
24
24
  type: :runtime
25
25
  prerelease: false
26
26
  version_requirements: !ruby/object:Gem::Requirement
27
27
  requirements:
28
- - - ~>
28
+ - - "~>"
29
29
  - !ruby/object:Gem::Version
30
30
  version: '1.2'
31
- - - ! '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: 1.0.0
34
34
  - !ruby/object:Gem::Dependency
35
35
  name: protest
36
36
  requirement: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ~>
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0.5'
41
- - - ! '>='
41
+ - - ">="
42
42
  - !ruby/object:Gem::Version
43
43
  version: 0.5.3
44
44
  type: :development
45
45
  prerelease: false
46
46
  version_requirements: !ruby/object:Gem::Requirement
47
47
  requirements:
48
- - - ~>
48
+ - - "~>"
49
49
  - !ruby/object:Gem::Version
50
50
  version: '0.5'
51
- - - ! '>='
51
+ - - ">="
52
52
  - !ruby/object:Gem::Version
53
53
  version: 0.5.3
54
54
  - !ruby/object:Gem::Dependency
55
55
  name: rack-test
56
56
  requirement: !ruby/object:Gem::Requirement
57
57
  requirements:
58
- - - ~>
58
+ - - "~>"
59
59
  - !ruby/object:Gem::Version
60
60
  version: '0.6'
61
- - - ! '>='
61
+ - - ">="
62
62
  - !ruby/object:Gem::Version
63
63
  version: 0.6.3
64
64
  type: :development
65
65
  prerelease: false
66
66
  version_requirements: !ruby/object:Gem::Requirement
67
67
  requirements:
68
- - - ~>
68
+ - - "~>"
69
69
  - !ruby/object:Gem::Version
70
70
  version: '0.6'
71
- - - ! '>='
71
+ - - ">="
72
72
  - !ruby/object:Gem::Version
73
73
  version: 0.6.3
74
74
  description: Lightweight Apache Cassandra ORM for Ruby
@@ -79,9 +79,9 @@ executables: []
79
79
  extensions: []
80
80
  extra_rdoc_files: []
81
81
  files:
82
- - .gems
83
- - .gems-test
84
- - .gitignore
82
+ - ".gems"
83
+ - ".gems-test"
84
+ - ".gitignore"
85
85
  - LICENSE
86
86
  - README.md
87
87
  - cassandro.gemspec
@@ -114,19 +114,18 @@ require_paths:
114
114
  - lib
115
115
  required_ruby_version: !ruby/object:Gem::Requirement
116
116
  requirements:
117
- - - ! '>='
117
+ - - ">="
118
118
  - !ruby/object:Gem::Version
119
119
  version: '0'
120
120
  required_rubygems_version: !ruby/object:Gem::Requirement
121
121
  requirements:
122
- - - ! '>='
122
+ - - ">="
123
123
  - !ruby/object:Gem::Version
124
124
  version: '0'
125
125
  requirements: []
126
126
  rubyforge_project:
127
- rubygems_version: 2.4.2
127
+ rubygems_version: 2.2.2
128
128
  signing_key:
129
129
  specification_version: 4
130
130
  summary: Ruby ORM for Apache Cassandra
131
131
  test_files: []
132
- has_rdoc: