kirei 0.5.1 → 0.6.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: ea3eb5dc8a6ee187383e7b287491e9454a5c4aebdc77be57bd072b95d293c227
4
- data.tar.gz: 63320aa40f86c0be7efbfe3c64f5d4dbb0a56124abc304f7f9b1eac356390504
3
+ metadata.gz: 150c91695e10d4bc3fefecf38dfaa5511512aea79c265fdbdb39b3fe2ec9ebba
4
+ data.tar.gz: 38d983a823588c9c12a4db38cc7f2885e02a7bd7e602eba0f0f04c0581d399b2
5
5
  SHA512:
6
- metadata.gz: 8c637488aed457873e0f4f929446356fc2cce3a381f3c19dfb13ca0bed53bb3b3a77b0e749637fd47b23aad50690b77973f41b39c8ccbeda9457361a9a407f9d
7
- data.tar.gz: 5eed46d30bf6558b76cd8735928db3e8be1563a67d7a0bc36139b0a650dc40f6b39d03a5f85b35ff54bf5aecddc2b4a1926a810e2d6be119217d09f4e1189f58
6
+ metadata.gz: 072cf7863e6f97b5886ffd4cf0e28d65fda67f23a3beebf967bc2bff7e2776923ac6c07ef42708d69440c91cf4978fff63941e8a5ea6d9c377be9e036613e6ab
7
+ data.tar.gz: d776d2a47e7d44d0b78b1c7142ae708a04c843472912e46fa0ba93be08b5877349aaa3e243d34092f7560463fecb06ed4b45a994e0661780c95dacd98422a5f2
@@ -168,12 +168,13 @@ module Cli
168
168
 
169
169
  models_dir = #{app_name}.root
170
170
 
171
- Dir.glob("app/models/*.rb").each do |model_file|
171
+ Dir.glob("app/models/**/*.rb").each do |model_file|
172
172
  next if !model_file_name.nil? && model_file == model_file_name
173
173
 
174
174
  model_path = File.expand_path(model_file, models_dir)
175
- model_name = Zeitwerk::Inflector.new.camelize(File.basename(model_file, ".rb"), model_path)
176
- model_klass = Object.const_get(model_name)
175
+ modules = model_file.gsub("app/models/", "").gsub(".rb", "").split("/").map { |mod| Zeitwerk::Inflector.new.camelize(mod, model_path) }
176
+ const_name = modules.join("::")
177
+ model_klass = Object.const_get(const_name)
177
178
  table_name = model_klass.table_name
178
179
  schema = db.schema(table_name)
179
180
 
@@ -184,8 +185,10 @@ module Cli
184
185
  # Remove existing schema info comments if present
185
186
  updated_contents = file_contents.sub(/# == Schema Info\\n(.*?)(\\n#\\n)?\\n(?=\\s*class)/m, "")
186
187
 
187
- # Insert the new schema comments before the class definition
188
- modified_contents = updated_contents.sub(/(\A|\\n)(class \#{model_name})/m, "\\\\1\#{schema_comments}\\n\\n\\\\2")
188
+ # Insert the new schema comments before the module/class definition
189
+ first_const = modules.first
190
+ first_module_or_class = modules.count == 1 ? "class #{first_const}" : "module #{first_const}"
191
+ modified_contents = updated_contents.sub(/(A|\n)(#{first_module_or_class})/m, "\\1#{schema_comments}\n\n\\2")
189
192
 
190
193
  File.write(model_path, modified_contents)
191
194
  end
data/lib/kirei/config.rb CHANGED
@@ -37,5 +37,7 @@ module Kirei
37
37
  #
38
38
  # Source: https://sorbet.org/docs/tstruct#from_hash-gotchas
39
39
  prop :db_strict_type_resolving, T.nilable(T::Boolean), default: nil
40
+
41
+ prop :allowed_origins, T::Array[String], default: []
40
42
  end
41
43
  end
@@ -40,6 +40,9 @@ module Kirei
40
40
  sig { abstract.returns(T.untyped) }
41
41
  def db; end
42
42
 
43
+ sig { abstract.params(sql: T.untyped, params: T.untyped).returns(T.untyped) }
44
+ def exec_sql(sql, params); end
45
+
43
46
  sig { abstract.returns(T.untyped) }
44
47
  def human_id_length; end
45
48
 
@@ -34,6 +34,12 @@ module Kirei
34
34
  App.raw_db_connection
35
35
  end
36
36
 
37
+ sig { override.params(sql: String, params: T::Array[T.untyped]).returns(T::Array[T::Hash[Symbol, T.untyped]]) }
38
+ def exec_sql(sql, params)
39
+ # Splats aren't supported in Sorbet, see: https://sorbet.org/docs/error-reference#7019
40
+ T.unsafe(db).fetch(sql, *params).all
41
+ end
42
+
37
43
  sig do
38
44
  override.params(
39
45
  hash: T::Hash[Symbol, T.untyped],
@@ -107,7 +113,6 @@ module Kirei
107
113
  col_info.fetch(:db_type).match?(/vector\(\d+\)/)
108
114
  end
109
115
 
110
- # New method to cast an array to a vector
111
116
  sig { params(value: T.any(T::Array[Numeric], Sequel::SQL::Expression)).returns(Sequel::SQL::Expression) }
112
117
  def cast_to_vector(value)
113
118
  return value if value.is_a?(Sequel::SQL::Expression) || value.is_a?(Sequel::SQL::PlaceholderLiteralString)
@@ -98,6 +98,8 @@ module Kirei
98
98
  headers[header_name] ||= default_value
99
99
  end
100
100
 
101
+ add_cors_headers(headers, env)
102
+
101
103
  [
102
104
  status,
103
105
  headers,
@@ -143,7 +145,6 @@ module Kirei
143
145
 
144
146
  sig { returns(T::Hash[String, String]) }
145
147
  def default_headers
146
- # "Access-Control-Allow-Origin": the user should set that, see comment about "cors" above
147
148
  {
148
149
  # security relevant headers
149
150
  "X-Frame-Options" => "DENY",
@@ -159,6 +160,18 @@ module Kirei
159
160
  }
160
161
  end
161
162
 
163
+ sig { params(headers: T::Hash[String, String], env: RackEnvType).void }
164
+ def add_cors_headers(headers, env)
165
+ origin = T.cast(env.fetch("HTTP_ORIGIN"), String)
166
+ allowed_origins = Kirei::App.config.allowed_origins
167
+ return unless allowed_origins.include?(origin)
168
+
169
+ headers["Access-Control-Allow-Origin"] = origin
170
+ headers["Access-Control-Allow-Methods"] = "GET, POST, PUT, PATCH, DELETE, OPTIONS"
171
+ headers["Access-Control-Allow-Headers"] = "Content-Type, Authorization, Referer"
172
+ headers["Access-Control-Allow-Credentials"] = "true"
173
+ end
174
+
162
175
  sig { params(hooks: NilableHooksType).void }
163
176
  private def run_hooks(hooks)
164
177
  return if hooks.nil? || hooks.empty?
data/lib/kirei/version.rb CHANGED
@@ -2,5 +2,5 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  module Kirei
5
- VERSION = "0.5.1"
5
+ VERSION = "0.6.0"
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kirei
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ludwig Reinmiedl
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-09-20 00:00:00.000000000 Z
11
+ date: 2024-09-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oj