kweerie 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -1
- data/README.md +2 -2
- data/lib/generators/kweerie/kweerie_generator.rb +6 -2
- data/lib/kweerie/base.rb +0 -9
- data/lib/kweerie/base_object.rb +42 -7
- data/lib/kweerie/configuration.rb +3 -1
- data/lib/kweerie/{result_class_components → object_methods}/accessors.rb +1 -1
- data/lib/kweerie/{result_class_components → object_methods}/comparison.rb +1 -1
- data/lib/kweerie/{result_class_components → object_methods}/key_transformation.rb +1 -1
- data/lib/kweerie/{result_class_components → object_methods}/serialization.rb +1 -1
- data/lib/kweerie/{result_class_components → object_methods}/type_casting.rb +1 -1
- data/lib/kweerie/sql_path_resolver.rb +5 -6
- data/lib/kweerie/version.rb +1 -1
- data/lib/kweerie.rb +0 -1
- metadata +7 -8
- data/lib/kweerie/result_class_generator.rb +0 -51
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8ee037d0d27c788d915bb3a17c5cdc9fb4dc00dd172ae8f32023df00fd70515d
|
4
|
+
data.tar.gz: '0950c053e7d14f261923f74c5987fa177fa6ffb1f9b2e8213e132f411722d877'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 75e8dee13094014cefe169804ee050424fe0a946b8055d7f92423a819d578c375817bc580ae1b57b658d513786efc3fa731d3dad3fde3b022ec95cafa2701264
|
7
|
+
data.tar.gz: 8c7066bd2b2d1b2812889c116518d87be3b4868e57443fd54bc67b6980772999c4db8b853e98453331d364caf289ec8005c5ebf3d619ab2330d2b8dab781f7a0
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -324,8 +324,8 @@ Kweerie.configure do |config|
|
|
324
324
|
# Use a custom connection provider
|
325
325
|
config.connection_provider = -> { MyCustomConnectionPool.connection }
|
326
326
|
|
327
|
-
# Configure where to look for SQL files
|
328
|
-
config.
|
327
|
+
# Configure where to look for SQL files. Generator also uses this path
|
328
|
+
config.default_path = 'app/sql'
|
329
329
|
end
|
330
330
|
```
|
331
331
|
|
@@ -25,10 +25,10 @@ class KweerieGenerator < Rails::Generators::NamedBase
|
|
25
25
|
FileUtils.mkdir_p("app/queries")
|
26
26
|
|
27
27
|
# Create the Ruby file
|
28
|
-
create_file "
|
28
|
+
create_file "#{default_path}/#{file_name}.rb", template_content
|
29
29
|
|
30
30
|
# Create the SQL file
|
31
|
-
create_file "
|
31
|
+
create_file "#{default_path}/#{file_name}.sql", <<~SQL
|
32
32
|
-- Write your SQL query here
|
33
33
|
-- Available parameters: #{parameters.map { |p| "$#{parameters.index(p) + 1} (#{p})" }.join(", ")}
|
34
34
|
|
@@ -50,4 +50,8 @@ class KweerieGenerator < Rails::Generators::NamedBase
|
|
50
50
|
def class_name
|
51
51
|
name.classify
|
52
52
|
end
|
53
|
+
|
54
|
+
def default_path
|
55
|
+
Kweerie.configuration.default_path
|
56
|
+
end
|
53
57
|
end
|
data/lib/kweerie/base.rb
CHANGED
@@ -172,15 +172,6 @@ module Kweerie
|
|
172
172
|
ordered_params = bindings.transform_values { |position| params[bindings.key(position)] }
|
173
173
|
ordered_params.values
|
174
174
|
end
|
175
|
-
|
176
|
-
def root_path
|
177
|
-
defined?(Rails) ? Rails.root : Dir.pwd
|
178
|
-
end
|
179
|
-
|
180
|
-
def using_activerecord?
|
181
|
-
defined?(ActiveRecord::Base) &&
|
182
|
-
Kweerie.configuration.connection_provider == Kweerie::Configuration.new.connection_provider
|
183
|
-
end
|
184
175
|
end
|
185
176
|
end
|
186
177
|
end
|
data/lib/kweerie/base_object.rb
CHANGED
@@ -1,7 +1,10 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
require_relative "object_methods/accessors"
|
4
|
+
require_relative "object_methods/comparison"
|
5
|
+
require_relative "object_methods/key_transformation"
|
6
|
+
require_relative "object_methods/serialization"
|
7
|
+
require_relative "object_methods/type_casting"
|
5
8
|
|
6
9
|
module Kweerie
|
7
10
|
class BaseObject < Base
|
@@ -11,8 +14,7 @@ module Kweerie
|
|
11
14
|
return [] if results.empty?
|
12
15
|
|
13
16
|
# Create a unique result class for this query
|
14
|
-
result_class =
|
15
|
-
|
17
|
+
result_class = generate(results.first.keys)
|
16
18
|
# Map results to objects
|
17
19
|
results.map { |row| result_class.new(row) }
|
18
20
|
end
|
@@ -25,10 +27,43 @@ module Kweerie
|
|
25
27
|
@cast_definitions ||= {}
|
26
28
|
end
|
27
29
|
|
28
|
-
|
30
|
+
def generate(attribute_names)
|
31
|
+
cast_definitions = self.cast_definitions
|
32
|
+
Class.new(self) do
|
33
|
+
include Comparable
|
34
|
+
include ObjectMethods::Accessors
|
35
|
+
include ObjectMethods::Comparison
|
36
|
+
include ObjectMethods::KeyTransformation
|
37
|
+
include ObjectMethods::Serialization
|
38
|
+
include ObjectMethods::TypeCasting
|
39
|
+
|
40
|
+
# Define attr_readers for all columns
|
41
|
+
attribute_names.each { |name| attr_reader name }
|
42
|
+
|
43
|
+
define_method :initialize do |attrs|
|
44
|
+
# Store both raw and casted versions
|
45
|
+
@_raw_original_attributes = attrs.dup
|
46
|
+
@_original_attributes = attrs.each_with_object({}) do |(key, value), hash|
|
47
|
+
type_definition = cast_definitions[key.to_sym]
|
48
|
+
casted_value = type_cast_value(value, type_definition)
|
49
|
+
hash[key.to_s] = casted_value
|
50
|
+
instance_variable_set("@#{key}", casted_value)
|
51
|
+
end
|
52
|
+
|
53
|
+
super() if defined?(super)
|
54
|
+
end
|
55
|
+
|
56
|
+
# Nice inspect output
|
57
|
+
define_method :inspect do
|
58
|
+
attrs = attribute_names.map do |name|
|
59
|
+
"#{name}=#{instance_variable_get("@#{name}").inspect}"
|
60
|
+
end.join(" ")
|
61
|
+
"#<#{self.class.superclass.name} #{attrs}>"
|
62
|
+
end
|
29
63
|
|
30
|
-
|
31
|
-
|
64
|
+
# Make attribute_names available to instance methods
|
65
|
+
define_method(:attribute_names) { attribute_names }
|
66
|
+
end
|
32
67
|
end
|
33
68
|
end
|
34
69
|
end
|
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
module Kweerie
|
4
4
|
class Configuration
|
5
|
-
attr_accessor :connection_provider, :sql_paths
|
5
|
+
attr_accessor :connection_provider, :sql_paths, :default_path
|
6
6
|
|
7
7
|
def initialize
|
8
8
|
# Default to using ActiveRecord's connection if available
|
@@ -20,6 +20,8 @@ module Kweerie
|
|
20
20
|
paths.unshift("lib/queries") unless defined?(Rails)
|
21
21
|
paths
|
22
22
|
}
|
23
|
+
|
24
|
+
@default_path = defined?(Rails) ? "app/queries" : "lib/queries"
|
23
25
|
end
|
24
26
|
end
|
25
27
|
end
|
@@ -51,14 +51,13 @@ class SQLPathResolver
|
|
51
51
|
end
|
52
52
|
|
53
53
|
def find_in_configured_paths(filename)
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
end
|
54
|
+
full_path = File.join(path, filename)
|
55
|
+
|
56
|
+
full_path if File.exist?(full_path)
|
58
57
|
end
|
59
58
|
|
60
|
-
def
|
61
|
-
Kweerie.configuration.
|
59
|
+
def path
|
60
|
+
Kweerie.configuration.default_path
|
62
61
|
end
|
63
62
|
|
64
63
|
def validate_file_exists!(path)
|
data/lib/kweerie/version.rb
CHANGED
data/lib/kweerie.rb
CHANGED
@@ -28,7 +28,6 @@ require_relative "kweerie/configuration"
|
|
28
28
|
require_relative "kweerie/base"
|
29
29
|
require_relative "kweerie/base_object"
|
30
30
|
require_relative "kweerie/sql_path_resolver"
|
31
|
-
require_relative "kweerie/result_class_generator"
|
32
31
|
require_relative "kweerie/types/boolean"
|
33
32
|
require_relative "kweerie/types/pg_array"
|
34
33
|
require_relative "kweerie/types/pg_jsonb"
|
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.
|
4
|
+
version: 0.3.0
|
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-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: minitest
|
@@ -70,12 +70,11 @@ files:
|
|
70
70
|
- lib/kweerie/base.rb
|
71
71
|
- lib/kweerie/base_object.rb
|
72
72
|
- lib/kweerie/configuration.rb
|
73
|
-
- lib/kweerie/
|
74
|
-
- lib/kweerie/
|
75
|
-
- lib/kweerie/
|
76
|
-
- lib/kweerie/
|
77
|
-
- lib/kweerie/
|
78
|
-
- lib/kweerie/result_class_generator.rb
|
73
|
+
- lib/kweerie/object_methods/accessors.rb
|
74
|
+
- lib/kweerie/object_methods/comparison.rb
|
75
|
+
- lib/kweerie/object_methods/key_transformation.rb
|
76
|
+
- lib/kweerie/object_methods/serialization.rb
|
77
|
+
- lib/kweerie/object_methods/type_casting.rb
|
79
78
|
- lib/kweerie/sql_path_resolver.rb
|
80
79
|
- lib/kweerie/types/boolean.rb
|
81
80
|
- lib/kweerie/types/pg_array.rb
|
@@ -1,51 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative "result_class_components/accessors"
|
4
|
-
require_relative "result_class_components/comparison"
|
5
|
-
require_relative "result_class_components/key_transformation"
|
6
|
-
require_relative "result_class_components/serialization"
|
7
|
-
require_relative "result_class_components/type_casting"
|
8
|
-
|
9
|
-
module Kweerie
|
10
|
-
class ResultClassGenerator
|
11
|
-
def self.generate(parent_class, attribute_names)
|
12
|
-
Class.new(parent_class) do
|
13
|
-
include Comparable
|
14
|
-
include ResultClassComponents::Accessors
|
15
|
-
include ResultClassComponents::Comparison
|
16
|
-
include ResultClassComponents::KeyTransformation
|
17
|
-
include ResultClassComponents::Serialization
|
18
|
-
include ResultClassComponents::TypeCasting
|
19
|
-
|
20
|
-
# Define attr_readers for all columns
|
21
|
-
attribute_names.each { |name| attr_reader name }
|
22
|
-
|
23
|
-
define_method :initialize do |attrs|
|
24
|
-
# Store both raw and casted versions
|
25
|
-
cast_definitions = parent_class.cast_definitions
|
26
|
-
|
27
|
-
@_raw_original_attributes = attrs.dup
|
28
|
-
@_original_attributes = attrs.each_with_object({}) do |(key, value), hash|
|
29
|
-
type_definition = cast_definitions[key.to_sym]
|
30
|
-
casted_value = type_cast_value(value, type_definition)
|
31
|
-
hash[key.to_s] = casted_value
|
32
|
-
instance_variable_set("@#{key}", casted_value)
|
33
|
-
end
|
34
|
-
|
35
|
-
super() if defined?(super)
|
36
|
-
end
|
37
|
-
|
38
|
-
# Nice inspect output
|
39
|
-
define_method :inspect do
|
40
|
-
attrs = attribute_names.map do |name|
|
41
|
-
"#{name}=#{instance_variable_get("@#{name}").inspect}"
|
42
|
-
end.join(" ")
|
43
|
-
"#<#{self.class.superclass.name} #{attrs}>"
|
44
|
-
end
|
45
|
-
|
46
|
-
# Make attribute_names available to instance methods
|
47
|
-
define_method(:attribute_names) { attribute_names }
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|