cequel 1.2.6 → 1.3.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: a64f78d32fbb2e223d8537d5f0cf8c7ff61ed81c
4
- data.tar.gz: 50b4f001571a9aa00025e5e0b46a845dc8b70ae1
3
+ metadata.gz: bcbe817545ecdf0979da9b91e5e790965efdf8c4
4
+ data.tar.gz: acaede9ff87f4f1c379ab0f47dcba0f110905b45
5
5
  SHA512:
6
- metadata.gz: 297d35f852f20151515ebba24607a7c13873f1a4930e365f75b1a9adf4352b7b14bc8a57b7e4f2348d64b2107631fff4b2f20bf5a8e385286df5143e3fff3faa
7
- data.tar.gz: a12d232abd033573eaa674dd280dfee4d16a6775604ce510d23c7336190aa9572cf3cc4f23213e8bd5e41bd11a045ac7d5d44bbf579832ca78b6b0d6dddc1ad2
6
+ metadata.gz: 17b5623a4df15734d13f9c8d6bcef023d3b5a814582827260448a989c25832d4f95f8eda338a7bb322a5aeecc5e9ec04b6714b510719ec43d0e4531eaa84cca9
7
+ data.tar.gz: f75aac91c4ea18db149196aeaf3024a014bf5decc6a258b232c8c4c98fe2f41334d75d04b707d0ac459f990d104c606f68cd8717d2b3ab1954cd92cd098ca7d4
@@ -1,3 +1,9 @@
1
+ ## 1.3.0
2
+
3
+ * Add timestamps functionality (`created_at` and `updated_at`)
4
+ * More robust error handling when loading models for migrations
5
+ * Expose `#column_names` on Record and Schema
6
+
1
7
  ## 1.2.6
2
8
 
3
9
  * Fixes for Type::quote
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cequel (1.2.6)
4
+ cequel (1.3.0)
5
5
  activemodel (>= 3.1, < 5.0)
6
6
  cql-rb (~> 1.2)
7
7
 
@@ -27,7 +27,7 @@ GEM
27
27
  debugger-linecache (~> 1.2)
28
28
  coderay (1.1.0)
29
29
  columnize (0.8.9)
30
- cql-rb (1.2.1)
30
+ cql-rb (1.2.2)
31
31
  debugger (1.6.6)
32
32
  columnize (>= 0.3.1)
33
33
  debugger-linecache (~> 1.2.0)
@@ -279,9 +279,10 @@ GEM
279
279
  slop (3.5.0)
280
280
  spoon (0.0.4)
281
281
  ffi
282
- thread_safe (0.3.3)
283
- thread_safe (0.3.3-java)
284
- tzinfo (1.1.0)
282
+ thread_safe (0.3.4)
283
+ thread_safe (0.3.4-java)
284
+ timecop (0.7.1)
285
+ tzinfo (1.2.0)
285
286
  thread_safe (~> 0.1)
286
287
  yard (0.8.7.4)
287
288
 
@@ -301,4 +302,5 @@ DEPENDENCIES
301
302
  rspec (~> 2.0)
302
303
  rubocop (~> 0.19.0)
303
304
  rubysl (~> 2.0)
305
+ timecop (~> 0.7)
304
306
  yard (~> 0.6)
data/README.md CHANGED
@@ -115,6 +115,27 @@ class PostsController < ActionController::Base
115
115
  end
116
116
  ```
117
117
 
118
+ ### Timestamps ###
119
+
120
+ If your final primary key column is a `timeuuid` with the `:auto` option set,
121
+ the `created_at` method will return the time that the UUID key was generated.
122
+
123
+ To add timestamp columns, simply use the `timestamps` class macro:
124
+
125
+ ```ruby
126
+ class Blog
127
+ key :subdomain, :text
128
+ column :name, :text
129
+ timestamps
130
+ end
131
+ ```
132
+
133
+ This will automatically define `created_at` and `updated_at` columns, and
134
+ populate them appropriately on save.
135
+
136
+ If the creation time can be extracted from the primary key as outlined above,
137
+ this method will be preferred and no `created_at` column will be defined.
138
+
118
139
  ### Schema synchronization ###
119
140
 
120
141
  Cequel will automatically synchronize the schema stored in Cassandra to match
@@ -541,6 +562,7 @@ Cequel was written by:
541
562
  * Ilya Bazylchuk
542
563
  * Dan Cardamore
543
564
  * Kei Kusakari
565
+ * Oleh Novosad
544
566
 
545
567
  Special thanks to [Brewster](https://www.brewster.com), which supported the 0.x
546
568
  releases of Cequel.
@@ -23,6 +23,7 @@ require 'cequel/record/callbacks'
23
23
  require 'cequel/record/validations'
24
24
  require 'cequel/record/dirty'
25
25
  require 'cequel/record/conversion'
26
+ require 'cequel/record/timestamps'
26
27
 
27
28
  require 'cequel/record'
28
29
 
@@ -93,6 +94,7 @@ module Cequel
93
94
  include Conversion
94
95
  include ActiveModel::Serializers::JSON
95
96
  include ActiveModel::Serializers::Xml
97
+ include Timestamps
96
98
  end
97
99
 
98
100
  #
@@ -39,6 +39,9 @@ module Cequel
39
39
  # @!attribute [r] columns
40
40
  # (see Cequel::Schema::Table#columns)
41
41
  #
42
+ # @!attribute [r] column_names
43
+ # (see Cequel::Schema::Table#column_names)
44
+ #
42
45
  # @!attribute [r] key_columns
43
46
  # (see Cequel::Schema::Table#key_columns)
44
47
  #
@@ -57,7 +60,7 @@ module Cequel
57
60
  # @!method compact_storage?
58
61
  # (see Cequel::Schema::Table#compact_storage?)
59
62
  #
60
- def_delegators :table_schema, :columns, :key_columns,
63
+ def_delegators :table_schema, :columns, :column_names, :key_columns,
61
64
  :key_column_names, :partition_key_columns,
62
65
  :partition_key_column_names, :clustering_columns,
63
66
  :compact_storage?
@@ -38,7 +38,7 @@ namespace :cequel do
38
38
  new_constants.each do |class_name|
39
39
  begin
40
40
  clazz = class_name.constantize
41
- rescue NameError # rubocop:disable HandleExceptions
41
+ rescue NameError, RuntimeError # rubocop:disable HandleExceptions
42
42
  else
43
43
  if clazz.ancestors.include?(Cequel::Record) &&
44
44
  !migration_table_names.include?(clazz.table_name.to_sym)
@@ -0,0 +1,72 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Cequel
3
+ module Record
4
+ #
5
+ # This module provides `created_at` and `updated_at` functionality for
6
+ # records. It does this in two ways:
7
+ #
8
+ # * If a record's primary key is a `timeuuid` with the `:auto` option set,
9
+ # the `created_at` method will return the time extracted from the primary
10
+ # key.
11
+ # * Calling the `timestamps` macro in the class definition will define the
12
+ # `updated_at` and (if necessary) `created_at` columns, and set up
13
+ # lifecycle hooks to populate them appropriately.
14
+ #
15
+ # @example Record class with timestamps
16
+ # class Blog
17
+ # key :subdomain, :text
18
+ # column :name, :text
19
+ #
20
+ # timestamps
21
+ # end
22
+ #
23
+ # @since 1.3.0
24
+ #
25
+ module Timestamps
26
+ extend ActiveSupport::Concern
27
+
28
+ #
29
+ # Provides class methods for the Timestamps module
30
+ #
31
+ module ClassMethods
32
+ protected
33
+
34
+ def key(name, type, options = {})
35
+ super
36
+ if type == :timeuuid && options[:auto]
37
+ module_eval(<<-RUBY, __FILE__, __LINE__+1)
38
+ def created_at
39
+ read_attribute(#{name.inspect}).try(:to_time)
40
+ end
41
+ RUBY
42
+ end
43
+ end
44
+
45
+ def timestamps
46
+ column :updated_at, :timestamp
47
+
48
+ if method_defined?(:created_at)
49
+ before_save :set_updated_at
50
+ else
51
+ column :created_at, :timestamp
52
+
53
+ before_create :set_created_and_updated_at
54
+ before_update :set_updated_at
55
+ end
56
+ end
57
+ end
58
+
59
+ private
60
+
61
+ def set_created_and_updated_at
62
+ now = Time.now
63
+ self.created_at = now
64
+ self.updated_at = now
65
+ end
66
+
67
+ def set_updated_at
68
+ self.updated_at = Time.now
69
+ end
70
+ end
71
+ end
72
+ end
@@ -184,6 +184,12 @@ module Cequel
184
184
  columns_by_name[name.to_sym]
185
185
  end
186
186
 
187
+ #
188
+ # @return [Array<Symbol>] the names of all columns
189
+ def column_names
190
+ columns.map { |column| column.name }
191
+ end
192
+
187
193
  #
188
194
  # @return [Array<Column>] all key columns (partition + clustering)
189
195
  #
@@ -1,5 +1,5 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
  module Cequel
3
3
  # The current version of the library
4
- VERSION = '1.2.6'
4
+ VERSION = '1.3.0'
5
5
  end
@@ -0,0 +1,60 @@
1
+ require File.expand_path('../spec_helper', __FILE__)
2
+
3
+ describe Cequel::Record::Timestamps do
4
+ model :Blog do
5
+ key :subdomain, :text
6
+ column :name, :text
7
+ timestamps
8
+ end
9
+
10
+ model :Post do
11
+ key :blog_subdomain, :text
12
+ key :id, :timeuuid, auto: true
13
+ column :name, :text
14
+ timestamps
15
+ end
16
+
17
+ let!(:now) { Timecop.freeze }
18
+
19
+ context 'with simple primary key' do
20
+ let(:blog) { Blog.create!(subdomain: 'bigdata') }
21
+
22
+ it 'should populate created_at after create new record' do
23
+ expect(blog.created_at).to eq(now)
24
+ end
25
+
26
+ it 'should populate updated_at after create new record' do
27
+ expect(blog.updated_at).to eq(now)
28
+ end
29
+
30
+ it 'should update updated_at after record update but not created_at' do
31
+ future = Timecop.freeze(now + 2.minutes)
32
+ blog.name = 'name'
33
+ blog.save!
34
+ expect(blog.updated_at).to eq(future)
35
+ end
36
+ end
37
+
38
+ context 'with auto-generated timeuuid primary key' do
39
+ let(:post) { Post['bigdata'].create! }
40
+
41
+ it 'should not have created_at column' do
42
+ expect(Post.column_names).not_to include(:created_at)
43
+ end
44
+
45
+ it 'should expose created_at' do
46
+ expect(post.created_at.to_i).to eq(now.to_i)
47
+ end
48
+
49
+ it 'should populate updated_at after create new record' do
50
+ expect(post.updated_at).to eq(now)
51
+ end
52
+
53
+ it 'should update updated_at after record update but not created_at' do
54
+ future = Timecop.freeze(now + 2.minutes)
55
+ post.name = 'name'
56
+ post.save!
57
+ expect(post.updated_at).to eq(future)
58
+ end
59
+ end
60
+ end
@@ -34,6 +34,8 @@ RSpec.configure do |config|
34
34
  config.after(:all) do
35
35
  cequel.schema.drop!
36
36
  end
37
+
38
+ config.after(:each) { Timecop.return }
37
39
  end
38
40
 
39
41
  if defined? byebug
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cequel
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.6
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mat Brown
@@ -15,6 +15,7 @@ authors:
15
15
  - Ilya Bazylchuk
16
16
  - Dan Cardamore
17
17
  - Kei Kusakari
18
+ - Oleh Novosad
18
19
  autorequire:
19
20
  bindir: bin
20
21
  cert_chain: []
@@ -68,6 +69,20 @@ dependencies:
68
69
  - - "~>"
69
70
  - !ruby/object:Gem::Version
70
71
  version: '0.5'
72
+ - !ruby/object:Gem::Dependency
73
+ name: rake
74
+ requirement: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - "~>"
77
+ - !ruby/object:Gem::Version
78
+ version: '10.1'
79
+ type: :development
80
+ prerelease: false
81
+ version_requirements: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - "~>"
84
+ - !ruby/object:Gem::Version
85
+ version: '10.1'
71
86
  - !ruby/object:Gem::Dependency
72
87
  name: rspec
73
88
  requirement: !ruby/object:Gem::Requirement
@@ -83,47 +98,47 @@ dependencies:
83
98
  - !ruby/object:Gem::Version
84
99
  version: '2.0'
85
100
  - !ruby/object:Gem::Dependency
86
- name: yard
101
+ name: rubocop
87
102
  requirement: !ruby/object:Gem::Requirement
88
103
  requirements:
89
104
  - - "~>"
90
105
  - !ruby/object:Gem::Version
91
- version: '0.6'
106
+ version: 0.19.0
92
107
  type: :development
93
108
  prerelease: false
94
109
  version_requirements: !ruby/object:Gem::Requirement
95
110
  requirements:
96
111
  - - "~>"
97
112
  - !ruby/object:Gem::Version
98
- version: '0.6'
113
+ version: 0.19.0
99
114
  - !ruby/object:Gem::Dependency
100
- name: rake
115
+ name: timecop
101
116
  requirement: !ruby/object:Gem::Requirement
102
117
  requirements:
103
118
  - - "~>"
104
119
  - !ruby/object:Gem::Version
105
- version: '10.1'
120
+ version: '0.7'
106
121
  type: :development
107
122
  prerelease: false
108
123
  version_requirements: !ruby/object:Gem::Requirement
109
124
  requirements:
110
125
  - - "~>"
111
126
  - !ruby/object:Gem::Version
112
- version: '10.1'
127
+ version: '0.7'
113
128
  - !ruby/object:Gem::Dependency
114
- name: rubocop
129
+ name: yard
115
130
  requirement: !ruby/object:Gem::Requirement
116
131
  requirements:
117
132
  - - "~>"
118
133
  - !ruby/object:Gem::Version
119
- version: 0.19.0
134
+ version: '0.6'
120
135
  type: :development
121
136
  prerelease: false
122
137
  version_requirements: !ruby/object:Gem::Requirement
123
138
  requirements:
124
139
  - - "~>"
125
140
  - !ruby/object:Gem::Version
126
- version: 0.19.0
141
+ version: '0.6'
127
142
  description: |
128
143
  Cequel is an ActiveRecord-like domain model layer for Cassandra that exposes
129
144
  the robust data modeling capabilities of CQL3, including parent-child
@@ -188,6 +203,7 @@ files:
188
203
  - lib/cequel/record/schema.rb
189
204
  - lib/cequel/record/scoped.rb
190
205
  - lib/cequel/record/tasks.rb
206
+ - lib/cequel/record/timestamps.rb
191
207
  - lib/cequel/record/validations.rb
192
208
  - lib/cequel/schema.rb
193
209
  - lib/cequel/schema/column.rb
@@ -224,6 +240,7 @@ files:
224
240
  - spec/examples/record/serialization_spec.rb
225
241
  - spec/examples/record/set_spec.rb
226
242
  - spec/examples/record/spec_helper.rb
243
+ - spec/examples/record/timestamps_spec.rb
227
244
  - spec/examples/record/validations_spec.rb
228
245
  - spec/examples/schema/table_reader_spec.rb
229
246
  - spec/examples/schema/table_synchronizer_spec.rb
@@ -281,6 +298,7 @@ test_files:
281
298
  - spec/examples/record/serialization_spec.rb
282
299
  - spec/examples/record/set_spec.rb
283
300
  - spec/examples/record/spec_helper.rb
301
+ - spec/examples/record/timestamps_spec.rb
284
302
  - spec/examples/record/validations_spec.rb
285
303
  - spec/examples/schema/table_reader_spec.rb
286
304
  - spec/examples/schema/table_synchronizer_spec.rb