kweerie 0.2.0 → 0.3.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a1ca73e0c3e5890aa9805eed0b491c100be177b958fc9bab9592eaf4f2cdb0d0
4
- data.tar.gz: b79729e675821e02c2ff02e3dfcdb8e5237230364790cc9e59e1d4a953ea8da6
3
+ metadata.gz: 8ee037d0d27c788d915bb3a17c5cdc9fb4dc00dd172ae8f32023df00fd70515d
4
+ data.tar.gz: '0950c053e7d14f261923f74c5987fa177fa6ffb1f9b2e8213e132f411722d877'
5
5
  SHA512:
6
- metadata.gz: 511ea093375cca24abbb8514a98b26cfb662239b836c3da7140aac4ef06ba7a23bf40074e1d9a973ac56a1cd02d1dd5dac486409968bed8f40b39312b6a8ac6d
7
- data.tar.gz: abd8e02e09a00f3d4dd683c5e9e179386c053ed0c390d2f09a8c227866e39ff6303400766f2ff173eaf5d379e3bcad84840621a2fdde5b593c76e8643c72741a
6
+ metadata.gz: 75e8dee13094014cefe169804ee050424fe0a946b8055d7f92423a819d578c375817bc580ae1b57b658d513786efc3fa731d3dad3fde3b022ec95cafa2701264
7
+ data.tar.gz: 8c7066bd2b2d1b2812889c116518d87be3b4868e57443fd54bc67b6980772999c4db8b853e98453331d364caf289ec8005c5ebf3d619ab2330d2b8dab781f7a0
data/CHANGELOG.md CHANGED
@@ -1,4 +1,8 @@
1
- ## [Unreleased]
1
+ ## [0.3.0] - 2024-11-21
2
+
3
+ - changed sql_paths to default_path.
4
+ - fixed generate to handle defined path
5
+
2
6
  ## [0.2.0] - 2024-11-12
3
7
 
4
8
  - changed BaseObjects to BaseObject.
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.sql_paths = -> { ['db/queries', 'app/sql'] }
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 "app/queries/#{file_name}.rb", template_content
28
+ create_file "#{default_path}/#{file_name}.rb", template_content
29
29
 
30
30
  # Create the SQL file
31
- create_file "app/queries/#{file_name}.sql", <<~SQL
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
@@ -1,7 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "active_support/core_ext/string"
4
- require "json"
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 = generate_result_class(results.first.keys)
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
- private
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
- def generate_result_class(attribute_names)
31
- @generate_result_class ||= ResultClassGenerator.generate(self, attribute_names)
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
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module ResultClassComponents
3
+ module ObjectMethods
4
4
  module Accessors
5
5
  def [](key)
6
6
  instance_variable_get("@#{key}")
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module ResultClassComponents
3
+ module ObjectMethods
4
4
  module Comparison
5
5
  def <=>(other)
6
6
  return nil unless other.is_a?(self.class)
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module ResultClassComponents
3
+ module ObjectMethods
4
4
  module KeyTransformation
5
5
  def deep_stringify_keys(obj)
6
6
  case obj
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module ResultClassComponents
3
+ module ObjectMethods
4
4
  module Serialization
5
5
  def to_h
6
6
  attribute_names.each_with_object({}) do |name, hash|
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module ResultClassComponents
3
+ module ObjectMethods
4
4
  module TypeCasting
5
5
  def type_cast_value(value, type_definition)
6
6
  return value if type_definition.nil?
@@ -51,14 +51,13 @@ class SQLPathResolver
51
51
  end
52
52
 
53
53
  def find_in_configured_paths(filename)
54
- configured_paths.find do |path|
55
- full_path = File.join(path, filename)
56
- return full_path if File.exist?(full_path)
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 configured_paths
61
- Kweerie.configuration.sql_paths.call
59
+ def path
60
+ Kweerie.configuration.default_path
62
61
  end
63
62
 
64
63
  def validate_file_exists!(path)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Kweerie
4
- VERSION = "0.2.0"
4
+ VERSION = "0.3.0"
5
5
  end
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.2.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-12 00:00:00.000000000 Z
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/result_class_components/accessors.rb
74
- - lib/kweerie/result_class_components/comparison.rb
75
- - lib/kweerie/result_class_components/key_transformation.rb
76
- - lib/kweerie/result_class_components/serialization.rb
77
- - lib/kweerie/result_class_components/type_casting.rb
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