groovy 0.6.4 → 0.6.6

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: dc2a1c85a6d482813b6ec9de3616e2c369206a419008b55600194f930bc56416
4
+ data.tar.gz: 968d357fb616dcfa9761f1ba93843678263d69ff38af2ec342ee9430bbf7ccc6
5
5
  SHA512:
6
- metadata.gz: 9943e2715a0099f8b1b2795ab66259cbb5b71d5a782e4669b503e61f30a8df56fa57332f88c3592e3b69c0c63638da5d1ea471b102e62511028178886cd5b968
7
- data.tar.gz: fa02339637f33c69bb37301f63d8b14b77d61b185f0b3c23d78712fb26159d2d5594dc2bb9bba7bb225211c4ab6a5adef167ddade4ea5b5999dae8ce2d5e5f07
6
+ metadata.gz: 7df3be7e3c43b5d48be0b563bb156da21a8992c97be9392b91b939a1943920f0587ee01ff4521c983b8524beb27b0ecd0eede630aff7c16e42e80e004ec42a46
7
+ data.tar.gz: e9a7fd6abc2dda7d902cd07d091f1fafbfe053e229a730754f0170dc04aa3326fbbef4a9646f3d6788cf896b7b72e9c0c10f0c010a4d17bd1e99aa97222fcda9
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
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.6'.freeze
3
3
  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.6.4
4
+ version: 0.6.6
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-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rroonga