groovy 0.6.4 → 0.6.7

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: a622cdd3453e0fee1feeebf798cbcec9f0ef9f68759aec64f65c3d4a500f52af
4
- data.tar.gz: be1220642bf299a61ad5900bea1fffa3359725575def843ce233290e26a5b8e2
3
+ metadata.gz: d24298885415bc4acd243ec853d839187216d3f3bf8be758e309168fb3a4b4a6
4
+ data.tar.gz: c8a743dab8efe577290c91688e5107114bd5c0c7a7153f79db4d6985ce4467dd
5
5
  SHA512:
6
- metadata.gz: 9943e2715a0099f8b1b2795ab66259cbb5b71d5a782e4669b503e61f30a8df56fa57332f88c3592e3b69c0c63638da5d1ea471b102e62511028178886cd5b968
7
- data.tar.gz: fa02339637f33c69bb37301f63d8b14b77d61b185f0b3c23d78712fb26159d2d5594dc2bb9bba7bb225211c4ab6a5adef167ddade4ea5b5999dae8ce2d5e5f07
6
+ metadata.gz: 156828c23af648e267cee9a761183dd49b7d16d783fb993cee0653a449340c23bd25c03162da6666f30cc3989978c196d671198ac8da041bace897aa1bb0419d
7
+ data.tar.gz: 100290a07dabdd5da4dacd586ff5571c08c433b4f182a91530eb31bc6d5c78b21960d056190d73054952e614e21e428018ea2f8bad9b2637633739929d1216e7
data/lib/groovy/model.rb CHANGED
@@ -62,6 +62,10 @@ module Groovy
62
62
  db_context[table_name]
63
63
  end
64
64
 
65
+ def underscore_name
66
+ name.gsub(/([A-Z])/) { |x| "_#{x.downcase}" }.sub(/^_/, '')
67
+ end
68
+
65
69
  def attribute_names
66
70
  @attribute_names ||= schema.attribute_columns
67
71
  end
@@ -349,10 +353,16 @@ module Groovy
349
353
  @attributes
350
354
  end
351
355
 
356
+ alias_method :attrs, :attributes
357
+
352
358
  def inspect
353
359
  "#<#{self.class.name} id:#{id.inspect} attributes:[#{self.class.attribute_names.join(', ')}]>"
354
360
  end
355
361
 
362
+ def dump
363
+ pp attributes; nil
364
+ end
365
+
356
366
  def new_record?
357
367
  id.nil?
358
368
  # _key.nil?
@@ -470,7 +480,6 @@ module Groovy
470
480
 
471
481
  private
472
482
 
473
-
474
483
  def get_record_attribute(key)
475
484
  return if record.nil?
476
485
  val = record[key]
@@ -507,7 +516,6 @@ module Groovy
507
516
  end
508
517
  end
509
518
 
510
-
511
519
  def set_ref(name, obj, from_foreign = false)
512
520
  # puts "Setting ref #{name} -> #{obj} (#{obj.class})"
513
521
 
@@ -565,7 +573,7 @@ module Groovy
565
573
  # puts "Updating #{key} from #{values[0]} to #{values[1]}"
566
574
  record[key] = values.last
567
575
  end
568
- self.class.set_timestamp(record, :updated_at)
576
+ self.class.set_timestamp(record, :updated_at) unless changes[:updated_at]
569
577
  changes = {}
570
578
  update_foreign_refs
571
579
  fire_callbacks(:after_update)
data/lib/groovy/schema.rb CHANGED
@@ -127,6 +127,14 @@ module Groovy
127
127
  @spec[name.to_sym] = { type: :reference, args: [table_name, options] }
128
128
  end
129
129
 
130
+ def one(name, table_name = nil, options = {})
131
+ reference(name, table_name, options)
132
+ end
133
+
134
+ def many(name, table_name = nil, options = {})
135
+ reference(name, table_name, options.merge(type: :vector))
136
+ end
137
+
130
138
  def timestamps(opts = {})
131
139
  column(:created_at, 'time')
132
140
  column(:updated_at, 'time')
data/lib/groovy/vector.rb CHANGED
@@ -26,20 +26,6 @@ module Groovy
26
26
 
27
27
  alias_method :count, :size
28
28
 
29
- # we redefine first and last to avoid having to perform a full query
30
-
31
- def first
32
- if obj = records.first
33
- Model.initialize_from_record(obj)
34
- end
35
- end
36
-
37
- def last
38
- if obj = records.last
39
- Model.initialize_from_record(obj)
40
- end
41
- end
42
-
43
29
  def each(&block)
44
30
  items.each { |r| block.call(r) }
45
31
  end
@@ -95,12 +81,30 @@ module Groovy
95
81
 
96
82
  alias_method :<<, :push
97
83
 
98
- [:first, :last, :find_by, :search, :where, :not, :sort_by, :limit, :offset, :paginate, :in_batches].each do |scope_method|
84
+ [:new, :create, :create!].each do |relation_method|
85
+ define_method relation_method do |attrs|
86
+ vector_model.public_send(relation_method, attrs.merge(object_name => obj))
87
+ end
88
+ end
89
+
90
+ [:find_by, :search, :where, :not, :sort_by, :limit, :offset, :paginate, :in_batches].each do |scope_method|
99
91
  define_method scope_method do |*args, &block|
100
92
  query.public_send(scope_method, *args, &block)
101
93
  end
102
94
  end
103
95
 
96
+ def first
97
+ if obj = records.first
98
+ Model.initialize_from_record(obj)
99
+ end
100
+ end
101
+
102
+ def last
103
+ if obj = records.last
104
+ Model.initialize_from_record(obj)
105
+ end
106
+ end
107
+
104
108
  def ==(arr)
105
109
  to_a == arr
106
110
  end
@@ -108,21 +112,26 @@ module Groovy
108
112
  private
109
113
  attr_reader :obj, :key
110
114
 
111
- def query
112
- model = Model.model_from_table(key.capitalize)
113
- obj_name = obj.class.name.downcase
114
- model.query.where(obj_name => obj.id)
115
- end
116
-
117
115
  def method_missing(name, *args, &block)
118
- model = Model.model_from_table(key.capitalize)
119
- if model.scopes.include?(name)
116
+ if vector_model.scopes.include?(name)
120
117
  query.send(name)
121
118
  else
122
119
  super
123
120
  end
124
121
  end
125
122
 
123
+ def query
124
+ vector_model.query.where({ object_name => obj.id })
125
+ end
126
+
127
+ def object_name
128
+ obj.class.underscore_name # eg "product_brand"
129
+ end
130
+
131
+ def vector_model
132
+ @vector_model ||= Model.model_from_table(key.capitalize)
133
+ end
134
+
126
135
  def remove_record(rec)
127
136
  recs = obj.record[key].delete_if { |r| r == rec }
128
137
  obj.record[key] = recs
@@ -1,3 +1,3 @@
1
1
  module Groovy
2
- VERSION = '0.6.4'.freeze
2
+ VERSION = '0.6.7'.freeze
3
3
  end
data/spec/model_spec.rb CHANGED
@@ -123,6 +123,21 @@ describe Groovy::Model do
123
123
  end
124
124
 
125
125
  describe '#save' do
126
+
127
+ it 'sets created_at/updated_at timestamps when necesssary' do
128
+ time = Time.parse('2012-12-12')
129
+ allow(Time).to receive(:now).and_return(time)
130
+ prod = TestProduct.create!(name: 'A product', price: 100)
131
+ expect(prod.created_at).to eq(time)
132
+ expect(prod.updated_at).to eq(time)
133
+
134
+ another_time = Time.parse('2024-12-12')
135
+ prod.updated_at = another_time
136
+ prod.save
137
+
138
+ expect(prod.reload.created_at).to eq(time)
139
+ expect(prod.updated_at).to eq(another_time)
140
+ end
126
141
  end
127
142
 
128
143
  describe '#delete' do
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.6.4
4
+ version: 0.6.7
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: 2023-11-30 00:00:00.000000000 Z
11
+ date: 2023-12-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rroonga