diva 1.0.3 → 2.0.1

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: bcdd3aba855cf6fa2b14b6ea0a396c80610d768a17a512c7683055af150ea2b6
4
- data.tar.gz: d4db4b5de59960f5b77f4f5f58adeb54c96bbb93db8a5881f5c99796c5bd5236
3
+ metadata.gz: e1f4b5814e98320545b1b911edff9206dd0d701065496f6bc61a89c3ad1e102b
4
+ data.tar.gz: d07c6bcca5ad1c5c12d45373f3c813232356833fe000c39122b51686af2877d7
5
5
  SHA512:
6
- metadata.gz: b7d47a4e79a6f34fc98c352b63bf5089c7ce2f073f3f9be11f828e69089827f395a3a6724742bf5e8251b8a0ba2f488f446faa776fdd02f3ca15ee4b2eeec128
7
- data.tar.gz: 8c871d68c3ea3e31525334cad2d85568b2a99ae5f78acaf768c3e801ca14759a00acf8e3d319bbe1e7da5199283d236761eeca0bae2884d7ff2ec6cb02a983ec
6
+ metadata.gz: a0bfefc01f13322c81d39ef8cd7c4d31f1e66e30f12b5bb9061b25069eccd524989e0f83b9a84ea82f4d9f63f93a68376100ebbb77f2d1e095aaf06e3c02b53f
7
+ data.tar.gz: a1c7995794e5aec9cced403b1f59cd57b2e615ecb4dff64acf395b040792af26837f450ab08891aac6c9a0d271cd9fef200a4fed935c6d1ce63e64c2e57b9a14
data/.circleci/config.yml CHANGED
@@ -6,7 +6,7 @@ executors:
6
6
  tag:
7
7
  type: string
8
8
  docker:
9
- - image: circleci/ruby:<< parameters.tag >>
9
+ - image: ruby:<< parameters.tag >>
10
10
 
11
11
  jobs:
12
12
  build:
@@ -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:
50
+ - lint:
51
+ name: 'lint'
52
+ ruby-version: '3.0.3'
32
53
  - build:
33
- name: 'ruby-2.5'
34
- ruby-version: '2.5.7'
54
+ name: 'ruby-2.7'
55
+ ruby-version: '2.7.5'
56
+ requires:
57
+ - 'lint'
35
58
  - build:
36
- name: 'ruby-2.6'
37
- ruby-version: '2.6.5'
59
+ name: 'ruby-3.0'
60
+ ruby-version: '3.0.3'
61
+ requires:
62
+ - 'lint'
38
63
  - build:
39
- name: 'ruby-2.7'
40
- ruby-version: '2.7.0'
64
+ name: 'ruby-3.1'
65
+ ruby-version: '3.1.0-preview1'
66
+ requires:
67
+ - 'lint'
data/.rubocop.yml ADDED
@@ -0,0 +1,394 @@
1
+ AllCops:
2
+ TargetRubyVersion: '2.7'
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
+ Gemspec/DateAssignment: # new in 1.10
344
+ Enabled: true
345
+ Gemspec/RequireMFA: # new in 1.23
346
+ Enabled: true
347
+ Layout/LineEndStringConcatenationIndentation: # new in 1.18
348
+ Enabled: true
349
+ Lint/AmbiguousOperatorPrecedence: # new in 1.21
350
+ Enabled: false
351
+ Lint/AmbiguousRange: # new in 1.19
352
+ Enabled: true
353
+ RequireParenthesesForMethodChains: true
354
+ Lint/EmptyInPattern: # new in 1.16
355
+ Enabled: true
356
+ Lint/IncompatibleIoSelectWithFiberScheduler: # new in 1.21
357
+ Enabled: true
358
+ Lint/NumberedParameterAssignment: # new in 1.9
359
+ Enabled: true
360
+ Lint/OrAssignmentToConstant: # new in 1.9
361
+ Enabled: true
362
+ Lint/RequireRelativeSelfPath: # new in 1.22
363
+ Enabled: true
364
+ Lint/SymbolConversion: # new in 1.9
365
+ Enabled: true
366
+ Lint/TripleQuotes: # new in 1.9
367
+ Enabled: true
368
+ Lint/UselessRuby2Keywords: # new in 1.23
369
+ Enabled: true
370
+ Security/IoMethods: # new in 1.22
371
+ Enabled: true
372
+ Style/HashConversion: # new in 1.10
373
+ Enabled: true
374
+ Style/IfWithBooleanLiteralBranches: # new in 1.9
375
+ Enabled: true
376
+ Style/InPatternThen: # new in 1.16
377
+ Enabled: true
378
+ Style/MultilineInPatternThen: # new in 1.16
379
+ Enabled: true
380
+ Style/NumberedParameters: # new in 1.22
381
+ Enabled: true
382
+ Style/NumberedParametersLimit: # new in 1.22
383
+ Enabled: true
384
+ Max: 1
385
+ Style/OpenStructUse: # new in 1.23
386
+ Enabled: true
387
+ Style/QuotedSymbols: # new in 1.16
388
+ Enabled: true
389
+ Style/RedundantSelfAssignmentBranch: # new in 1.19
390
+ Enabled: true
391
+ Style/SelectByRegexp: # new in 1.22
392
+ Enabled: true
393
+ Style/StringChars: # new in 1.12
394
+ Enabled: true
data/Rakefile CHANGED
@@ -1,10 +1,10 @@
1
- require "bundler/gem_tasks"
2
- require "rake/testtask"
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
3
 
4
4
  Rake::TestTask.new(:test) do |t|
5
- t.libs << "test"
6
- t.libs << "lib"
5
+ t.libs << 'test'
6
+ t.libs << 'lib'
7
7
  t.test_files = FileList['test/**/*_test.rb']
8
8
  end
9
9
 
10
- task :default => :test
10
+ task default: :test
data/bin/console CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require "bundler/setup"
4
- require "diva"
3
+ require 'bundler/setup'
4
+ require 'diva'
5
5
 
6
6
  # You can add fixtures and/or initialization code here to make experimenting
7
7
  # with your gem easier. You can also use a different console, if you like.
@@ -10,5 +10,5 @@ require "diva"
10
10
  # require "pry"
11
11
  # Pry.start
12
12
 
13
- require "irb"
13
+ require 'irb'
14
14
  IRB.start
data/diva.gemspec CHANGED
@@ -1,28 +1,33 @@
1
1
  # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
2
+
3
+ lib = File.expand_path('lib', __dir__)
3
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
5
  require 'diva/version'
5
6
 
6
7
  Gem::Specification.new do |spec|
7
- spec.name = "diva"
8
+ spec.name = 'diva'
8
9
  spec.version = Diva::VERSION
9
- spec.authors = ["Toshiaki Asai"]
10
- spec.email = ["toshi.alternative@gmail.com"]
11
-
12
- spec.summary = %q{Implementation of expression for handling things.}
13
- spec.homepage = "https://github.com/toshia/diva"
14
- spec.license = "MIT"
10
+ spec.authors = ['Toshiaki Asai']
11
+ spec.email = ['toshi.alternative@gmail.com']
15
12
 
16
- spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
- spec.bindir = "exe"
18
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
- spec.require_paths = ["lib"]
13
+ spec.summary = 'Implementation of expression for handling things.'
14
+ spec.homepage = 'https://github.com/toshia/diva'
15
+ spec.license = 'MIT'
16
+ spec.required_ruby_version = '>= 2.7.0'
20
17
 
21
- spec.add_development_dependency "bundler", '>= 1.16.1', '< 3.0'
22
- spec.add_development_dependency "rake", '>= 13.0.1', '< 14.0'
23
- spec.add_development_dependency "minitest", '>= 5.14.4', '< 5.15'
24
- spec.add_development_dependency "irb", '>= 1.3.7', '< 2.0'
25
- spec.add_development_dependency "simplecov", '>= 0.17.1', '< 1.0'
18
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r<^(test|spec|features)/>) }
19
+ spec.bindir = 'exe'
20
+ spec.executables = spec.files.grep(%r<^exe/>) { |f| File.basename(f) }
21
+ spec.require_paths = ['lib']
26
22
 
27
- spec.add_dependency "addressable", ">= 2.5.2", "< 3.0"
23
+ spec.add_development_dependency 'bundler', '>= 1.16.1', '< 3.0'
24
+ spec.add_development_dependency 'irb', '>= 1.3.7', '< 2.0'
25
+ spec.add_development_dependency 'minitest', '>= 5.14.4', '< 5.15'
26
+ spec.add_development_dependency 'rake', '>= 13.0.1', '< 14.0'
27
+ spec.add_development_dependency 'rubocop', '>= 1.23.0', '< 1.24.0'
28
+ spec.add_development_dependency 'simplecov', '>= 0.17.1', '< 1.0'
29
+ spec.add_dependency 'addressable', '>= 2.5.2', '< 3.0'
30
+ spec.metadata = {
31
+ 'rubygems_mfa_required' => 'true'
32
+ }
28
33
  end
@@ -18,7 +18,8 @@ module Diva::DataSource
18
18
 
19
19
  # 取得できたらそのDivaのインスタンスをキーにして実行されるDeferredを返す
20
20
  def idof(id)
21
- Thread.new{ findbyid(id) } end
21
+ Thread.new { findbyid(id) }
22
+ end
22
23
  alias [] idof
23
24
 
24
25
  # データの保存
data/lib/diva/error.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  # -*- coding: utf-8 -*-
2
+
2
3
  module Diva
3
4
  class DivaError < StandardError; end
4
5
 
@@ -15,5 +16,4 @@ module Diva
15
16
 
16
17
  # URIとして受け付けられない値を渡された
17
18
  class InvalidURIError < InvalidTypeError; end
18
-
19
19
  end
@@ -9,6 +9,10 @@ class Diva::FieldGenerator
9
9
  @model_klass.add_field(field_name, type: :int, required: required)
10
10
  end
11
11
 
12
+ def float(field_name, required: false)
13
+ @model_klass.add_field(field_name, type: :float, required: required)
14
+ end
15
+
12
16
  def string(field_name, required: false)
13
17
  @model_klass.add_field(field_name, type: :string, required: required)
14
18
  end
@@ -29,11 +33,3 @@ class Diva::FieldGenerator
29
33
  @model_klass.add_field(field_name, type: type, required: required)
30
34
  end
31
35
  end
32
-
33
-
34
-
35
-
36
-
37
-
38
-
39
-
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
@@ -23,7 +24,7 @@ class Diva::Model
23
24
  # selfにあってotherにもあるカラムはotherの内容で上書きされる。
24
25
  # 上書き後、データはDataSourceに保存される
25
26
  def merge(other)
26
- @value.update(other.to_hash)
27
+ @value.update(other.to_h)
27
28
  validate
28
29
  self.class.store_datum(self)
29
30
  end
@@ -56,13 +57,13 @@ class Diva::Model
56
57
  end
57
58
 
58
59
  def hash
59
- @_hash ||= self.uri.to_s.hash ^ self.class.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?(:[]) and other[:created]
66
+ elsif other.respond_to?(:[]) && other[:created]
66
67
  created - other[:created]
67
68
  else
68
69
  id - other
@@ -79,14 +80,12 @@ class Diva::Model
79
80
  self == other
80
81
  end
81
82
 
82
- def to_hash
83
- Hash[self.class.fields.map{|f| [f.name, fetch(f.name)] }]
83
+ def to_h
84
+ self.class.fields.to_h { |f| [f.name, fetch(f.name)] }
84
85
  end
85
86
 
86
87
  def to_json(*rest)
87
- Hash[
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 RuntimeError, "argument is #{@value}, not Hash" if not @value.is_a?(Hash)
106
+ raise "argument is #{@value}, not Hash" unless @value.is_a?(Hash)
109
107
  self.class.fields.each do |field|
110
- begin
111
- @value[field.name] = field.type.cast(@value[field.name])
112
- rescue Diva::InvalidTypeError => err
113
- raise Diva::InvalidTypeError, "#{err} in field `#{field}'"
114
- end
108
+ @value[field.name] = field.type.cast(@value[field.name])
109
+ rescue Diva::InvalidTypeError
110
+ raise Diva::InvalidTypeError, "#{self.class}.#{field} (#{field.type}): Invalid assign #{@value[field.name].inspect}:#{@value[field.name].class}"
115
111
  end
116
112
  end
117
113
 
118
114
  # キーとして定義されていない値を全て除外した配列を生成して返す。
119
115
  # また、Modelを子に含んでいる場合、それを外部キーに変換する。
120
116
  def filtering
121
- datum = self.to_hash
122
- result = Hash.new
117
+ datum = to_h
118
+ result = {}
123
119
  self.class.fields.each do |field|
124
- begin
125
- result[field.name] = field.type.cast(datum[field.name])
126
- rescue Diva::InvalidTypeError => err
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
- case
137
- when fields.include?(:name)
130
+ if fields.include?(:name)
138
131
  name.gsub("\n", '')
139
- when fields.include?(:description)
132
+ elsif fields.include?(:description)
140
133
  description.gsub("\n", '')
141
134
  else
142
135
  to_s.gsub("\n", '')
@@ -146,18 +139,25 @@ 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.nil? || args.empty?
142
+ if value == nil || args.empty?
150
143
  value
151
144
  else
152
145
  value.dig(*args)
153
146
  end
154
147
  end
155
148
 
149
+ def deconstruct_keys(keys)
150
+ if keys
151
+ @value.slice(*keys) # 高速化のため、fetchを呼び出していない
152
+ else
153
+ @value
154
+ end
155
+ end
156
+
156
157
  private
158
+
157
159
  # URIがデフォルトで使うpath要素
158
160
  def path
159
161
  @path ||= "/#{SecureRandom.uuid}"
160
162
  end
161
-
162
163
  end
163
-
@@ -15,14 +15,14 @@ module Diva::ModelExtend
15
15
  # ==== Return
16
16
  # [String] URIスキーム
17
17
  def scheme
18
- @_scheme ||= self.to_s.split('::',2).first.gsub(/\W/,'').downcase.freeze
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 ||= self.to_s.split('::',2).last.split('::').reverse.join('.').gsub(/[^\w\.]/,'').downcase.freeze
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, "fields", 2018, 02
58
+ deprecate :keys, 'fields', 2018, 2
59
59
 
60
60
  def schema
61
61
  {
data/lib/diva/spec.rb CHANGED
@@ -5,5 +5,5 @@ Diva::ModelSpec = Struct.new(
5
5
  :name,
6
6
  :reply,
7
7
  :myself,
8
- :timeline,
8
+ :timeline
9
9
  )
data/lib/diva/type.rb CHANGED
@@ -27,7 +27,7 @@ Diva::Modelのサブクラスであれば、それを制約とすることがで
27
27
  配列の全ての要素が設定された制約を満たしていれば、配列制約が満たされたことになる。
28
28
 
29
29
  =end
30
- require "time"
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(#{to_s})"
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
@@ -153,7 +153,11 @@ module Diva::Type
153
153
  when Integer, Float
154
154
  Time.at(v)
155
155
  when String
156
- Time.iso8601(v)
156
+ begin
157
+ Time.iso8601(v)
158
+ rescue StandardError
159
+ Time.parse(v)
160
+ end
157
161
  else
158
162
  raise Diva::InvalidTypeError, "The value is not a `#{name}'."
159
163
  end
@@ -169,7 +173,7 @@ module Diva::Type
169
173
  end
170
174
  end
171
175
  NULL = AtomicType.new(:null, NilClass) do |v|
172
- if v.nil?
176
+ if v == nil
173
177
  v
174
178
  else
175
179
  raise Diva::InvalidTypeError, "The value is not a `#{name}'."
@@ -178,13 +182,14 @@ module Diva::Type
178
182
 
179
183
  class ModelType < MetaType
180
184
  attr_reader :model
185
+
181
186
  def initialize(model, *rest, &cast)
182
187
  super(:model, *rest)
183
188
  @model = model
184
189
  end
185
190
 
186
- def recommendation_point(v)
187
- v.is_a?(model) && 0
191
+ def recommendation_point(value)
192
+ value.is_a?(model) && 0
188
193
  end
189
194
 
190
195
  def cast(value)
@@ -199,7 +204,7 @@ module Diva::Type
199
204
  end
200
205
 
201
206
  def schema
202
- @schema ||= {type: uri}.freeze
207
+ @schema ||= { type: uri }.freeze
203
208
  end
204
209
 
205
210
  def to_s
@@ -219,7 +224,7 @@ module Diva::Type
219
224
  end
220
225
 
221
226
  def recommendation_point(values)
222
- values.is_a?(Enumerable) && values.all?{|v| @type.recommendation_point(v) } && 0
227
+ values.is_a?(Enumerable) && values.all? { |v| @type.recommendation_point(v) } && 0
223
228
  end
224
229
 
225
230
  def cast(value)
@@ -232,7 +237,7 @@ module Diva::Type
232
237
  end
233
238
 
234
239
  def to_s
235
- "Array of #{@type.to_s}"
240
+ "Array of #{@type}"
236
241
  end
237
242
 
238
243
  def schema
@@ -246,8 +251,8 @@ module Diva::Type
246
251
  super("union_#{@types.map(&:name).join('_')}")
247
252
  end
248
253
 
249
- def recommendation_point(v)
250
- @types.map{|t| t.recommendation_point(v) }.compact.min
254
+ def recommendation_point(value)
255
+ @types.map { |t| t.recommendation_point(value) }.compact.min
251
256
  end
252
257
 
253
258
  def cast(value)
@@ -267,18 +272,18 @@ module Diva::Type
267
272
  end
268
273
 
269
274
  def recommended_type_of(value)
270
- @types.map{|t|
275
+ available_types = @types.map { |t|
271
276
  [t, t.recommendation_point(value)]
272
- }.select{|_,p| p }.sort_by{|_,p| p }.each{|t,_|
273
- return t
274
- }
277
+ }.select { |_, p| p }
278
+ unless available_types.empty?
279
+ best, = available_types.min_by { |_, p| p }
280
+ return best
281
+ end
275
282
  @types.each do |type|
276
- begin
277
- type.cast(value)
278
- return type
279
- rescue Diva::InvalidTypeError
280
- # try next
281
- end
283
+ type.cast(value)
284
+ return type
285
+ rescue Diva::InvalidTypeError
286
+ # try next
282
287
  end
283
288
  raise Diva::InvalidTypeError, "The value is not #{self}"
284
289
  end
@@ -304,7 +309,7 @@ module Diva
304
309
  Diva::Type::URI
305
310
  when :null
306
311
  Diva::Type::NULL
307
- when ->x{x.class == Class && x.ancestors.include?(Diva::Model) }
312
+ when ->(x) { x.instance_of?(Class) && x.ancestors.include?(Diva::Model) }
308
313
  Diva::Type.model_of(type)
309
314
  when Array
310
315
  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 == self.to_s
49
+ other.to_s == to_s
50
50
  when Diva::URI
51
- if has_string? or other.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
@@ -60,6 +60,10 @@ class Diva::URI
60
60
  to_s.hash ^ self.class.hash
61
61
  end
62
62
 
63
+ def eql?(other)
64
+ self == other
65
+ end
66
+
63
67
  def has_string?
64
68
  !!@uri_string
65
69
  end
@@ -69,15 +73,15 @@ class Diva::URI
69
73
  end
70
74
 
71
75
  def to_s
72
- @uri_string ||= to_uri.to_s.freeze
76
+ @uri_string ||= to_uri.to_s.freeze # rubocop:disable Naming/MemoizedInstanceVariableName
73
77
  end
74
78
 
75
79
  def to_uri
76
- @uri ||= generate_uri.freeze
80
+ @uri ||= generate_uri.freeze # rubocop:disable Naming/MemoizedInstanceVariableName
77
81
  end
78
82
 
79
83
  def scheme
80
- if has_string? and !has_uri?
84
+ if has_string? && !has_uri?
81
85
  match = @uri_string.match(%r<\A(\w+):>)
82
86
  if match
83
87
  match[1]
data/lib/diva/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Diva
2
- VERSION = "1.0.3"
2
+ VERSION = '2.0.1'.freeze
3
3
  end
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,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: diva
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Toshiaki Asai
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-09-11 00:00:00.000000000 Z
11
+ date: 2021-12-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -31,25 +31,25 @@ dependencies:
31
31
  - !ruby/object:Gem::Version
32
32
  version: '3.0'
33
33
  - !ruby/object:Gem::Dependency
34
- name: rake
34
+ name: irb
35
35
  requirement: !ruby/object:Gem::Requirement
36
36
  requirements:
37
37
  - - ">="
38
38
  - !ruby/object:Gem::Version
39
- version: 13.0.1
39
+ version: 1.3.7
40
40
  - - "<"
41
41
  - !ruby/object:Gem::Version
42
- version: '14.0'
42
+ version: '2.0'
43
43
  type: :development
44
44
  prerelease: false
45
45
  version_requirements: !ruby/object:Gem::Requirement
46
46
  requirements:
47
47
  - - ">="
48
48
  - !ruby/object:Gem::Version
49
- version: 13.0.1
49
+ version: 1.3.7
50
50
  - - "<"
51
51
  - !ruby/object:Gem::Version
52
- version: '14.0'
52
+ version: '2.0'
53
53
  - !ruby/object:Gem::Dependency
54
54
  name: minitest
55
55
  requirement: !ruby/object:Gem::Requirement
@@ -71,25 +71,45 @@ dependencies:
71
71
  - !ruby/object:Gem::Version
72
72
  version: '5.15'
73
73
  - !ruby/object:Gem::Dependency
74
- name: irb
74
+ name: rake
75
75
  requirement: !ruby/object:Gem::Requirement
76
76
  requirements:
77
77
  - - ">="
78
78
  - !ruby/object:Gem::Version
79
- version: 1.3.7
79
+ version: 13.0.1
80
80
  - - "<"
81
81
  - !ruby/object:Gem::Version
82
- version: '2.0'
82
+ version: '14.0'
83
83
  type: :development
84
84
  prerelease: false
85
85
  version_requirements: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - ">="
88
88
  - !ruby/object:Gem::Version
89
- version: 1.3.7
89
+ version: 13.0.1
90
90
  - - "<"
91
91
  - !ruby/object:Gem::Version
92
- version: '2.0'
92
+ version: '14.0'
93
+ - !ruby/object:Gem::Dependency
94
+ name: rubocop
95
+ requirement: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: 1.23.0
100
+ - - "<"
101
+ - !ruby/object:Gem::Version
102
+ version: 1.24.0
103
+ type: :development
104
+ prerelease: false
105
+ version_requirements: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: 1.23.0
110
+ - - "<"
111
+ - !ruby/object:Gem::Version
112
+ version: 1.24.0
93
113
  - !ruby/object:Gem::Dependency
94
114
  name: simplecov
95
115
  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
@@ -161,7 +182,8 @@ files:
161
182
  homepage: https://github.com/toshia/diva
162
183
  licenses:
163
184
  - MIT
164
- metadata: {}
185
+ metadata:
186
+ rubygems_mfa_required: 'true'
165
187
  post_install_message:
166
188
  rdoc_options: []
167
189
  require_paths:
@@ -170,14 +192,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
170
192
  requirements:
171
193
  - - ">="
172
194
  - !ruby/object:Gem::Version
173
- version: '0'
195
+ version: 2.7.0
174
196
  required_rubygems_version: !ruby/object:Gem::Requirement
175
197
  requirements:
176
198
  - - ">="
177
199
  - !ruby/object:Gem::Version
178
200
  version: '0'
179
201
  requirements: []
180
- rubygems_version: 3.2.21
202
+ rubygems_version: 3.2.29
181
203
  signing_key:
182
204
  specification_version: 4
183
205
  summary: Implementation of expression for handling things.