jsapi 0.9.0 → 0.9.1

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: e68467f73e8f1e909a03cd8c50f16674231dc0d070f05a5368b84e0f75dedfe6
4
- data.tar.gz: 6c0b1304cb7192d223f517d5233fd96fa13aad7e0affb6832f313088bc3b3993
3
+ metadata.gz: 4631e17cbd92a36a62ab6e7aa139b65afa83fccc2c41da34c3f757f2b077340f
4
+ data.tar.gz: 4c547aeb52a0fc74c09389f2c97dd0b8c90883fd36c79b9c502689a25379f3f5
5
5
  SHA512:
6
- metadata.gz: 45b860d17ffc8f22cfbe4fca26d38e727ff87ddc93a959c6285fd1aaa8eb0de9c5e2a9c516f717f0f6d463b0bfe5c8708b9efa5bab5aa6b08eb642d90ee84d10
7
- data.tar.gz: fe573b4b85e50373bd832f1ec3d4fdd9c11fe57bf42527fe80021d6c84d2271e01161b08cdedcee587a69bd43a28219562d36ed0c127197e4cbfe34e05902aca
6
+ metadata.gz: 1db64f601b34597e5b13ee820482c5a72d2adef153feac7ef3e1141804df36cc8816f665ee18c73e1b30e50a8c13454bed4c7562d447b2fdaf1b3814d863bd09
7
+ data.tar.gz: a4bd27f4fcc7c50ee9447e70777ca54de3e81ee6fd7ab3124817baf54511c90d0d95cf461c09c7fdc42a3bf8b57bb62d91954bc96d8a44a77eb15f9d53d2f5be
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jsapi
4
+ # Holds the \Jsapi configuration.
5
+ class Configuration
6
+ # The path where the API definitions are located relative to +Rails.root+.
7
+ # The default is <code>"app/api_defs"</code>.
8
+ attr_accessor :api_defs_path
9
+
10
+ def initialize # :nodoc:
11
+ @api_defs_path = 'app/api_defs'
12
+ end
13
+
14
+ # Returns the absolute +Pathname+ for +args+ within +api_defs_path+.
15
+ def pathname(*args)
16
+ return unless (root = Rails.root)
17
+
18
+ root.join(*[api_defs_path, args].flatten)
19
+ end
20
+ end
21
+
22
+ class << self
23
+ # The singleton \Jsapi configuration.
24
+ def configuration
25
+ @configuration ||= Configuration.new
26
+ end
27
+ end
28
+ end
@@ -162,7 +162,7 @@ module Jsapi
162
162
  (operation.model || Model::Base).new(
163
163
  Parameters.new(
164
164
  params.except(:action, :controller, :format).permit!,
165
- headers,
165
+ request.headers,
166
166
  operation,
167
167
  definitions,
168
168
  strong: strong
@@ -3,9 +3,26 @@
3
3
  module Jsapi
4
4
  module DSL
5
5
  class Base
6
- def initialize(meta_model, &block)
6
+ def initialize(meta_model, pathname = nil, parent: nil, &block)
7
7
  @meta_model = meta_model
8
+ @pathname = pathname
9
+ @parent = parent
8
10
 
11
+ # Raise an error when pathname is attempted to be imported again
12
+ if pathname && (ancestor = parent)
13
+ while ancestor
14
+ if ancestor.pathname == pathname
15
+ raise Error, "Attempted #{pathname.to_path.inspect} to be imported again"
16
+ end
17
+
18
+ ancestor = ancestor.parent
19
+ end
20
+ end
21
+
22
+ # Evaluate the file to be imported
23
+ instance_eval(pathname.read, pathname.to_path) if pathname
24
+
25
+ # Evaluate block
9
26
  if block
10
27
  if meta_model.reference?
11
28
  raise Error, "reference can't be specified together with a block"
@@ -15,10 +32,30 @@ module Jsapi
15
32
  end
16
33
  end
17
34
 
35
+ # Imports the file named +filename+ relative to +Jsapi.configation.path+.
36
+ def import(filename)
37
+ raise ArgumentError, "file name can't be blank" if filename.blank?
38
+
39
+ pathname = Jsapi.configuration.pathname("#{filename}.rb")
40
+ self.class.new(@meta_model, pathname, parent: self)
41
+ end
42
+
43
+ # Imports the file named +filename+ relative to the current file's path.
44
+ def import_relative(filename)
45
+ raise ArgumentError, "file name can't be blank" if filename.blank?
46
+
47
+ pathname = (@pathname&.parent || Jsapi.configuration.pathname) + "#{filename}.rb"
48
+ self.class.new(@meta_model, pathname, parent: self)
49
+ end
50
+
18
51
  def respond_to_missing?(*args) # :nodoc:
19
52
  keyword?(args.first)
20
53
  end
21
54
 
55
+ protected
56
+
57
+ attr_reader :parent, :pathname
58
+
22
59
  private
23
60
 
24
61
  def define(*args, &block)
@@ -29,13 +29,29 @@ module Jsapi
29
29
  api_definitions { default(type, **keywords, &block) }
30
30
  end
31
31
 
32
- # The API definitions of the current class.
33
- def api_definitions(&block)
34
- @api_definitions ||= Meta::Definitions.new(
35
- owner: self,
36
- parent: superclass.try(:api_definitions)
37
- )
32
+ # Returns the API definitions associated with the current class. Adds additional
33
+ # definitions when any keywords or a block is specified.
34
+ #
35
+ # api_definitions base_path: '/foo' do
36
+ # operation 'bar'
37
+ # end
38
+ def api_definitions(**keywords, &block)
39
+ unless defined? @api_definitions
40
+ @api_definitions = Meta::Definitions.new(
41
+ owner: self,
42
+ parent: superclass.try(:api_definitions)
43
+ )
44
+ if (name = try(:name))
45
+ pathname = Jsapi.configuration.pathname(
46
+ name.deconstantize.split('::').map(&:underscore),
47
+ "#{name.demodulize.delete_suffix('Controller').underscore}.rb"
48
+ )
49
+ Definitions.new(@api_definitions, pathname) if pathname&.file?
50
+ end
51
+ end
52
+ @api_definitions.merge!(keywords) if keywords.any?
38
53
  Definitions.new(@api_definitions, &block) if block
54
+
39
55
  @api_definitions
40
56
  end
41
57
 
@@ -71,6 +87,11 @@ module Jsapi
71
87
  api_definitions { host(arg) }
72
88
  end
73
89
 
90
+ # Imports the file named +filename+ relative to +Jsapi.configation.path+.
91
+ def api_import(filename)
92
+ api_definitions { import(filename) }
93
+ end
94
+
74
95
  # Includes API definitions from +klasses+.
75
96
  def api_include(*klasses)
76
97
  api_definitions { include(*klasses) }
data/lib/jsapi/version.rb CHANGED
@@ -5,6 +5,6 @@ module Jsapi
5
5
  # NOTE: See https://bundler.io/guides/creating_gem.html
6
6
 
7
7
  # The current GEM version.
8
- VERSION = '0.9.0'
8
+ VERSION = '0.9.1'
9
9
  end
10
10
  end
data/lib/jsapi.rb CHANGED
@@ -3,7 +3,7 @@
3
3
  require 'jsapi/invalid_value_helper'
4
4
  require 'jsapi/invalid_value_error'
5
5
  require 'jsapi/invalid_argument_error'
6
-
6
+ require 'jsapi/configuration'
7
7
  require 'jsapi/model'
8
8
  require 'jsapi/meta'
9
9
  require 'jsapi/dsl'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.9.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Denis Göller
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-10-12 00:00:00.000000000 Z
11
+ date: 2024-10-18 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: denis@dmgoeller.de
@@ -17,6 +17,7 @@ extensions: []
17
17
  extra_rdoc_files: []
18
18
  files:
19
19
  - lib/jsapi.rb
20
+ - lib/jsapi/configuration.rb
20
21
  - lib/jsapi/controller.rb
21
22
  - lib/jsapi/controller/base.rb
22
23
  - lib/jsapi/controller/error.rb