stairwell 0.5.0 → 0.6.0

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: a9eb7f5f503d74ee340bc8e9011dcf587604c20d6fd959a5b006def17769b71a
4
- data.tar.gz: 6bcbbc5866685ef8d767428a33b5862b58952a849e37237f869622ba720806d0
3
+ metadata.gz: c3a4932425e6273ba11f06755995cc39d7045566808bd60870e5ed3ae866bc1a
4
+ data.tar.gz: 5694267cbbc05428eb8161c244d7d9ead34c21ee6999156a3046c08a736e524d
5
5
  SHA512:
6
- metadata.gz: fc068d002df5eddc9026ae6ce34c786697712c2b475255fdcf5e0a0e8eaedbe9b3dc138f505ca6328342cc2e7a99648fd78087ae26b6946a481ddaefec35d943
7
- data.tar.gz: 8d9b86448fe37776e4aef1c4a7620946af302f016516c06fe931975e6d00b9128ee1ce2d087567850bc384ff0fa147fbbf82a93cb6a2e94de9daf09918e009c4
6
+ metadata.gz: fdcb61bf3e077028063dc94ecdef023aaccc709fd326cdce4354af181be15c84fbd805b492a324bd6d2117a84c6981b481835cf5ebb879ae84fda9ae2077cbca
7
+ data.tar.gz: cbb08179152c8bb7205c2d469afe0794e8505757bd65b248e53e7d6f6639fd42fa87a0523bd42993a64e97c47363d702bb90d48f72540ed63468624a49a5b4f4
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- stairwell (0.5.0)
4
+ stairwell (0.6.0)
5
5
  activerecord (>= 4.2.11)
6
6
  rake
7
7
  sqlite3
data/README.md CHANGED
@@ -83,6 +83,22 @@ They types of the binds are validated too.
83
83
  The names binds in your sql are also validated.
84
84
  All types are quoted using ActiveRecord quoting, which will be different depending on your database type (Mysql, postgres etc.)
85
85
 
86
+ ## SQL files
87
+ Support for sql files is being trialed. If you would like to reference raw sql file instead of quoting the sql in the ruby file, you can. You just have to place the file adjacent to the ruby file and name them the same eq: ruby file is `users_count.rb` the file would need to be in the same directory and named `users_count.sql`. You also need to set the path in your config:
88
+
89
+ ```ruby
90
+ # in ruby project:
91
+ Stairwell.configure do |config|
92
+ config.path = './app/queries/'
93
+ end
94
+
95
+ # in Rails:
96
+ Stairwell.configure do |config|
97
+ config.path = "#{Rails.root}/app/queries"
98
+ end
99
+
100
+ ```
101
+
86
102
  ## Supported Types
87
103
 
88
104
  | Type | Values Accepted | Info |
@@ -9,6 +9,7 @@ module Stairwell
9
9
  end
10
10
 
11
11
  def sql(**args)
12
+ set_sql_string_from_file
12
13
  raise InvalidBindCount, 'Incorrect amount of args passed' if args.keys.sort != all_validations.keys.sort
13
14
 
14
15
  @type_hash = args.each_with_object({}) do |(name, value), hash|
@@ -18,7 +19,7 @@ module Stairwell
18
19
  end
19
20
 
20
21
  def query(string)
21
- @sql_string = string.squish
22
+ @sql_string ||= string.squish
22
23
  end
23
24
 
24
25
  private
@@ -28,6 +29,25 @@ module Stairwell
28
29
  def transformer
29
30
  BindTransformer.new(sql_string, type_hash)
30
31
  end
32
+
33
+ def set_sql_string_from_file
34
+ return unless Stairwell.configuration.path && File.exist?(associated_sql_file)
35
+
36
+ file = File.read(associated_sql_file)
37
+ @sql_string = file&.squish
38
+ end
39
+
40
+ def associated_sql_file
41
+ "#{Stairwell.configuration.path}#{camelized_class}.sql"
42
+ end
43
+
44
+ def camelized_class
45
+ self.to_s.gsub(/::/, '/')
46
+ .gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
47
+ .gsub(/([a-z\d])([A-Z])/,'\1_\2')
48
+ .tr("-", "_")
49
+ .downcase
50
+ end
31
51
  end
32
52
  end
33
53
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Stairwell
4
- VERSION = '0.5.0'
4
+ VERSION = '0.6.0'
5
5
  end
data/lib/stairwell.rb CHANGED
@@ -20,6 +20,16 @@ module Stairwell
20
20
  table_name: 'Stairwell::Types::TableNameType'
21
21
  }.freeze
22
22
 
23
+ class << self
24
+ def configuration
25
+ @configuration ||= Configuration.new
26
+ end
27
+
28
+ def configure
29
+ yield configuration
30
+ end
31
+ end
32
+
23
33
  # for development and testing
24
34
  unless defined?(Rails)
25
35
  ::ActiveRecord::Base.establish_connection(
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stairwell
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - tobyond
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-11-10 00:00:00.000000000 Z
11
+ date: 2023-11-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord