kweerie 0.1.1 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rake_tasks~ +20 -0
- data/README.md +21 -1
- data/lib/kweerie/base_objects.rb +56 -13
- data/lib/kweerie/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b17930ae6088ef2d4fb7f4e20c36479bc8bcc97ae2ed3bd2bfd8d7baed4266b0
|
4
|
+
data.tar.gz: 7100335df50ba93ee28fb811d530cdf2231db2efaed2f149073144a15e389c41
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 52f8165082fb447dcc9219c968256f867ead4dba70722920928acedae4e150d9608ee3ffd5dd803131d556031f588b3bba7f8f17171d56cca4487ae427dfb6b4
|
7
|
+
data.tar.gz: bdfdf6ecf8cf3495ef4ed157ea3e149d23ff16f5a7ff98a73329dd3f3a1516d80a8a57ddbea1b23dd26df43805a27164a28ba4836550ed9951f7392f07118eb9
|
data/.rake_tasks~
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
build
|
2
|
+
build:checksum
|
3
|
+
clean
|
4
|
+
clobber
|
5
|
+
default
|
6
|
+
install
|
7
|
+
install:local
|
8
|
+
release[remote]
|
9
|
+
release:guard_clean
|
10
|
+
release:rubygem_push
|
11
|
+
release:source_control_push[remote]
|
12
|
+
rubocop
|
13
|
+
rubocop:auto_correct
|
14
|
+
rubocop:autocorrect
|
15
|
+
rubocop:autocorrect_all
|
16
|
+
test
|
17
|
+
test:cmd
|
18
|
+
test:deps
|
19
|
+
test:isolated
|
20
|
+
test:slow
|
data/README.md
CHANGED
@@ -147,7 +147,27 @@ user.changes # => Hash of changes with [old, new] values
|
|
147
147
|
user.original_attributes # => Original attributes from DB
|
148
148
|
```
|
149
149
|
|
150
|
-
|
150
|
+
## PostgreSQL Array Support
|
151
|
+
|
152
|
+
BaseObjects handles PostgreSQL arrays by converting them to Ruby arrays with proper type casting:
|
153
|
+
|
154
|
+
```ruby
|
155
|
+
# In your PostgreSQL schema
|
156
|
+
create_table :users do |t|
|
157
|
+
t.integer :preferred_ordering, array: true, default: []
|
158
|
+
t.string :tags, array: true
|
159
|
+
t.float :scores, array: true
|
160
|
+
end
|
161
|
+
|
162
|
+
# In your query
|
163
|
+
user = UserSearch.with(name: 'Claude').first
|
164
|
+
|
165
|
+
user.preferred_ordering # => [1, 3, 2]
|
166
|
+
user.tags # => ["ruby", "rails"]
|
167
|
+
user.scores # => [98.5, 87.2, 92.0]
|
168
|
+
```
|
169
|
+
|
170
|
+
## Performance Considerations
|
151
171
|
|
152
172
|
BaseObjects creates a unique class for each query result set, with the following optimizations:
|
153
173
|
|
data/lib/kweerie/base_objects.rb
CHANGED
@@ -20,7 +20,7 @@ module Kweerie
|
|
20
20
|
private
|
21
21
|
|
22
22
|
def generate_result_class(attribute_names)
|
23
|
-
@generate_result_class ||= Class.new do
|
23
|
+
@generate_result_class ||= Class.new(self) do
|
24
24
|
# Include comparison and serialization modules
|
25
25
|
include Comparable
|
26
26
|
|
@@ -29,6 +29,20 @@ module Kweerie
|
|
29
29
|
attr_reader name
|
30
30
|
end
|
31
31
|
|
32
|
+
define_method :initialize do |attrs|
|
33
|
+
# Store both raw and casted versions
|
34
|
+
@_raw_original_attributes = attrs.dup
|
35
|
+
@_original_attributes = attrs.transform_keys(&:to_s).transform_values do |value|
|
36
|
+
type_cast_value(value)
|
37
|
+
end
|
38
|
+
|
39
|
+
attrs.each do |name, value|
|
40
|
+
casted_value = type_cast_value(value)
|
41
|
+
instance_variable_set("@#{name}", casted_value)
|
42
|
+
end
|
43
|
+
super() if defined?(super)
|
44
|
+
end
|
45
|
+
|
32
46
|
define_method :type_cast_value do |value|
|
33
47
|
case value
|
34
48
|
when /^\d{4}-\d{2}-\d{2}( \d{2}:\d{2}:\d{2})?$/ # DateTime check
|
@@ -39,10 +53,22 @@ module Kweerie
|
|
39
53
|
value.to_f
|
40
54
|
when /^(true|false)$/i # Boolean check
|
41
55
|
value.downcase == "true"
|
42
|
-
when /^
|
56
|
+
when /^{.*}$/ # Could be PG array or JSON
|
57
|
+
if value.start_with?("{") && value.end_with?("}") && !value.include?('"=>') && !value.include?(": ")
|
58
|
+
# PostgreSQL array (simple heuristic: no "=>" or ":" suggests it's not JSON)
|
59
|
+
parse_pg_array(value)
|
60
|
+
else
|
61
|
+
# Attempt JSON parse
|
62
|
+
begin
|
63
|
+
parsed = JSON.parse(value)
|
64
|
+
deep_stringify_keys(parsed)
|
65
|
+
rescue JSON::ParserError
|
66
|
+
value
|
67
|
+
end
|
68
|
+
end
|
69
|
+
when /^[\[{]/ # Pure JSON (arrays starting with [ or other JSON objects)
|
43
70
|
begin
|
44
71
|
parsed = JSON.parse(value)
|
45
|
-
# Use string keys for consistency in output
|
46
72
|
deep_stringify_keys(parsed)
|
47
73
|
rescue JSON::ParserError
|
48
74
|
value
|
@@ -52,15 +78,27 @@ module Kweerie
|
|
52
78
|
end
|
53
79
|
end
|
54
80
|
|
55
|
-
define_method :
|
56
|
-
#
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
81
|
+
define_method :parse_pg_array do |value|
|
82
|
+
# Remove the curly braces
|
83
|
+
clean_value = value.gsub(/^{|}$/, "")
|
84
|
+
return [] if clean_value.empty?
|
85
|
+
|
86
|
+
# Split on comma, but not within quoted strings
|
87
|
+
elements = clean_value.split(/,(?=(?:[^"]*"[^"]*")*[^"]*$)/)
|
88
|
+
|
89
|
+
elements.map do |element|
|
90
|
+
case element
|
91
|
+
when /^\d+$/ # Integer
|
92
|
+
element.to_i
|
93
|
+
when /^\d*\.\d+$/ # Float
|
94
|
+
element.to_f
|
95
|
+
when /^(true|false)$/i # Boolean
|
96
|
+
element.downcase == "true"
|
97
|
+
when /^"(.*)"$/ # Quoted string
|
98
|
+
::Regexp.last_match(1)
|
99
|
+
else
|
100
|
+
element
|
101
|
+
end
|
64
102
|
end
|
65
103
|
end
|
66
104
|
|
@@ -80,7 +118,7 @@ module Kweerie
|
|
80
118
|
attrs = attribute_names.map do |name|
|
81
119
|
"#{name}=#{instance_variable_get("@#{name}").inspect}"
|
82
120
|
end.join(" ")
|
83
|
-
"#<#{self.class.name
|
121
|
+
"#<#{self.class.superclass.name} #{attrs}>"
|
84
122
|
end
|
85
123
|
|
86
124
|
# Hash-like access
|
@@ -149,6 +187,11 @@ module Kweerie
|
|
149
187
|
@_original_attributes
|
150
188
|
end
|
151
189
|
|
190
|
+
# Raw attributes access
|
191
|
+
define_method :raw_original_attributes do
|
192
|
+
@_raw_original_attributes
|
193
|
+
end
|
194
|
+
|
152
195
|
# ActiveModel-like changes tracking
|
153
196
|
define_method :changed? do
|
154
197
|
to_h != @_original_attributes
|
data/lib/kweerie/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kweerie
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Toby
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-11-
|
11
|
+
date: 2024-11-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: minitest
|
@@ -59,6 +59,7 @@ executables: []
|
|
59
59
|
extensions: []
|
60
60
|
extra_rdoc_files: []
|
61
61
|
files:
|
62
|
+
- ".rake_tasks~"
|
62
63
|
- ".rubocop.yml"
|
63
64
|
- CHANGELOG.md
|
64
65
|
- LICENSE.txt
|