api_recipes 2.10.3 → 3.0.0.beta1
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 +4 -4
- data/README.md +1276 -28
- data/UPGRADING.md +473 -0
- data/exe/api_recipes +6 -0
- data/lib/api_recipes/api.rb +71 -49
- data/lib/api_recipes/cli/upgrade.rb +179 -0
- data/lib/api_recipes/cli.rb +70 -0
- data/lib/api_recipes/configuration.rb +185 -36
- data/lib/api_recipes/endpoint.rb +68 -118
- data/lib/api_recipes/errors.rb +137 -0
- data/lib/api_recipes/internals/compiler.rb +680 -0
- data/lib/api_recipes/internals/context.rb +237 -0
- data/lib/api_recipes/internals/credential.rb +86 -0
- data/lib/api_recipes/internals/declaration.rb +225 -0
- data/lib/api_recipes/internals/definition.rb +179 -0
- data/lib/api_recipes/internals/handles.rb +81 -0
- data/lib/api_recipes/internals/level.rb +192 -0
- data/lib/api_recipes/internals/path_template.rb +131 -0
- data/lib/api_recipes/internals/registry.rb +146 -0
- data/lib/api_recipes/internals/request.rb +285 -0
- data/lib/api_recipes/internals/template.rb +73 -0
- data/lib/api_recipes/response.rb +90 -9
- data/lib/api_recipes/version.rb +3 -1
- data/lib/api_recipes.rb +44 -143
- metadata +43 -13
- data/lib/api_recipes/exceptions.rb +0 -91
- data/lib/api_recipes/route.rb +0 -165
- data/lib/api_recipes/settings.rb +0 -30
- data/lib/api_recipes/utils.rb +0 -195
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ApiRecipes
|
|
4
|
+
module Internals
|
|
5
|
+
# Who uses which api: the compiled tree, the owner whose levels hold
|
|
6
|
+
# credentials and variables, the declaration in use (nil for a global api)
|
|
7
|
+
# and the options given to with. Immutable: with returns a new context.
|
|
8
|
+
# Values are read when a request is built, never cached. generation is the
|
|
9
|
+
# one of the registry the tree was compiled for (nil: a context built by
|
|
10
|
+
# hand, which no configure replaces).
|
|
11
|
+
class Context
|
|
12
|
+
# Levels by distance from the request: the smaller, the nearer.
|
|
13
|
+
RANKS = { with: 0, instance: 1, class: 2, global: 3 }.freeze
|
|
14
|
+
DEFAULT_PORTS = { 'http' => 80, 'https' => 443 }.freeze
|
|
15
|
+
|
|
16
|
+
attr_reader :definition, :owner, :global_definition, :overrides, :handles, :declaration
|
|
17
|
+
|
|
18
|
+
def initialize(definition:, owner:, global_definition: nil, overrides: nil, handles: nil, declaration: nil,
|
|
19
|
+
generation: nil)
|
|
20
|
+
@definition = definition
|
|
21
|
+
@owner = owner
|
|
22
|
+
@global_definition = global_definition
|
|
23
|
+
@overrides = (overrides || {}).freeze
|
|
24
|
+
@handles = handles
|
|
25
|
+
@declaration = declaration
|
|
26
|
+
@generation = generation
|
|
27
|
+
freeze
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# A new context with these options on top of the current ones, as the
|
|
31
|
+
# compiler resolved them. default_headers and variables add up, the other
|
|
32
|
+
# options replace. A key given with nil: no credential for authorization
|
|
33
|
+
# and basic_auth, no override for the others.
|
|
34
|
+
def with(options)
|
|
35
|
+
resolved = Compiler.resolve_options(options, :with_api)
|
|
36
|
+
problems = Compiler.unknown_variables(resolved[:variables], @definition)
|
|
37
|
+
raise ConfigurationError.new(problems) unless problems.empty?
|
|
38
|
+
|
|
39
|
+
# The names as the compiler reads them, in the order given, nil ones included.
|
|
40
|
+
names = Compiler.normalize(options.to_h { |key, _value| [key, nil] }).keys
|
|
41
|
+
merged = @overrides.dup
|
|
42
|
+
names.each do |name|
|
|
43
|
+
merge_option(merged, name, resolved[name]) if Compiler::WITH_API_KEYS.include?(name)
|
|
44
|
+
end
|
|
45
|
+
self.class.new(definition: @definition, owner: @owner, global_definition: @global_definition,
|
|
46
|
+
overrides: merged, handles: @handles, declaration: @declaration, generation: @generation)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# The key of the levels of this api for its readers and writers: the
|
|
50
|
+
# declaration in use, or the api name for a global api.
|
|
51
|
+
def level_key = @declaration || @definition.name
|
|
52
|
+
|
|
53
|
+
# The levels that hold a value for this api, nearest first. A request
|
|
54
|
+
# reads them once and passes them as chain to the methods below.
|
|
55
|
+
def levels = Levels.chain(@owner, @definition.name, @declaration)
|
|
56
|
+
|
|
57
|
+
# The value of an option, from the nearest place that has one.
|
|
58
|
+
def setting(key, endpoint = nil, endpoint_overrides = nil)
|
|
59
|
+
if endpoint
|
|
60
|
+
value = endpoint_overrides && endpoint_overrides[key]
|
|
61
|
+
return value unless value.nil?
|
|
62
|
+
|
|
63
|
+
value = endpoint.own(key)
|
|
64
|
+
return value unless value.nil?
|
|
65
|
+
end
|
|
66
|
+
value = @overrides[key]
|
|
67
|
+
return value unless value.nil?
|
|
68
|
+
|
|
69
|
+
@definition.respond_to?(key) ? @definition.public_send(key) : nil
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# Declared defaults, then the levels from the farthest up to up_to. For
|
|
73
|
+
# :global the defaults are those of the global definition (none without it).
|
|
74
|
+
def variables(up_to: :with, chain: levels)
|
|
75
|
+
sum_variables(definition_for(up_to), chain, start_of(chain, up_to), up_to == :with)
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# "scheme://host" or "scheme://host:port". Only :with applies the protocol,
|
|
79
|
+
# host and port given to with. nil when a variable is missing or there is
|
|
80
|
+
# no global definition, except for :with, which raises MissingVariable.
|
|
81
|
+
def origin(up_to: :with, chain: levels)
|
|
82
|
+
origin_from(definition_for(up_to), chain, start_of(chain, up_to), with: up_to == :with)
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def base_path
|
|
86
|
+
text = @overrides[:base_url]
|
|
87
|
+
template = text ? Template.new(text, :base_url) : @definition.base_url_template
|
|
88
|
+
return '/' unless template
|
|
89
|
+
|
|
90
|
+
"/#{template.expand(variables, api: @definition.name)}".squeeze('/')
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# The default headers with their variables filled, then the ones given to with.
|
|
94
|
+
def headers(chain: levels)
|
|
95
|
+
values = variables(chain: chain)
|
|
96
|
+
templates = @definition.header_templates
|
|
97
|
+
added = @overrides[:default_headers] || {}
|
|
98
|
+
templates = templates.merge(added.transform_values { |text| Template.new(text, :header) }) unless added.empty?
|
|
99
|
+
templates.transform_values { |template| template.expand(values, api: @definition.name) }
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# The nearest credential: the one given to with, then the levels from the
|
|
103
|
+
# instance to the global api. An explicit "none" ends the search with nil.
|
|
104
|
+
# A credential taken from a level is bound to the origin of that level: if
|
|
105
|
+
# destination (the origin of the request, by default) is another one, it
|
|
106
|
+
# raises CredentialOriginMismatch.
|
|
107
|
+
def credential(destination = nil, chain: nil)
|
|
108
|
+
given = @overrides[:credential]
|
|
109
|
+
return (given.none? ? nil : given) if given
|
|
110
|
+
|
|
111
|
+
chain ||= levels
|
|
112
|
+
chain.each_with_index do |(_kind, level), index|
|
|
113
|
+
found = level.credential
|
|
114
|
+
next if found.nil?
|
|
115
|
+
return nil if found.none?
|
|
116
|
+
|
|
117
|
+
check_origin(chain, index, destination || origin(chain: chain))
|
|
118
|
+
return found
|
|
119
|
+
end
|
|
120
|
+
nil
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# The credential a request would look at first, as assigned, without
|
|
124
|
+
# checking its origin: what the readers of an api return. Nil when no
|
|
125
|
+
# level has one; Credential::NONE when the nearest one is "none".
|
|
126
|
+
def assigned_credential
|
|
127
|
+
given = @overrides[:credential]
|
|
128
|
+
return given if given
|
|
129
|
+
|
|
130
|
+
levels.each do |_kind, level|
|
|
131
|
+
return level.credential unless level.credential.nil?
|
|
132
|
+
end
|
|
133
|
+
nil
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
# The origin for inspect. It never raises: a host whose variables have
|
|
137
|
+
# no valid value is shown as written.
|
|
138
|
+
def shown_origin
|
|
139
|
+
origin
|
|
140
|
+
rescue Error
|
|
141
|
+
address(@definition, @overrides[:host] || @definition.host, with: true)
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def inspect = "#<#{self.class.name} #{@definition.name}>"
|
|
145
|
+
|
|
146
|
+
def to_s = inspect
|
|
147
|
+
|
|
148
|
+
def pretty_print(queue) = queue.text(inspect)
|
|
149
|
+
|
|
150
|
+
# The options of with can hold header values: a Context is never serialized.
|
|
151
|
+
def marshal_dump = raise(TypeError, 'an ApiRecipes::Internals::Context cannot be serialized')
|
|
152
|
+
|
|
153
|
+
def encode_with(_coder) = raise(TypeError, 'an ApiRecipes::Internals::Context cannot be serialized')
|
|
154
|
+
|
|
155
|
+
private
|
|
156
|
+
|
|
157
|
+
# The tree a level reads: the global one for the global level, else the owner's.
|
|
158
|
+
def definition_for(kind) = kind == :global ? @global_definition : @definition
|
|
159
|
+
|
|
160
|
+
# Index of the first level of chain at or farther than up_to.
|
|
161
|
+
def start_of(chain, up_to)
|
|
162
|
+
rank = RANKS.fetch(up_to) { raise ArgumentError, "unknown level '#{up_to}'" }
|
|
163
|
+
chain.index { |kind, _| RANKS.fetch(kind) >= rank } || chain.size
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
def sum_variables(api, chain, start, with)
|
|
167
|
+
values = {}
|
|
168
|
+
api&.variables&.each { |name, spec| values[name] = spec[:default] unless spec[:default].nil? }
|
|
169
|
+
chain[start..].reverse_each do |_kind, level|
|
|
170
|
+
level.variables&.each { |name, value| values[name.respond_to?(:to_sym) ? name.to_sym : name] = value }
|
|
171
|
+
end
|
|
172
|
+
values.merge!(@overrides[:variables]) if with && @overrides[:variables]
|
|
173
|
+
values
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
# The origin of api with the variables of the levels from start to the
|
|
177
|
+
# farthest; with: true also applies the options given to with.
|
|
178
|
+
def origin_from(api, chain, start, with:)
|
|
179
|
+
return nil unless api
|
|
180
|
+
|
|
181
|
+
values = sum_variables(api, chain, start, with)
|
|
182
|
+
host = with && @overrides[:host] ? Template.new(@overrides[:host], :host) : api.host_template
|
|
183
|
+
address(api, host.expand(values, api: api.name), with: with)
|
|
184
|
+
rescue MissingVariable
|
|
185
|
+
raise if with
|
|
186
|
+
|
|
187
|
+
nil
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
# "scheme://host" or "scheme://host:port", without the default port.
|
|
191
|
+
def address(api, host, with:)
|
|
192
|
+
protocol = (with && @overrides[:protocol]) || api.protocol
|
|
193
|
+
port = (with && @overrides[:port]) || api.port
|
|
194
|
+
port = nil if port == DEFAULT_PORTS[protocol]
|
|
195
|
+
"#{protocol}://#{host}#{":#{port}" if port}"
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
# The origin of a credential comes from its own level and the farther ones,
|
|
199
|
+
# by position: a nearer level that moves the destination does not move it.
|
|
200
|
+
# The global level uses the global definition, the others the owner's one:
|
|
201
|
+
# every other level of the chain was written through the declaration in
|
|
202
|
+
# use, whose tree that is.
|
|
203
|
+
def check_origin(chain, index, destination)
|
|
204
|
+
bound = origin_from(definition_in_force(chain[index].first), chain, index, with: false)
|
|
205
|
+
return if bound && destination && bound.casecmp?(destination)
|
|
206
|
+
|
|
207
|
+
raise CredentialOriginMismatch.new(@definition.name, bound, destination)
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
# The tree a level reads now. An api or an endpoint kept across a
|
|
211
|
+
# configure still holds the tree it was created with, and so its
|
|
212
|
+
# destination, while the levels are read when the request is sent: their
|
|
213
|
+
# credentials are bound to the origin that the configuration in force
|
|
214
|
+
# gives them. nil when that configuration no longer has the api.
|
|
215
|
+
def definition_in_force(kind)
|
|
216
|
+
registry = Registry.current
|
|
217
|
+
return definition_for(kind) if @generation.nil? || @generation == registry.generation
|
|
218
|
+
return registry.definitions[@definition.name] if kind == :global || @declaration.nil?
|
|
219
|
+
|
|
220
|
+
@declaration.definition
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
# value is the one resolved by the compiler, nil when the key was given as nil.
|
|
224
|
+
def merge_option(merged, key, value)
|
|
225
|
+
if %i[authorization basic_auth].include?(key)
|
|
226
|
+
merged[:credential] = Credential.from(key, value)
|
|
227
|
+
elsif value.nil?
|
|
228
|
+
merged.delete(key)
|
|
229
|
+
elsif %i[default_headers variables].include?(key)
|
|
230
|
+
merged[key] = (merged[key] || {}).merge(value).freeze
|
|
231
|
+
else
|
|
232
|
+
merged[key] = value
|
|
233
|
+
end
|
|
234
|
+
end
|
|
235
|
+
end
|
|
236
|
+
end
|
|
237
|
+
end
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ApiRecipes
|
|
4
|
+
module Internals
|
|
5
|
+
# A secret sent with requests. It never shows its value: not in inspect, not
|
|
6
|
+
# in to_s, not in error messages.
|
|
7
|
+
class Credential
|
|
8
|
+
KINDS = %i[authorization basic_auth none].freeze
|
|
9
|
+
CONTROL_CHARACTERS = /[[:cntrl:]]/
|
|
10
|
+
|
|
11
|
+
class << self
|
|
12
|
+
def authorization(value)
|
|
13
|
+
check_text(value, 'the authorization value')
|
|
14
|
+
new(:authorization, value.dup.freeze)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def basic_auth(value)
|
|
18
|
+
unless value.is_a?(Hash) && value.key?(:user) && value.key?(:pass)
|
|
19
|
+
raise InvalidCredential, 'basic_auth must be a Hash with :user and :pass'
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
check_text(value[:user], 'the basic_auth user')
|
|
23
|
+
check_text(value[:pass], 'the basic_auth pass')
|
|
24
|
+
new(:basic_auth, { user: value[:user].dup.freeze, pass: value[:pass].dup.freeze }.freeze)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def from(kind, value)
|
|
28
|
+
return NONE if value.nil?
|
|
29
|
+
|
|
30
|
+
case kind
|
|
31
|
+
when :authorization then authorization(value)
|
|
32
|
+
when :basic_auth then basic_auth(value)
|
|
33
|
+
else raise InvalidCredential, "unknown kind #{kind.inspect}"
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
private
|
|
38
|
+
|
|
39
|
+
def check_text(value, what)
|
|
40
|
+
raise InvalidCredential, "#{what} must be a String" unless value.is_a?(String)
|
|
41
|
+
raise InvalidCredential, "#{what} must be valid UTF-8" unless value.valid_encoding?
|
|
42
|
+
raise InvalidCredential, "#{what} contains a control character" if value.match?(CONTROL_CHARACTERS)
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
attr_reader :kind, :value
|
|
47
|
+
|
|
48
|
+
def initialize(kind, value)
|
|
49
|
+
@kind = kind
|
|
50
|
+
@value = value
|
|
51
|
+
freeze
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def none? = @kind == :none
|
|
55
|
+
|
|
56
|
+
def inspect = "#<ApiRecipes::Internals::Credential #{@kind} [FILTERED]>"
|
|
57
|
+
|
|
58
|
+
def to_s = inspect
|
|
59
|
+
|
|
60
|
+
def pretty_print(queue) = queue.text(inspect)
|
|
61
|
+
|
|
62
|
+
# Serialized forms never carry the value: a rebuilt credential is NONE-like.
|
|
63
|
+
def marshal_dump = nil
|
|
64
|
+
|
|
65
|
+
def marshal_load(_data)
|
|
66
|
+
@kind = :none
|
|
67
|
+
@value = nil
|
|
68
|
+
freeze
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def encode_with(coder)
|
|
72
|
+
coder['kind'] = 'none'
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def init_with(_coder)
|
|
76
|
+
marshal_load(nil)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def as_json(*) = '[FILTERED]'
|
|
80
|
+
|
|
81
|
+
def to_json(*) = '"[FILTERED]"'
|
|
82
|
+
|
|
83
|
+
NONE = new(:none, nil)
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
end
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ApiRecipes
|
|
4
|
+
module Internals
|
|
5
|
+
# One `api :name` in a class or a module: its options and the tree they
|
|
6
|
+
# make with the global configuration, compiled at the first use after every
|
|
7
|
+
# configure. Only the class or module that declared it refers to it (an
|
|
8
|
+
# instance variable and the generated methods); its subclasses and the
|
|
9
|
+
# classes that include the module share it.
|
|
10
|
+
#
|
|
11
|
+
# A credential or a variable set through a declaration is stored under it
|
|
12
|
+
# and read only by requests that the same declaration serves (see
|
|
13
|
+
# Levels.chain), so it never reaches the tree of another declaration of the
|
|
14
|
+
# api. An api name is also declared once per hierarchy, checked when `api`
|
|
15
|
+
# is called and again when a declaration serves an owner, to give a clear
|
|
16
|
+
# error when include or extend brings a second one: the levels do not
|
|
17
|
+
# depend on that check.
|
|
18
|
+
class Declaration
|
|
19
|
+
IVAR = :@__api_recipes_declarations
|
|
20
|
+
Tree = Data.define(:generation, :definition, :handles, :global, :api_class)
|
|
21
|
+
|
|
22
|
+
class << self
|
|
23
|
+
# Checks the arguments of `api`, then defines the method on klass and
|
|
24
|
+
# on its instances. Returns the name of the method.
|
|
25
|
+
def declare(klass, name, options)
|
|
26
|
+
unless name.is_a?(String) || name.is_a?(Symbol)
|
|
27
|
+
raise ArgumentError, "api name must be a Symbol or a String, not #{name.class}"
|
|
28
|
+
end
|
|
29
|
+
if name.empty? || (name.is_a?(String) && !name.valid_encoding?)
|
|
30
|
+
raise ArgumentError, 'api name must not be empty and must be valid UTF-8'
|
|
31
|
+
end
|
|
32
|
+
raise ApiConfigIsNotAnHash.new(name) unless options.nil? || options.is_a?(Hash)
|
|
33
|
+
|
|
34
|
+
options = Compiler.normalize(options || {})
|
|
35
|
+
method_name = method_for(options.delete(:as), name)
|
|
36
|
+
variables = options.delete(:variables)
|
|
37
|
+
api_name = name.to_sym
|
|
38
|
+
check_clash(klass, api_name, method_name)
|
|
39
|
+
variables = Registry.variable_values(variables)
|
|
40
|
+
global = Registry.current.definitions[api_name]
|
|
41
|
+
check_variables(api_name, variables, global)
|
|
42
|
+
# The variables that the options use are those of the global api.
|
|
43
|
+
# Without one yet, the tree compiled at the first use checks them.
|
|
44
|
+
errors = Compiler.check_options(options, :api, variables: global ? global.variables.keys : :any)
|
|
45
|
+
raise ConfigurationError.new(errors) unless errors.empty?
|
|
46
|
+
|
|
47
|
+
declaration = new(klass, api_name, Compiler.frozen_copy(options), variables)
|
|
48
|
+
declared = klass.instance_variable_get(IVAR)
|
|
49
|
+
klass.instance_variable_set(IVAR, (declared.is_a?(Hash) ? declared : {}).merge(api_name => declaration).freeze)
|
|
50
|
+
declaration.write_variables
|
|
51
|
+
klass.define_method(method_name) { declaration.api(self) }
|
|
52
|
+
klass.define_singleton_method(method_name) { declaration.api(self) }
|
|
53
|
+
method_name
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
private
|
|
57
|
+
|
|
58
|
+
# The names of variables against the global api, when it is configured
|
|
59
|
+
# already; else refresh checks them at the first use.
|
|
60
|
+
def check_variables(api_name, variables, definition)
|
|
61
|
+
return unless definition
|
|
62
|
+
|
|
63
|
+
problems = Compiler.unknown_variables(variables, definition, api_name.to_s)
|
|
64
|
+
raise ConfigurationError.new(problems) unless problems.empty?
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def method_for(as, name)
|
|
68
|
+
return name.to_sym if as.nil?
|
|
69
|
+
return as.to_sym if (as.is_a?(String) || as.is_a?(Symbol)) && Compiler::METHOD_NAME.match?(as.to_s)
|
|
70
|
+
|
|
71
|
+
raise ConfigurationError.new(['as: must be a valid method name'])
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# One declaration of an api name per class hierarchy, never on a
|
|
75
|
+
# singleton class; and no method of the class or of its instances is
|
|
76
|
+
# replaced.
|
|
77
|
+
def check_clash(klass, api_name, method_name)
|
|
78
|
+
raise ApiNameClashError.new(klass, api_name) if klass.singleton_class?
|
|
79
|
+
|
|
80
|
+
holder = klass.ancestors.find { |mod| declares?(mod, api_name) }
|
|
81
|
+
holder ||= descendants(klass).find { |mod| declares?(mod, api_name) }
|
|
82
|
+
raise ApiNameClashError.new(klass, api_name, holder) if holder
|
|
83
|
+
|
|
84
|
+
taken = Handles::RESERVED.include?(method_name) || klass.respond_to?(method_name, true) ||
|
|
85
|
+
klass.method_defined?(method_name) || klass.private_method_defined?(method_name)
|
|
86
|
+
raise ApiNameClashError.new(klass, method_name) if taken
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def declares?(mod, api_name)
|
|
90
|
+
declared = mod.instance_variable_get(IVAR)
|
|
91
|
+
declared.is_a?(Hash) && declared.key?(api_name)
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def descendants(klass)
|
|
95
|
+
return [] unless klass.is_a?(Class)
|
|
96
|
+
|
|
97
|
+
found = []
|
|
98
|
+
pending = klass.subclasses
|
|
99
|
+
until pending.empty?
|
|
100
|
+
subclass = pending.pop
|
|
101
|
+
found << subclass
|
|
102
|
+
pending.concat(subclass.subclasses)
|
|
103
|
+
end
|
|
104
|
+
found
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def initialize(klass, name, options, variables)
|
|
109
|
+
@klass = klass
|
|
110
|
+
@name = name
|
|
111
|
+
@options = options
|
|
112
|
+
@variables = variables
|
|
113
|
+
@variables_generation = nil
|
|
114
|
+
@tree = nil
|
|
115
|
+
@lock = Mutex.new
|
|
116
|
+
@checked = ObjectSpace::WeakMap.new
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# The class or module where `api` was called.
|
|
120
|
+
def declared_in = @klass
|
|
121
|
+
|
|
122
|
+
# The api for owner: the class or module, a subclass, an includer, or an
|
|
123
|
+
# instance of one of them.
|
|
124
|
+
def api(owner)
|
|
125
|
+
tree = current_tree
|
|
126
|
+
check_owner(owner, tree.generation)
|
|
127
|
+
tree.api_class.new(Context.new(definition: tree.definition, owner: owner, global_definition: tree.global,
|
|
128
|
+
handles: tree.handles, declaration: self, generation: tree.generation))
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
# The tree of this declaration under the configuration in force.
|
|
132
|
+
def definition = current_tree.definition
|
|
133
|
+
|
|
134
|
+
# Writes the declared variables in the level of the class, again after
|
|
135
|
+
# a reset! of the levels.
|
|
136
|
+
def write_variables
|
|
137
|
+
return if @variables.nil?
|
|
138
|
+
|
|
139
|
+
LevelSet.write(@klass, self) { |level| level.with(variables: @variables) }
|
|
140
|
+
@variables_generation = LevelSet.generation
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
def inspect = "#<#{self.class.name} #{@name}>"
|
|
144
|
+
|
|
145
|
+
def to_s = inspect
|
|
146
|
+
|
|
147
|
+
def pretty_print(queue) = queue.text(inspect)
|
|
148
|
+
|
|
149
|
+
private
|
|
150
|
+
|
|
151
|
+
# Refuses an owner whose hierarchy holds another declaration of this api.
|
|
152
|
+
# The ancestors of its class (the owner itself for a class or module) are
|
|
153
|
+
# checked once per class and registry generation. An owner that reaches a
|
|
154
|
+
# module's declaration through extend has a singleton class: its
|
|
155
|
+
# ancestors are checked too. Only an owner with singleton methods has
|
|
156
|
+
# one, so asking for it never creates one (an object can also reach the
|
|
157
|
+
# declaration through a method copied with define_method).
|
|
158
|
+
def check_owner(owner, generation)
|
|
159
|
+
klass = owner.is_a?(Module) ? owner : owner.class
|
|
160
|
+
unless @checked[klass] == generation
|
|
161
|
+
holder = other_declarer(klass.ancestors)
|
|
162
|
+
raise ApiNameClashError.new(klass, @name, holder) if holder
|
|
163
|
+
|
|
164
|
+
@checked[klass] = generation
|
|
165
|
+
end
|
|
166
|
+
return if @klass.is_a?(Class) || klass <= @klass || owner.singleton_methods.empty?
|
|
167
|
+
|
|
168
|
+
holder = other_declarer(owner.singleton_class.ancestors)
|
|
169
|
+
raise ApiNameClashError.new(klass, @name, holder) if holder
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
def other_declarer(modules)
|
|
173
|
+
modules.find do |mod|
|
|
174
|
+
declared = mod.instance_variable_get(IVAR)
|
|
175
|
+
declared.is_a?(Hash) && declared.key?(@name) && !declared[@name].equal?(self)
|
|
176
|
+
end
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
def current_tree
|
|
180
|
+
tree = @tree
|
|
181
|
+
return tree if tree && tree.generation == Registry.current.generation
|
|
182
|
+
|
|
183
|
+
@lock.synchronize { refresh }
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
# Without options of its own the class uses the global tree; with them,
|
|
187
|
+
# or without a global api, its own tree, compiled from the global
|
|
188
|
+
# configuration and the options of the class. The registry is read under
|
|
189
|
+
# the lock, so an older registry never replaces a newer tree.
|
|
190
|
+
def refresh
|
|
191
|
+
registry = Registry.current
|
|
192
|
+
tree = @tree
|
|
193
|
+
return tree if tree && tree.generation == registry.generation
|
|
194
|
+
|
|
195
|
+
global = registry.definitions[@name]
|
|
196
|
+
global_handles = registry.handles[@name]
|
|
197
|
+
if @options.empty? && global
|
|
198
|
+
definition = global
|
|
199
|
+
handles = global_handles
|
|
200
|
+
else
|
|
201
|
+
definition = Compiler.compile(@name, own_config(registry.configs[@name]))
|
|
202
|
+
handles = Handles.new(definition)
|
|
203
|
+
warnings = handles.warnings - (global_handles ? global_handles.warnings : [])
|
|
204
|
+
warnings.each { |message| ApiRecipes.logger.warn(message) }
|
|
205
|
+
end
|
|
206
|
+
problems = Compiler.unknown_variables(@variables, definition, @name.to_s)
|
|
207
|
+
raise ConfigurationError.new(problems) unless problems.empty?
|
|
208
|
+
|
|
209
|
+
write_variables if @variables_generation != LevelSet.generation
|
|
210
|
+
@tree = Tree.new(generation: registry.generation, definition: definition, handles: handles, global: global,
|
|
211
|
+
api_class: handles.for(definition))
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
# The configuration of the tree of the class: its options over those of
|
|
215
|
+
# the global api. For an api that only the class declares, the names
|
|
216
|
+
# given in variables: are its variables.
|
|
217
|
+
def own_config(global_config)
|
|
218
|
+
return Compiler.deep_merge(global_config, @options) if global_config
|
|
219
|
+
return @options if @variables.nil? || @variables.empty?
|
|
220
|
+
|
|
221
|
+
@options.merge(variables: @variables.to_h { |name, _value| [name, {}] })
|
|
222
|
+
end
|
|
223
|
+
end
|
|
224
|
+
end
|
|
225
|
+
end
|