groovy 0.5.1 → 0.6.1

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
  SHA256:
3
- metadata.gz: af9ef6dd23a84a0887fa26436fb1a3f98112621f25b1088c7e5835706fedefc9
4
- data.tar.gz: 4fa395ca4c1f32c3d31df1707a2ee66e2c6fabf0550957962b7d5efa3fd7458c
3
+ metadata.gz: bf0e3aa6e2c0d1e3d9e0245062e1b778e4da15e9130cc672d07eb1e474e25d0f
4
+ data.tar.gz: add6fe670e018efb4f5706b02d05154bdf525d5f2bb3f8ce378e60bb92471f59
5
5
  SHA512:
6
- metadata.gz: 7c6936e830cb7449843a43882cf5895b452806ab61ca7f01c2e7c97ae272d0daeb456a2f4d9c16b5cad52c46b8f9902e851b4e6e9c43d27de1d995bdca44fdbf
7
- data.tar.gz: dbde160c35f34339e5f155bea793c0c4fd5671916326a34c9c69efe9e266baffb47ed12e0afe5edd7a1fae5ba46abc7c41cc8957cba511be5ad4f3972df09ed9
6
+ metadata.gz: '0228c03673a0d86e3072c3f7a952a152c3799125f3c3a2998a219d6015a0c6ccea54bd10972996f17dab52b47298fdc3424fe9bff5de3343a3803748d22bbef1'
7
+ data.tar.gz: 8302d8af1e7940a878643f123760e5ae482b786c6c6857cf69306f8b2bb0e79a7364f05035659611102966a7b199aae7ce1eab869cdc9f2ab8055551cb5cdc42
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- source :rubygems
1
+ source 'https://rubygems.org'
2
2
 
3
3
  # specified in groovy.gemspec
4
4
  gemspec
data/groovy.gemspec CHANGED
@@ -14,7 +14,7 @@ Gem::Specification.new do |s|
14
14
  # s.required_rubygems_version = ">= 1.3.6"
15
15
  # s.rubyforge_project = "groovy"
16
16
 
17
- s.add_runtime_dependency "rroonga", "= 9.0.3"
17
+ s.add_runtime_dependency "rroonga", "= 12.1.0"
18
18
 
19
19
  s.add_development_dependency "bundler", ">= 1.0.0"
20
20
  s.add_development_dependency "rspec", '~> 3.0', '>= 3.0.0'
data/lib/groovy/model.rb CHANGED
@@ -26,7 +26,7 @@ module Groovy
26
26
  # end
27
27
 
28
28
  def self.model_from_table(table_name)
29
- Kernel.const_get(table_name.to_s.sub(/ies$/, 'y').sub(/s$/, '').classify)
29
+ get_class(table_name.to_s.sub(/ies$/, 'y').sub(/s$/, ''))
30
30
  end
31
31
 
32
32
  def self.included(base)
@@ -35,6 +35,11 @@ module Groovy
35
35
  base.table_name = base.name.sub(/y$/, 'ie') + 's'
36
36
  end
37
37
 
38
+ def self.get_class(table_name)
39
+ classified = table_name.gsub(/([A-Z])/, '_\1').split('_').collect! { |w| w.capitalize }.join
40
+ Kernel.const_get(classified)
41
+ end
42
+
38
43
  module ClassMethods
39
44
  extend Forwardable
40
45
  attr_accessor :context_name, :table_name
@@ -86,6 +91,12 @@ module Groovy
86
91
  add_accessors_for(name)
87
92
  end
88
93
 
94
+ def add_reference(name, table_name, options = {})
95
+ schema.reference(name, table_name, options)
96
+ schema.sync
97
+ add_accessors_for(name)
98
+ end
99
+
89
100
  def add_accessors_for(col, s = schema)
90
101
  if s.attribute_columns.include?(col)
91
102
  add_attr_accessors(col)
@@ -202,9 +213,13 @@ module Groovy
202
213
  end
203
214
  end
204
215
 
205
- [:first, :last, :select, :find_by, :search, :where, :not, :sort_by, :limit, :offset, :paginate, :in_batches].each do |scope_method|
206
- define_method scope_method do |*args, &block|
207
- query.public_send(scope_method, *args, &block)
216
+ def where(*args, &block)
217
+ query.where(*args, &block)
218
+ end
219
+
220
+ [:first, :last, :select, :find_by, :search, :not, :sort_by, :in_batches, :limit, :offset, :paginate].each do |scope_method|
221
+ define_method scope_method do |**args, &block|
222
+ query.public_send(scope_method, **args, &block)
208
223
  end
209
224
  end
210
225
 
@@ -231,7 +246,7 @@ module Groovy
231
246
  end
232
247
 
233
248
  def set_timestamp(obj, key_name)
234
- obj[key_name] = Time.now if attribute_names.include?(key_name.to_sym)
249
+ obj[key_name] = Time.now.utc if attribute_names.include?(key_name.to_sym)
235
250
  end
236
251
 
237
252
  def callbacks
@@ -256,7 +271,7 @@ module Groovy
256
271
  end
257
272
 
258
273
  def db_context
259
- Groovy.contexts[context_name.to_sym] \
274
+ Groovy.contexts[context_name&.to_sym] \
260
275
  or raise "Context not defined: #{context_name} Please call Groovy.open('./db/path') first."
261
276
  end
262
277
 
@@ -374,7 +389,11 @@ module Groovy
374
389
 
375
390
  def save(options = {})
376
391
  return false if respond_to?(:invalid?) and invalid?
377
- new_record? ? create : update
392
+ if new_record?
393
+ create && true
394
+ else
395
+ update
396
+ end
378
397
  end
379
398
 
380
399
  def save!(options = {})
data/lib/groovy/query.rb CHANGED
@@ -75,7 +75,7 @@ module Groovy
75
75
  def where(conditions = nil)
76
76
  case conditions
77
77
  when String # "foo:bar"
78
- add_param(AND + "(#{map_operator(conditions)})")
78
+ add_param(AND + "(#{map_operator(handle_timestamps(conditions))})")
79
79
  when Hash # { foo: 'bar' } or { views: 1..100 }
80
80
  conditions.each do |key, val|
81
81
  if val.is_a?(Range)
@@ -91,6 +91,15 @@ module Groovy
91
91
  str = "#{key}:#{val.join(" OR #{key}:")}"
92
92
  add_param(AND + str)
93
93
 
94
+ elsif val.is_a?(Time)
95
+ # The second, specify the timestamp as string in following format:
96
+ # “(YEAR)/(MONTH)/(DAY) (HOUR):(MINUTE):(SECOND)”
97
+
98
+ # date_str = val.utc.strftime("%Y/%m/%d %H\:%M\:%S")
99
+ date_str = val.strftime("%Y/%m/%d %H\:%M\:%S")
100
+ str = "#{key}:#{date_str}"
101
+ add_param(AND + str)
102
+
94
103
  else
95
104
  str = val.nil? || val === false || val.to_s.strip == '' ? "\"\"" : escape_val(val)
96
105
  add_param(AND + [key, str].join(':'))
@@ -229,6 +238,7 @@ module Groovy
229
238
  end
230
239
 
231
240
  def in_batches(of: 1000, from: nil, &block)
241
+ puts "of: #{of}"
232
242
  sorting[:limit] = of
233
243
  sorting[:offset] = from || 0
234
244
 
@@ -305,6 +315,12 @@ module Groovy
305
315
  .sub(' >= ', ':>=')
306
316
  end
307
317
 
318
+ # remove timezones from timestamps, otherwise we get a 'failed to cast to <Time>' error
319
+ def handle_timestamps(str)
320
+ # 2023-11-21 10:38:17 -0300 -> 2023/11/21 10:38:17
321
+ str.sub(/(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+) (.+)/, '\1/\2/\3 \4:\5:\6')
322
+ end
323
+
308
324
  def sort_key_and_order
309
325
  sorting[:by] or [{ key: @default_sort_key, order: :asc }]
310
326
  end
data/lib/groovy/schema.rb CHANGED
@@ -108,9 +108,11 @@ module Groovy
108
108
 
109
109
  ensure_created!
110
110
  remove_columns_not_in(@spec.keys)
111
+
111
112
  @spec.each do |col, spec|
112
113
  check_and_add_column(col, spec[:type], spec[:args])
113
114
  end
115
+
114
116
  @index_columns.each do |col|
115
117
  add_index_on(col)
116
118
  end
@@ -187,12 +189,12 @@ module Groovy
187
189
  end
188
190
 
189
191
  def create_table!
190
- log "Creating table!"
192
+ log "Creating table #{table_name}!"
191
193
  Groonga::Schema.create_table(table_name, context: context)
192
194
  end
193
195
 
194
196
  def remove_table!
195
- log "Removing table!"
197
+ log "Removing table #{table_name}!"
196
198
  Groonga::Schema.remove_table(table_name, context: context)
197
199
  @table = nil
198
200
  end
@@ -202,7 +204,7 @@ module Groovy
202
204
  end
203
205
 
204
206
  def log(str)
205
- puts "[#{table_name}] #{str}" if ENV['DEBUG']
207
+ puts "[#{table_name}] #{str}" # if ENV['DEBUG']
206
208
  end
207
209
  end
208
210
  end
data/lib/groovy/vector.rb CHANGED
@@ -80,6 +80,10 @@ module Groovy
80
80
  end
81
81
  end
82
82
 
83
+ def ==(arr)
84
+ to_a == arr
85
+ end
86
+
83
87
  private
84
88
  attr_reader :obj, :key
85
89
 
@@ -1,3 +1,3 @@
1
1
  module Groovy
2
- VERSION = '0.5.1'.freeze
2
+ VERSION = '0.6.1'.freeze
3
3
  end
data/lib/groovy.rb CHANGED
@@ -41,7 +41,7 @@ module Groovy
41
41
  contexts[name.to_sym] = if name == :default
42
42
  Groonga::Context.default.tap { |ctx| open_or_create_db(ctx, db_path) }
43
43
  else
44
- init_context(db_path, opts)
44
+ init_context(db_path)
45
45
  end
46
46
  end
47
47
 
@@ -63,8 +63,8 @@ module Groovy
63
63
 
64
64
  private
65
65
 
66
- def init_context(db_path, opts)
67
- Groonga::Context.new(opts).tap do |ctx|
66
+ def init_context(db_path)
67
+ Groonga::Context.new.tap do |ctx|
68
68
  open_or_create_db(ctx, db_path)
69
69
  end
70
70
  end
data/spec/groovy_spec.rb CHANGED
@@ -5,6 +5,18 @@ describe Groovy do
5
5
  Groonga::Context.default = nil
6
6
  end
7
7
 
8
+ it 'explodes if opening a scheme without opening first' do
9
+ expect do
10
+ class Foobar; end
11
+ Foobar.class_eval do
12
+ include Groovy::Model
13
+ schema do |t|
14
+ t.string :foobar
15
+ end
16
+ end
17
+ end.to raise_error("Context not defined: Please call Groovy.open('./db/path') first.")
18
+ end
19
+
8
20
  describe '.open' do
9
21
  it 'explodes if called with no params' do
10
22
  expect do
data/spec/query_spec.rb CHANGED
@@ -95,10 +95,10 @@ describe Groovy::Query do
95
95
  res = TestProduct.where(created_at: @one_week_ago)
96
96
  expect(res.map(&:id)).to eq([@p3.id])
97
97
 
98
- res = TestProduct.where("created_at > #{@one_week_ago.to_s}")
98
+ res = TestProduct.where("created_at > #{@one_week_ago}")
99
99
  expect(res.map(&:id)).to eq([@p1.id, @p2.id, @p4.id, @p5.id])
100
100
 
101
- res = TestProduct.where("created_at < #{Time.now.to_s}")
101
+ res = TestProduct.where("created_at < #{Time.now}")
102
102
  expect(res.map(&:id)).to eq([@p3.id])
103
103
 
104
104
  eight_days = Time.at(@one_week_ago.to_i - 3600 * 24)
@@ -0,0 +1,56 @@
1
+ require_relative './spec_helper'
2
+
3
+ describe Groovy::Model do
4
+
5
+ before :all do
6
+ Groovy.open('tmp/vector', 'vector_spec')
7
+ load_schema! 'vector_spec'
8
+ end
9
+
10
+ after :all do
11
+ Groovy.close('vector_spec')
12
+ end
13
+
14
+ def load_vector_schema!
15
+ klass = Class.new
16
+ Object.const_set("Comment", klass)
17
+ Comment.class_eval do
18
+ include Groovy::Model
19
+ schema(context: 'vector_spec') do |t|
20
+ t.string :content
21
+ t.timestamps
22
+ end
23
+ end
24
+
25
+ klass = Class.new;
26
+ Object.const_set("User", klass)
27
+ User.class_eval do
28
+ include Groovy::Model
29
+ schema(context: 'vector_spec') do |t|
30
+ t.string :name
31
+ t.timestamps
32
+ end
33
+ end
34
+
35
+ Comment.add_reference :author, "Users"
36
+ User.add_reference :comments, "Comments", type: :vector
37
+ end
38
+
39
+ describe 'relations' do
40
+
41
+ before do
42
+ load_vector_schema!
43
+ end
44
+
45
+ it 'loads records and keeps relations' do
46
+ user = User.new(name: 'John')
47
+ expect(user.save).to eq(true)
48
+ comment = Comment.new(author: user, content: 'Hello there!')
49
+ expect(comment.save).to eq(true)
50
+ user.comments << comment
51
+ expect(user.reload.comments).to eq([comment])
52
+ end
53
+
54
+ end
55
+
56
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: groovy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomás Pollak
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-07-08 00:00:00.000000000 Z
11
+ date: 2023-11-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rroonga
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - '='
18
18
  - !ruby/object:Gem::Version
19
- version: 9.0.3
19
+ version: 12.1.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - '='
25
25
  - !ruby/object:Gem::Version
26
- version: 9.0.3
26
+ version: 12.1.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -102,6 +102,7 @@ files:
102
102
  - spec/query_spec.rb
103
103
  - spec/search_spec.rb
104
104
  - spec/spec_helper.rb
105
+ - spec/vector_spec.rb
105
106
  homepage: https://github.com/tomas/groovy
106
107
  licenses: []
107
108
  metadata: {}
@@ -120,8 +121,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
120
121
  - !ruby/object:Gem::Version
121
122
  version: '0'
122
123
  requirements: []
123
- rubyforge_project:
124
- rubygems_version: 2.7.3
124
+ rubygems_version: 3.4.10
125
125
  signing_key:
126
126
  specification_version: 4
127
127
  summary: A wrapper around Groonga/Rroonga