diva 1.0.3 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.circleci/config.yml +32 -5
- data/.rubocop.yml +343 -0
- data/diva.gemspec +2 -1
- data/lib/diva/datasource.rb +2 -1
- data/lib/diva/error.rb +1 -1
- data/lib/diva/field_generator.rb +0 -8
- data/lib/diva/model.rb +18 -26
- data/lib/diva/model_extend.rb +3 -3
- data/lib/diva/spec.rb +1 -1
- data/lib/diva/type.rb +24 -23
- data/lib/diva/uri.rb +5 -5
- data/lib/diva/version.rb +1 -1
- data/lib/diva.rb +1 -2
- metadata +23 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 927072ed062a2f9959488484142e4db0484c0084305426cf16ef80108ff22971
|
4
|
+
data.tar.gz: 5fc1ee93546cbe77541b212d0a3abf43c2ab4591564993889bea8307f667505b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f01a2df2a5477857590c8460e39507fcd07f83ea67daf5ebc4924f630439d43bb7344a64be3bde7cb300a314c013af9ca47fb484d91993a64aed46a7bc7f2289
|
7
|
+
data.tar.gz: 33aff1e9079cfd0dc68b472a7e491741a3afe1500c671e8157e78c331dc6e5f212bec4c69fe77520c1d2ad6bfb92204baf79d16ae1d52c521bd36881eba2883b
|
data/.circleci/config.yml
CHANGED
@@ -26,15 +26,42 @@ jobs:
|
|
26
26
|
- run:
|
27
27
|
name: test
|
28
28
|
command: bundle exec rake test
|
29
|
+
lint:
|
30
|
+
parameters:
|
31
|
+
ruby-version:
|
32
|
+
type: string
|
33
|
+
executor:
|
34
|
+
name: ruby
|
35
|
+
tag: << parameters.ruby-version >>
|
36
|
+
steps:
|
37
|
+
- checkout
|
38
|
+
- run:
|
39
|
+
name: Which bundler?
|
40
|
+
command: bundle -v
|
41
|
+
- run:
|
42
|
+
command: bundle install --path vendor/bundle
|
43
|
+
- run:
|
44
|
+
name: rubocop
|
45
|
+
command: bundle exec rubocop lib/
|
46
|
+
|
29
47
|
workflows:
|
30
48
|
build:
|
31
49
|
jobs:
|
32
|
-
-
|
33
|
-
name: '
|
34
|
-
ruby-version: '
|
50
|
+
- lint:
|
51
|
+
name: 'lint'
|
52
|
+
ruby-version: '3.0.2'
|
35
53
|
- build:
|
36
54
|
name: 'ruby-2.6'
|
37
|
-
ruby-version: '2.6.
|
55
|
+
ruby-version: '2.6.7'
|
56
|
+
requires:
|
57
|
+
- 'lint'
|
38
58
|
- build:
|
39
59
|
name: 'ruby-2.7'
|
40
|
-
ruby-version: '2.7.
|
60
|
+
ruby-version: '2.7.4'
|
61
|
+
requires:
|
62
|
+
- 'lint'
|
63
|
+
- build:
|
64
|
+
name: 'ruby-3.0'
|
65
|
+
ruby-version: '3.0.2'
|
66
|
+
requires:
|
67
|
+
- 'lint'
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,343 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.5
|
3
|
+
|
4
|
+
Metrics/BlockLength:
|
5
|
+
Exclude:
|
6
|
+
- test/**/*.rb
|
7
|
+
- lib/tasks/**/*.rake
|
8
|
+
- Gemfile
|
9
|
+
Max: 156
|
10
|
+
|
11
|
+
Layout/LineLength:
|
12
|
+
AllowHeredoc: true
|
13
|
+
AllowURI: true
|
14
|
+
Max: 293
|
15
|
+
Exclude:
|
16
|
+
- test/**/*.rb
|
17
|
+
|
18
|
+
Metrics/ModuleLength:
|
19
|
+
Max: 109
|
20
|
+
CountAsOne: ['array', 'hash', 'heredoc']
|
21
|
+
Exclude:
|
22
|
+
- test/**/*.rb
|
23
|
+
|
24
|
+
Naming/AccessorMethodName:
|
25
|
+
Enabled: false
|
26
|
+
|
27
|
+
Style/Alias:
|
28
|
+
Enabled: false
|
29
|
+
|
30
|
+
Style/AsciiComments:
|
31
|
+
Enabled: false
|
32
|
+
|
33
|
+
Naming/AsciiIdentifiers:
|
34
|
+
Enabled: false
|
35
|
+
|
36
|
+
Metrics/BlockNesting:
|
37
|
+
Enabled: false
|
38
|
+
|
39
|
+
Style/CaseEquality:
|
40
|
+
Enabled: false
|
41
|
+
|
42
|
+
Style/ClassAndModuleChildren:
|
43
|
+
Enabled: false
|
44
|
+
|
45
|
+
Metrics/ClassLength:
|
46
|
+
Enabled: false
|
47
|
+
|
48
|
+
Style/ClassVars:
|
49
|
+
Enabled: false
|
50
|
+
|
51
|
+
Style/CollectionMethods:
|
52
|
+
PreferredMethods:
|
53
|
+
collect: map
|
54
|
+
detect: find
|
55
|
+
find_all: select
|
56
|
+
member?: include?
|
57
|
+
reduce: inject
|
58
|
+
|
59
|
+
Style/ColonMethodCall:
|
60
|
+
Enabled: true
|
61
|
+
|
62
|
+
Style/CommentAnnotation:
|
63
|
+
Enabled: false
|
64
|
+
|
65
|
+
Metrics/CyclomaticComplexity:
|
66
|
+
Enabled: false
|
67
|
+
|
68
|
+
Style/Documentation:
|
69
|
+
Enabled: false
|
70
|
+
|
71
|
+
Layout/DotPosition:
|
72
|
+
EnforcedStyle: leading
|
73
|
+
|
74
|
+
Style/DoubleNegation:
|
75
|
+
Enabled: false
|
76
|
+
|
77
|
+
Style/EachWithObject:
|
78
|
+
Enabled: false
|
79
|
+
|
80
|
+
Style/EmptyLiteral:
|
81
|
+
Enabled: true
|
82
|
+
|
83
|
+
Style/Encoding:
|
84
|
+
Enabled: false
|
85
|
+
|
86
|
+
Style/FormatString:
|
87
|
+
EnforcedStyle: percent
|
88
|
+
|
89
|
+
Style/FrozenStringLiteralComment:
|
90
|
+
Enabled: false
|
91
|
+
|
92
|
+
Style/GlobalVars:
|
93
|
+
Enabled: true
|
94
|
+
|
95
|
+
Style/GuardClause:
|
96
|
+
Enabled: false
|
97
|
+
|
98
|
+
Style/IfUnlessModifier:
|
99
|
+
Enabled: false
|
100
|
+
|
101
|
+
Style/IfWithSemicolon:
|
102
|
+
Enabled: false
|
103
|
+
|
104
|
+
Layout/IndentationConsistency:
|
105
|
+
EnforcedStyle: normal
|
106
|
+
|
107
|
+
Style/InlineComment:
|
108
|
+
Enabled: false
|
109
|
+
|
110
|
+
Style/Lambda:
|
111
|
+
EnforcedStyle: literal
|
112
|
+
|
113
|
+
Style/LambdaCall:
|
114
|
+
Enabled: false
|
115
|
+
|
116
|
+
Metrics/MethodLength:
|
117
|
+
Enabled: false
|
118
|
+
|
119
|
+
Style/ModuleFunction:
|
120
|
+
Enabled: false
|
121
|
+
|
122
|
+
Style/NegatedIf:
|
123
|
+
Enabled: true
|
124
|
+
EnforcedStyle: both
|
125
|
+
|
126
|
+
Style/Next:
|
127
|
+
Enabled: false
|
128
|
+
|
129
|
+
# false と nil の違いで分岐するようなコードは書くべきではない。
|
130
|
+
# `if a.nil?` のようなコードは、 `if a` に置き換える。
|
131
|
+
# nil と false を区別せざるを得ないところでのみ、 Style/NilComparison を無効化して nil? 利用すること
|
132
|
+
Style/NilComparison:
|
133
|
+
EnforcedStyle: comparison
|
134
|
+
|
135
|
+
Style/NumericLiterals:
|
136
|
+
Enabled: false
|
137
|
+
|
138
|
+
Style/OneLineConditional:
|
139
|
+
Enabled: false
|
140
|
+
|
141
|
+
Style/ParallelAssignment:
|
142
|
+
Enabled: false
|
143
|
+
|
144
|
+
Metrics/ParameterLists:
|
145
|
+
Enabled: false
|
146
|
+
|
147
|
+
Naming/PredicateName:
|
148
|
+
ForbiddenPrefixes:
|
149
|
+
- is_
|
150
|
+
MethodDefinitionMacros:
|
151
|
+
- define_method
|
152
|
+
- define_singleton_method
|
153
|
+
- defdsl
|
154
|
+
Exclude:
|
155
|
+
- 'test/*/'
|
156
|
+
|
157
|
+
Style/RaiseArgs:
|
158
|
+
Enabled: false
|
159
|
+
|
160
|
+
Style/RegexpLiteral:
|
161
|
+
Enabled: false
|
162
|
+
|
163
|
+
Style/SingleLineBlockParams:
|
164
|
+
Enabled: false
|
165
|
+
|
166
|
+
Style/SignalException:
|
167
|
+
Enabled: false
|
168
|
+
|
169
|
+
Style/StringLiterals:
|
170
|
+
EnforcedStyle: single_quotes
|
171
|
+
|
172
|
+
Style/TrailingCommaInArrayLiteral:
|
173
|
+
Enabled: true
|
174
|
+
EnforcedStyleForMultiline: no_comma
|
175
|
+
|
176
|
+
Style/TrailingCommaInHashLiteral:
|
177
|
+
Enabled: true
|
178
|
+
EnforcedStyleForMultiline: no_comma
|
179
|
+
|
180
|
+
Style/TrailingCommaInArguments:
|
181
|
+
Enabled: true
|
182
|
+
EnforcedStyleForMultiline: no_comma
|
183
|
+
|
184
|
+
Style/FormatStringToken:
|
185
|
+
EnforcedStyle: template
|
186
|
+
|
187
|
+
Style/TrivialAccessors:
|
188
|
+
Enabled: false
|
189
|
+
|
190
|
+
Lint/UnusedMethodArgument:
|
191
|
+
Enabled: false
|
192
|
+
|
193
|
+
Naming/VariableNumber:
|
194
|
+
Enabled: false
|
195
|
+
|
196
|
+
Style/WhileUntilModifier:
|
197
|
+
Enabled: false
|
198
|
+
|
199
|
+
Style/WordArray:
|
200
|
+
Enabled: false
|
201
|
+
|
202
|
+
Lint/AssignmentInCondition:
|
203
|
+
AllowSafeAssignment: false
|
204
|
+
|
205
|
+
Lint/SuppressedException:
|
206
|
+
Enabled: false
|
207
|
+
|
208
|
+
Lint/UnderscorePrefixedVariableName:
|
209
|
+
AllowKeywordBlockArguments: true
|
210
|
+
|
211
|
+
Lint/Void:
|
212
|
+
Enabled: true
|
213
|
+
|
214
|
+
Lint/UselessAssignment:
|
215
|
+
Exclude:
|
216
|
+
- test/**/*.rb
|
217
|
+
|
218
|
+
Layout/EmptyLines:
|
219
|
+
Exclude:
|
220
|
+
- test/**/*.rb
|
221
|
+
|
222
|
+
Style/NumericPredicate:
|
223
|
+
EnforcedStyle: comparison
|
224
|
+
|
225
|
+
# 結果が配列になるもの → []
|
226
|
+
# 配列にならないもの → <>
|
227
|
+
Style/PercentLiteralDelimiters:
|
228
|
+
PreferredDelimiters:
|
229
|
+
"%": '<>'
|
230
|
+
"%i": '[]'
|
231
|
+
"%q": '<>'
|
232
|
+
"%Q": '<>'
|
233
|
+
"%r": '<>'
|
234
|
+
"%s": '<>'
|
235
|
+
"%w": '[]'
|
236
|
+
"%W": '[]'
|
237
|
+
"%x": '<>'
|
238
|
+
|
239
|
+
Layout/SpaceAroundMethodCallOperator:
|
240
|
+
Enabled: true
|
241
|
+
|
242
|
+
Lint/RaiseException:
|
243
|
+
Enabled: true
|
244
|
+
|
245
|
+
Lint/StructNewOverride:
|
246
|
+
Enabled: true
|
247
|
+
|
248
|
+
Style/ExponentialNotation:
|
249
|
+
Enabled: true
|
250
|
+
|
251
|
+
Style/HashEachMethods:
|
252
|
+
Enabled: true
|
253
|
+
|
254
|
+
Style/HashTransformKeys:
|
255
|
+
Enabled: true
|
256
|
+
|
257
|
+
Style/HashTransformValues:
|
258
|
+
Enabled: true
|
259
|
+
|
260
|
+
Style/PreferredHashMethods:
|
261
|
+
EnforcedStyle: verbose
|
262
|
+
|
263
|
+
Layout/EmptyLineAfterGuardClause:
|
264
|
+
Enabled: false
|
265
|
+
|
266
|
+
Style/BlockDelimiters:
|
267
|
+
EnforcedStyle: braces_for_chaining
|
268
|
+
|
269
|
+
Metrics/AbcSize:
|
270
|
+
Max: 24.27
|
271
|
+
|
272
|
+
Style/MultilineBlockChain:
|
273
|
+
Enabled: false
|
274
|
+
|
275
|
+
Layout/MultilineMethodCallIndentation:
|
276
|
+
EnforcedStyle: indented_relative_to_receiver
|
277
|
+
|
278
|
+
Naming/MemoizedInstanceVariableName:
|
279
|
+
EnforcedStyleForLeadingUnderscores: optional
|
280
|
+
|
281
|
+
Layout/SpaceAroundOperators:
|
282
|
+
EnforcedStyleForExponentOperator: space
|
283
|
+
|
284
|
+
Layout/SpaceAroundEqualsInParameterDefault:
|
285
|
+
EnforcedStyle: no_space
|
286
|
+
|
287
|
+
Style/BlockComments:
|
288
|
+
Enabled: false
|
289
|
+
|
290
|
+
Naming/MethodName:
|
291
|
+
EnforcedStyle: snake_case
|
292
|
+
IgnoredPatterns:
|
293
|
+
- URI
|
294
|
+
- Type
|
295
|
+
|
296
|
+
Naming/RescuedExceptionsVariableName:
|
297
|
+
PreferredName: "exception"
|
298
|
+
|
299
|
+
Layout/SpaceBeforeBrackets: # (new in 1.7)
|
300
|
+
Enabled: true
|
301
|
+
Lint/AmbiguousAssignment: # (new in 1.7)
|
302
|
+
Enabled: true
|
303
|
+
Lint/DeprecatedConstants: # (new in 1.8)
|
304
|
+
Enabled: true
|
305
|
+
Lint/DuplicateBranch: # (new in 1.3)
|
306
|
+
Enabled: true
|
307
|
+
Lint/DuplicateRegexpCharacterClassElement: # (new in 1.1)
|
308
|
+
Enabled: true
|
309
|
+
Lint/EmptyBlock: # (new in 1.1)
|
310
|
+
Enabled: true
|
311
|
+
Lint/EmptyClass: # (new in 1.3)
|
312
|
+
Enabled: true
|
313
|
+
Lint/LambdaWithoutLiteralBlock: # (new in 1.8)
|
314
|
+
Enabled: true
|
315
|
+
Lint/NoReturnInBeginEndBlocks: # (new in 1.2)
|
316
|
+
Enabled: true
|
317
|
+
Lint/RedundantDirGlobSort: # (new in 1.8)
|
318
|
+
Enabled: true
|
319
|
+
Lint/ToEnumArguments: # (new in 1.1)
|
320
|
+
Enabled: true
|
321
|
+
Lint/UnexpectedBlockArity: # (new in 1.5)
|
322
|
+
Enabled: true
|
323
|
+
Lint/UnmodifiedReduceAccumulator: # (new in 1.1)
|
324
|
+
Enabled: true
|
325
|
+
Style/ArgumentsForwarding: # (new in 1.1)
|
326
|
+
Enabled: true
|
327
|
+
Style/CollectionCompact: # (new in 1.2)
|
328
|
+
Enabled: true
|
329
|
+
Style/DocumentDynamicEvalDefinition: # (new in 1.1)
|
330
|
+
Enabled: true
|
331
|
+
Style/EndlessMethod: # (new in 1.8)
|
332
|
+
Enabled: true
|
333
|
+
Style/HashExcept: # (new in 1.7)
|
334
|
+
Enabled: true
|
335
|
+
Style/NegatedIfElseCondition: # (new in 1.2)
|
336
|
+
Enabled: true
|
337
|
+
Style/NilLambda: # (new in 1.3)
|
338
|
+
Enabled: true
|
339
|
+
Style/RedundantArgument: # (new in 1.4)
|
340
|
+
Enabled: true
|
341
|
+
Style/SwapValues: # (new in 1.1)
|
342
|
+
Enabled: true
|
343
|
+
|
data/diva.gemspec
CHANGED
@@ -12,6 +12,7 @@ Gem::Specification.new do |spec|
|
|
12
12
|
spec.summary = %q{Implementation of expression for handling things.}
|
13
13
|
spec.homepage = "https://github.com/toshia/diva"
|
14
14
|
spec.license = "MIT"
|
15
|
+
spec.required_ruby_version = '>= 2.6.0'
|
15
16
|
|
16
17
|
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
18
|
spec.bindir = "exe"
|
@@ -23,6 +24,6 @@ Gem::Specification.new do |spec|
|
|
23
24
|
spec.add_development_dependency "minitest", '>= 5.14.4', '< 5.15'
|
24
25
|
spec.add_development_dependency "irb", '>= 1.3.7', '< 2.0'
|
25
26
|
spec.add_development_dependency "simplecov", '>= 0.17.1', '< 1.0'
|
26
|
-
|
27
|
+
spec.add_development_dependency "rubocop", '>= 1.20.0', '< 1.21.0'
|
27
28
|
spec.add_dependency "addressable", ">= 2.5.2", "< 3.0"
|
28
29
|
end
|
data/lib/diva/datasource.rb
CHANGED
data/lib/diva/error.rb
CHANGED
data/lib/diva/field_generator.rb
CHANGED
data/lib/diva/model.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# -*- coding: utf-8 -*-
|
2
|
+
|
2
3
|
=begin rdoc
|
3
4
|
いろんなリソースの基底クラス
|
4
5
|
=end
|
@@ -56,13 +57,13 @@ class Diva::Model
|
|
56
57
|
end
|
57
58
|
|
58
59
|
def hash
|
59
|
-
@_hash ||=
|
60
|
+
@_hash ||= uri.to_s.hash ^ self.class.hash
|
60
61
|
end
|
61
62
|
|
62
63
|
def <=>(other)
|
63
64
|
if other.is_a?(Diva::Model)
|
64
65
|
created - other.created
|
65
|
-
elsif other.respond_to?(:[])
|
66
|
+
elsif other.respond_to?(:[]) && other[:created]
|
66
67
|
created - other[:created]
|
67
68
|
else
|
68
69
|
id - other
|
@@ -80,13 +81,11 @@ class Diva::Model
|
|
80
81
|
end
|
81
82
|
|
82
83
|
def to_hash
|
83
|
-
|
84
|
+
self.class.fields.to_h { |f| [f.name, fetch(f.name)] }
|
84
85
|
end
|
85
86
|
|
86
87
|
def to_json(*rest)
|
87
|
-
|
88
|
-
self.class.fields.map{|f| [f.name, f.dump_for_json(fetch(f.name))] }
|
89
|
-
].to_json(*rest)
|
88
|
+
self.class.fields.to_h { |f| [f.name, f.dump_for_json(fetch(f.name))] }.to_json(*rest)
|
90
89
|
end
|
91
90
|
|
92
91
|
# カラムの生の内容を返す
|
@@ -100,32 +99,27 @@ class Diva::Model
|
|
100
99
|
def []=(key, value)
|
101
100
|
@value[key.to_sym] = value
|
102
101
|
self.class.store_datum(self)
|
103
|
-
value
|
104
102
|
end
|
105
103
|
|
106
104
|
# カラムと型が違うものがある場合、例外を発生させる。
|
107
105
|
def validate
|
108
|
-
raise
|
106
|
+
raise "argument is #{@value}, not Hash" unless @value.is_a?(Hash)
|
109
107
|
self.class.fields.each do |field|
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
raise Diva::InvalidTypeError, "#{err} in field `#{field}'"
|
114
|
-
end
|
108
|
+
@value[field.name] = field.type.cast(@value[field.name])
|
109
|
+
rescue Diva::InvalidTypeError => exception
|
110
|
+
raise Diva::InvalidTypeError, "#{exception} in field `#{field}'"
|
115
111
|
end
|
116
112
|
end
|
117
113
|
|
118
114
|
# キーとして定義されていない値を全て除外した配列を生成して返す。
|
119
115
|
# また、Modelを子に含んでいる場合、それを外部キーに変換する。
|
120
116
|
def filtering
|
121
|
-
datum =
|
122
|
-
result =
|
117
|
+
datum = to_hash
|
118
|
+
result = {}
|
123
119
|
self.class.fields.each do |field|
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
raise Diva::InvalidTypeError, "#{err} in field `#{field}'"
|
128
|
-
end
|
120
|
+
result[field.name] = field.type.cast(datum[field.name])
|
121
|
+
rescue Diva::InvalidTypeError => exception
|
122
|
+
raise Diva::InvalidTypeError, "#{exception} in field `#{field}'"
|
129
123
|
end
|
130
124
|
result
|
131
125
|
end
|
@@ -133,10 +127,9 @@ class Diva::Model
|
|
133
127
|
# このインスタンスのタイトル。
|
134
128
|
def title
|
135
129
|
fields = self.class.fields.lazy.map(&:name)
|
136
|
-
|
137
|
-
when fields.include?(:name)
|
130
|
+
if fields.include?(:name)
|
138
131
|
name.gsub("\n", '')
|
139
|
-
|
132
|
+
elsif fields.include?(:description)
|
140
133
|
description.gsub("\n", '')
|
141
134
|
else
|
142
135
|
to_s.gsub("\n", '')
|
@@ -146,7 +139,7 @@ class Diva::Model
|
|
146
139
|
def dig(key, *args)
|
147
140
|
return nil unless key.respond_to?(:to_sym)
|
148
141
|
value = fetch(key)
|
149
|
-
if value
|
142
|
+
if value == nil || args.empty?
|
150
143
|
value
|
151
144
|
else
|
152
145
|
value.dig(*args)
|
@@ -154,10 +147,9 @@ class Diva::Model
|
|
154
147
|
end
|
155
148
|
|
156
149
|
private
|
150
|
+
|
157
151
|
# URIがデフォルトで使うpath要素
|
158
152
|
def path
|
159
153
|
@path ||= "/#{SecureRandom.uuid}"
|
160
154
|
end
|
161
|
-
|
162
155
|
end
|
163
|
-
|
data/lib/diva/model_extend.rb
CHANGED
@@ -15,14 +15,14 @@ module Diva::ModelExtend
|
|
15
15
|
# ==== Return
|
16
16
|
# [String] URIスキーム
|
17
17
|
def scheme
|
18
|
-
@_scheme ||=
|
18
|
+
@_scheme ||= to_s.split('::', 2).first.gsub(/\W/, '').downcase.freeze
|
19
19
|
end
|
20
20
|
|
21
21
|
# Modelのインスタンスのホスト名。オーバライドして適切な値にする
|
22
22
|
# ==== Return
|
23
23
|
# [String] ホスト名
|
24
24
|
def host
|
25
|
-
@_host ||=
|
25
|
+
@_host ||= to_s.split('::', 2).last.split('::').reverse.join('.').gsub(/[^\w.]/, '').downcase.freeze
|
26
26
|
end
|
27
27
|
|
28
28
|
# Modelにフィールドを追加する。
|
@@ -55,7 +55,7 @@ module Diva::ModelExtend
|
|
55
55
|
@fields ||= []
|
56
56
|
end
|
57
57
|
alias :keys :fields
|
58
|
-
deprecate :keys,
|
58
|
+
deprecate :keys, 'fields', 2018, 2
|
59
59
|
|
60
60
|
def schema
|
61
61
|
{
|
data/lib/diva/spec.rb
CHANGED
data/lib/diva/type.rb
CHANGED
@@ -27,7 +27,7 @@ Diva::Modelのサブクラスであれば、それを制約とすることがで
|
|
27
27
|
配列の全ての要素が設定された制約を満たしていれば、配列制約が満たされたことになる。
|
28
28
|
|
29
29
|
=end
|
30
|
-
require
|
30
|
+
require 'time'
|
31
31
|
require 'diva/uri'
|
32
32
|
|
33
33
|
module Diva::Type
|
@@ -73,7 +73,7 @@ module Diva::Type
|
|
73
73
|
end
|
74
74
|
|
75
75
|
def inspect
|
76
|
-
"Diva::Type(#{
|
76
|
+
"Diva::Type(#{self})"
|
77
77
|
end
|
78
78
|
end
|
79
79
|
|
@@ -84,12 +84,12 @@ module Diva::Type
|
|
84
84
|
end
|
85
85
|
|
86
86
|
def recommendation_point(value)
|
87
|
-
_, point = @recommended_classes.map.with_index{|k, i| [k, i] }.find{|k, _| value.is_a?(k) }
|
87
|
+
_, point = @recommended_classes.map.with_index { |k, i| [k, i] }.find { |k, _| value.is_a?(k) }
|
88
88
|
point
|
89
89
|
end
|
90
90
|
|
91
91
|
def schema
|
92
|
-
@schema ||= {type: uri}.freeze
|
92
|
+
@schema ||= { type: uri }.freeze
|
93
93
|
end
|
94
94
|
|
95
95
|
def uri
|
@@ -169,7 +169,7 @@ module Diva::Type
|
|
169
169
|
end
|
170
170
|
end
|
171
171
|
NULL = AtomicType.new(:null, NilClass) do |v|
|
172
|
-
if v
|
172
|
+
if v == nil
|
173
173
|
v
|
174
174
|
else
|
175
175
|
raise Diva::InvalidTypeError, "The value is not a `#{name}'."
|
@@ -178,13 +178,14 @@ module Diva::Type
|
|
178
178
|
|
179
179
|
class ModelType < MetaType
|
180
180
|
attr_reader :model
|
181
|
+
|
181
182
|
def initialize(model, *rest, &cast)
|
182
183
|
super(:model, *rest)
|
183
184
|
@model = model
|
184
185
|
end
|
185
186
|
|
186
|
-
def recommendation_point(
|
187
|
-
|
187
|
+
def recommendation_point(value)
|
188
|
+
value.is_a?(model) && 0
|
188
189
|
end
|
189
190
|
|
190
191
|
def cast(value)
|
@@ -199,7 +200,7 @@ module Diva::Type
|
|
199
200
|
end
|
200
201
|
|
201
202
|
def schema
|
202
|
-
@schema ||= {type: uri}.freeze
|
203
|
+
@schema ||= { type: uri }.freeze
|
203
204
|
end
|
204
205
|
|
205
206
|
def to_s
|
@@ -219,7 +220,7 @@ module Diva::Type
|
|
219
220
|
end
|
220
221
|
|
221
222
|
def recommendation_point(values)
|
222
|
-
values.is_a?(Enumerable) && values.all?{|v| @type.recommendation_point(v) } && 0
|
223
|
+
values.is_a?(Enumerable) && values.all? { |v| @type.recommendation_point(v) } && 0
|
223
224
|
end
|
224
225
|
|
225
226
|
def cast(value)
|
@@ -232,7 +233,7 @@ module Diva::Type
|
|
232
233
|
end
|
233
234
|
|
234
235
|
def to_s
|
235
|
-
"Array of #{@type
|
236
|
+
"Array of #{@type}"
|
236
237
|
end
|
237
238
|
|
238
239
|
def schema
|
@@ -246,8 +247,8 @@ module Diva::Type
|
|
246
247
|
super("union_#{@types.map(&:name).join('_')}")
|
247
248
|
end
|
248
249
|
|
249
|
-
def recommendation_point(
|
250
|
-
@types.map{|t| t.recommendation_point(
|
250
|
+
def recommendation_point(value)
|
251
|
+
@types.map { |t| t.recommendation_point(value) }.compact.min
|
251
252
|
end
|
252
253
|
|
253
254
|
def cast(value)
|
@@ -267,18 +268,18 @@ module Diva::Type
|
|
267
268
|
end
|
268
269
|
|
269
270
|
def recommended_type_of(value)
|
270
|
-
@types.map{|t|
|
271
|
+
available_types = @types.map { |t|
|
271
272
|
[t, t.recommendation_point(value)]
|
272
|
-
}.select{|_,
|
273
|
-
|
274
|
-
|
273
|
+
}.select { |_, p| p }
|
274
|
+
unless available_types.empty?
|
275
|
+
best, = available_types.min_by { |_, p| p }
|
276
|
+
return best
|
277
|
+
end
|
275
278
|
@types.each do |type|
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
# try next
|
281
|
-
end
|
279
|
+
type.cast(value)
|
280
|
+
return type
|
281
|
+
rescue Diva::InvalidTypeError
|
282
|
+
# try next
|
282
283
|
end
|
283
284
|
raise Diva::InvalidTypeError, "The value is not #{self}"
|
284
285
|
end
|
@@ -304,7 +305,7 @@ module Diva
|
|
304
305
|
Diva::Type::URI
|
305
306
|
when :null
|
306
307
|
Diva::Type::NULL
|
307
|
-
when ->x{x.
|
308
|
+
when ->(x) { x.instance_of?(Class) && x.ancestors.include?(Diva::Model) }
|
308
309
|
Diva::Type.model_of(type)
|
309
310
|
when Array
|
310
311
|
if type.size >= 2
|
data/lib/diva/uri.rb
CHANGED
@@ -46,9 +46,9 @@ class Diva::URI
|
|
46
46
|
def ==(other)
|
47
47
|
case other
|
48
48
|
when URI, Addressable::URI, String
|
49
|
-
other.to_s ==
|
49
|
+
other.to_s == to_s
|
50
50
|
when Diva::URI
|
51
|
-
if has_string?
|
51
|
+
if has_string? || other.has_string?
|
52
52
|
to_s == other.to_s
|
53
53
|
else
|
54
54
|
other.to_uri == to_uri
|
@@ -69,15 +69,15 @@ class Diva::URI
|
|
69
69
|
end
|
70
70
|
|
71
71
|
def to_s
|
72
|
-
@uri_string ||= to_uri.to_s.freeze
|
72
|
+
@uri_string ||= to_uri.to_s.freeze # rubocop:disable Naming/MemoizedInstanceVariableName
|
73
73
|
end
|
74
74
|
|
75
75
|
def to_uri
|
76
|
-
@uri ||= generate_uri.freeze
|
76
|
+
@uri ||= generate_uri.freeze # rubocop:disable Naming/MemoizedInstanceVariableName
|
77
77
|
end
|
78
78
|
|
79
79
|
def scheme
|
80
|
-
if has_string?
|
80
|
+
if has_string? && !has_uri?
|
81
81
|
match = @uri_string.match(%r<\A(\w+):>)
|
82
82
|
if match
|
83
83
|
match[1]
|
data/lib/diva/version.rb
CHANGED
data/lib/diva.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# coding: utf-8
|
2
|
+
|
2
3
|
require 'diva/version'
|
3
4
|
require 'diva/datasource'
|
4
5
|
require 'diva/error'
|
@@ -8,8 +9,6 @@ require 'diva/model'
|
|
8
9
|
require 'diva/spec'
|
9
10
|
require 'diva/type'
|
10
11
|
require 'diva/uri'
|
11
|
-
require 'diva/version'
|
12
|
-
|
13
12
|
|
14
13
|
module Diva
|
15
14
|
def self.URI(uri)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: diva
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Toshiaki Asai
|
@@ -110,6 +110,26 @@ dependencies:
|
|
110
110
|
- - "<"
|
111
111
|
- !ruby/object:Gem::Version
|
112
112
|
version: '1.0'
|
113
|
+
- !ruby/object:Gem::Dependency
|
114
|
+
name: rubocop
|
115
|
+
requirement: !ruby/object:Gem::Requirement
|
116
|
+
requirements:
|
117
|
+
- - ">="
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: 1.20.0
|
120
|
+
- - "<"
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: 1.21.0
|
123
|
+
type: :development
|
124
|
+
prerelease: false
|
125
|
+
version_requirements: !ruby/object:Gem::Requirement
|
126
|
+
requirements:
|
127
|
+
- - ">="
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
version: 1.20.0
|
130
|
+
- - "<"
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
version: 1.21.0
|
113
133
|
- !ruby/object:Gem::Dependency
|
114
134
|
name: addressable
|
115
135
|
requirement: !ruby/object:Gem::Requirement
|
@@ -139,6 +159,7 @@ extra_rdoc_files: []
|
|
139
159
|
files:
|
140
160
|
- ".circleci/config.yml"
|
141
161
|
- ".gitignore"
|
162
|
+
- ".rubocop.yml"
|
142
163
|
- ".travis.yml"
|
143
164
|
- Gemfile
|
144
165
|
- LICENSE.txt
|
@@ -170,7 +191,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
170
191
|
requirements:
|
171
192
|
- - ">="
|
172
193
|
- !ruby/object:Gem::Version
|
173
|
-
version:
|
194
|
+
version: 2.6.0
|
174
195
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
175
196
|
requirements:
|
176
197
|
- - ">="
|