diva 1.0.0 → 1.1.0

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: 65b2025d22ea687dfa98ca063ca511630ce75a4928e0e6be9581f7b6f2216ce2
4
- data.tar.gz: 6b514157b95438022b2b28ab72b0af436489e3f8edf8e8c12bca80e1298a1b69
3
+ metadata.gz: 927072ed062a2f9959488484142e4db0484c0084305426cf16ef80108ff22971
4
+ data.tar.gz: 5fc1ee93546cbe77541b212d0a3abf43c2ab4591564993889bea8307f667505b
5
5
  SHA512:
6
- metadata.gz: ea64c7762bc79c8c4eba0f9ac41fd3622a3af663c2a9f2057175695da9ba0f9226b4dbb71133e91c288dfc2799cdfae6d075fb84dab1905cc19ec2b7081b7045
7
- data.tar.gz: af41fb3db4368b08f71af3cc1886ae44553865d4797f6402278ec1a6a812e2095e94ec752785734592c09f757f58fcde3198e185efc49a494260692dd2afb45f
6
+ metadata.gz: f01a2df2a5477857590c8460e39507fcd07f83ea67daf5ebc4924f630439d43bb7344a64be3bde7cb300a314c013af9ca47fb484d91993a64aed46a7bc7f2289
7
+ data.tar.gz: 33aff1e9079cfd0dc68b472a7e491741a3afe1500c671e8157e78c331dc6e5f212bec4c69fe77520c1d2ad6bfb92204baf79d16ae1d52c521bd36881eba2883b
@@ -0,0 +1,67 @@
1
+ version: '2.1'
2
+
3
+ executors:
4
+ ruby:
5
+ parameters:
6
+ tag:
7
+ type: string
8
+ docker:
9
+ - image: circleci/ruby:<< parameters.tag >>
10
+
11
+ jobs:
12
+ build:
13
+ parameters:
14
+ ruby-version:
15
+ type: string
16
+ executor:
17
+ name: ruby
18
+ tag: << parameters.ruby-version >>
19
+ steps:
20
+ - checkout
21
+ - run:
22
+ name: Which bundler?
23
+ command: bundle -v
24
+ - run:
25
+ command: bundle install --path vendor/bundle
26
+ - run:
27
+ name: test
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
+
47
+ workflows:
48
+ build:
49
+ jobs:
50
+ - lint:
51
+ name: 'lint'
52
+ ruby-version: '3.0.2'
53
+ - build:
54
+ name: 'ruby-2.6'
55
+ ruby-version: '2.6.7'
56
+ requires:
57
+ - 'lint'
58
+ - build:
59
+ name: 'ruby-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/README.md CHANGED
@@ -8,6 +8,8 @@ By handling data as a subclass of Diva::Model as necessary,
8
8
  you can obtain a common interface and it is useful for cooperation
9
9
  among mikutter plugins.
10
10
 
11
+ [![toshia](https://circleci.com/gh/toshia/diva.svg?style=svg)](https://circleci.com/gh/toshia/diva)
12
+
11
13
  ## Installation
12
14
 
13
15
  Add this line to your application's Gemfile:
data/diva.gemspec CHANGED
@@ -12,17 +12,18 @@ 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"
18
19
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
20
  spec.require_paths = ["lib"]
20
21
 
21
- spec.add_development_dependency "bundler", '>= 1.16.1', '< 2.1'
22
- spec.add_development_dependency "rake", '>= 12.3.2', '< 13.0'
23
- spec.add_development_dependency "minitest", '>= 5.11.3', '< 5.12'
24
- spec.add_development_dependency "pry", '>= 0.12.2', '< 1.0'
25
- spec.add_development_dependency "simplecov", '>= 0.16.1', '< 1.0'
26
-
27
- spec.add_dependency "addressable", ">= 2.5.2", "< 2.7"
22
+ spec.add_development_dependency "bundler", '>= 1.16.1', '< 3.0'
23
+ spec.add_development_dependency "rake", '>= 13.0.1', '< 14.0'
24
+ spec.add_development_dependency "minitest", '>= 5.14.4', '< 5.15'
25
+ spec.add_development_dependency "irb", '>= 1.3.7', '< 2.0'
26
+ spec.add_development_dependency "simplecov", '>= 0.17.1', '< 1.0'
27
+ spec.add_development_dependency "rubocop", '>= 1.20.0', '< 1.21.0'
28
+ spec.add_dependency "addressable", ">= 2.5.2", "< 3.0"
28
29
  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
@@ -29,11 +29,3 @@ class Diva::FieldGenerator
29
29
  @model_klass.add_field(field_name, type: type, required: required)
30
30
  end
31
31
  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
@@ -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
@@ -80,13 +81,11 @@ class Diva::Model
80
81
  end
81
82
 
82
83
  def to_hash
83
- Hash[self.class.fields.map{|f| [f.name, fetch(f.name)] }]
84
+ self.class.fields.to_h { |f| [f.name, fetch(f.name)] }
84
85
  end
85
86
 
86
- def to_json(*rest, **kwrest)
87
- Hash[
88
- self.class.fields.map{|f| [f.name, f.dump_for_json(fetch(f.name))] }
89
- ].to_json(*rest, **kwrest)
87
+ def 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 => 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 = self.to_hash
122
- result = Hash.new
117
+ datum = to_hash
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,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.nil? || args.empty?
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
-
@@ -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
@@ -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.nil?
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(v)
187
- v.is_a?(model) && 0
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.to_s}"
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(v)
250
- @types.map{|t| t.recommendation_point(v) }.compact.min
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{|_,p| p }.sort_by{|_,p| p }.each{|t,_|
273
- return t
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
- begin
277
- type.cast(value)
278
- return type
279
- rescue Diva::InvalidTypeError
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.class == Class && x.ancestors.include?(Diva::Model) }
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 == 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
@@ -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? and !has_uri?
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
@@ -1,3 +1,3 @@
1
1
  module Diva
2
- VERSION = "1.0.0"
2
+ VERSION = '1.1.0'.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.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Toshiaki Asai
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-06-01 00:00:00.000000000 Z
11
+ date: 2021-09-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -19,7 +19,7 @@ dependencies:
19
19
  version: 1.16.1
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: '2.1'
22
+ version: '3.0'
23
23
  type: :development
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
@@ -29,74 +29,74 @@ dependencies:
29
29
  version: 1.16.1
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: '2.1'
32
+ version: '3.0'
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: rake
35
35
  requirement: !ruby/object:Gem::Requirement
36
36
  requirements:
37
37
  - - ">="
38
38
  - !ruby/object:Gem::Version
39
- version: 12.3.2
39
+ version: 13.0.1
40
40
  - - "<"
41
41
  - !ruby/object:Gem::Version
42
- version: '13.0'
42
+ version: '14.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: 12.3.2
49
+ version: 13.0.1
50
50
  - - "<"
51
51
  - !ruby/object:Gem::Version
52
- version: '13.0'
52
+ version: '14.0'
53
53
  - !ruby/object:Gem::Dependency
54
54
  name: minitest
55
55
  requirement: !ruby/object:Gem::Requirement
56
56
  requirements:
57
57
  - - ">="
58
58
  - !ruby/object:Gem::Version
59
- version: 5.11.3
59
+ version: 5.14.4
60
60
  - - "<"
61
61
  - !ruby/object:Gem::Version
62
- version: '5.12'
62
+ version: '5.15'
63
63
  type: :development
64
64
  prerelease: false
65
65
  version_requirements: !ruby/object:Gem::Requirement
66
66
  requirements:
67
67
  - - ">="
68
68
  - !ruby/object:Gem::Version
69
- version: 5.11.3
69
+ version: 5.14.4
70
70
  - - "<"
71
71
  - !ruby/object:Gem::Version
72
- version: '5.12'
72
+ version: '5.15'
73
73
  - !ruby/object:Gem::Dependency
74
- name: pry
74
+ name: irb
75
75
  requirement: !ruby/object:Gem::Requirement
76
76
  requirements:
77
77
  - - ">="
78
78
  - !ruby/object:Gem::Version
79
- version: 0.12.2
79
+ version: 1.3.7
80
80
  - - "<"
81
81
  - !ruby/object:Gem::Version
82
- version: '1.0'
82
+ version: '2.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: 0.12.2
89
+ version: 1.3.7
90
90
  - - "<"
91
91
  - !ruby/object:Gem::Version
92
- version: '1.0'
92
+ version: '2.0'
93
93
  - !ruby/object:Gem::Dependency
94
94
  name: simplecov
95
95
  requirement: !ruby/object:Gem::Requirement
96
96
  requirements:
97
97
  - - ">="
98
98
  - !ruby/object:Gem::Version
99
- version: 0.16.1
99
+ version: 0.17.1
100
100
  - - "<"
101
101
  - !ruby/object:Gem::Version
102
102
  version: '1.0'
@@ -106,10 +106,30 @@ dependencies:
106
106
  requirements:
107
107
  - - ">="
108
108
  - !ruby/object:Gem::Version
109
- version: 0.16.1
109
+ version: 0.17.1
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
@@ -119,7 +139,7 @@ dependencies:
119
139
  version: 2.5.2
120
140
  - - "<"
121
141
  - !ruby/object:Gem::Version
122
- version: '2.7'
142
+ version: '3.0'
123
143
  type: :runtime
124
144
  prerelease: false
125
145
  version_requirements: !ruby/object:Gem::Requirement
@@ -129,15 +149,17 @@ dependencies:
129
149
  version: 2.5.2
130
150
  - - "<"
131
151
  - !ruby/object:Gem::Version
132
- version: '2.7'
133
- description:
152
+ version: '3.0'
153
+ description:
134
154
  email:
135
155
  - toshi.alternative@gmail.com
136
156
  executables: []
137
157
  extensions: []
138
158
  extra_rdoc_files: []
139
159
  files:
160
+ - ".circleci/config.yml"
140
161
  - ".gitignore"
162
+ - ".rubocop.yml"
141
163
  - ".travis.yml"
142
164
  - Gemfile
143
165
  - LICENSE.txt
@@ -161,7 +183,7 @@ homepage: https://github.com/toshia/diva
161
183
  licenses:
162
184
  - MIT
163
185
  metadata: {}
164
- post_install_message:
186
+ post_install_message:
165
187
  rdoc_options: []
166
188
  require_paths:
167
189
  - lib
@@ -169,15 +191,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
169
191
  requirements:
170
192
  - - ">="
171
193
  - !ruby/object:Gem::Version
172
- version: '0'
194
+ version: 2.6.0
173
195
  required_rubygems_version: !ruby/object:Gem::Requirement
174
196
  requirements:
175
197
  - - ">="
176
198
  - !ruby/object:Gem::Version
177
199
  version: '0'
178
200
  requirements: []
179
- rubygems_version: 3.0.3
180
- signing_key:
201
+ rubygems_version: 3.2.21
202
+ signing_key:
181
203
  specification_version: 4
182
204
  summary: Implementation of expression for handling things.
183
205
  test_files: []