diva 0.1.1 → 0.2.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
  SHA1:
3
- metadata.gz: 2929c06c06248e820556866e691d09ca8b17701c
4
- data.tar.gz: df318101c0fc97e58705d17f8f996e9dac248216
3
+ metadata.gz: bc21e64173564102b7a3da104909d3a1742e5c58
4
+ data.tar.gz: 0c826bb45017d63bde247bc292e775e04b17b9fc
5
5
  SHA512:
6
- metadata.gz: ce219d41da367da84d92c36fdd312ede04a7f302bdaa9a48f1446546e006c9146ed753f3ff91fc2aed845381c36c626a8dc50bf49850315914fb7e15d7e2bb37
7
- data.tar.gz: 7274106f32830aaaacb8365b63ebd8f9de4e60b7afce1f0107173cd9fb347324fbfd2338f3ae43072d6b3fdbc616829257ec223c6cc1219936da02e11c956ce1
6
+ metadata.gz: 17bdfd00b4008192e9b859276a9684b5a3b475d6dde53f2e94b8845236373f95f66fdda5db0410fe29cb19acfb4bbffc3239809f423005ee4c104072a8d0bebd
7
+ data.tar.gz: 534176c4015edc96efc3714c004692084cd49e78b94e4be8541c755a7fa855823ccba4d88c39cf70c9f2f8a4d82e3629699c3c797abf18e7014d37c5341144af
@@ -0,0 +1,66 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ module Diva::Combinable
4
+ class Combinator
5
+ def initialize(a, b)
6
+ @a, @b = a, b
7
+ end
8
+
9
+ # コンストラクタに渡された二つの値が _behavior_ をサポートしていれば真を返す
10
+ # ==== Args
11
+ # [behavior] 想定する振る舞い(Symbol)
12
+ # ==== Return
13
+ # true or false
14
+ def =~(behavior)
15
+ @a.respond_to?(behavior) && @a.__send__(behavior, @b) || @b.respond_to?(behavior) && @b.__send__(behavior, @a)
16
+ end
17
+ alias_method :===, :=~
18
+
19
+ # このCombinatorが持っている値のすべての組み合わせのうち、適切に _behavior_
20
+ # をサポートしている組み合わせについて繰り返すEnumeratorを返す
21
+ # ==== Args
22
+ # [behavior] 想定する振る舞い(Symbol)
23
+ # ==== Return
24
+ # [レシーバ, 引数] のペアを列挙するEnumerator
25
+ def enum(behavior)
26
+ Enumerator.new do |yielder|
27
+ yielder << [@a, @b] if @a.respond_to?(behavior) && @a.__send__(behavior, @b)
28
+ yielder << [@b, @a] if @b.respond_to?(behavior) && @b.__send__(behavior, @a)
29
+ end
30
+ end
31
+
32
+ def method_missing(behavior, *rest)
33
+ if rest.empty?
34
+ return self =~ behavior
35
+ end
36
+ super
37
+ end
38
+ end
39
+
40
+ class EnumerableCombinator < Combinator
41
+ def =~(behavior)
42
+ @b.any?{|b| !enum(behavior).empty? }
43
+ end
44
+
45
+ def enum(behavior)
46
+ Enumerator.new do |yielder|
47
+ @b.each do |b|
48
+ (@a | b).enum(behavior).each(&yielder.method(:<<))
49
+ end
50
+ end
51
+ end
52
+ end
53
+
54
+ # _other_ との Diva::Conbinable::Combinator を生成する
55
+ # ==== Args
56
+ # [other] Diva::Modelか、Diva::Modelを列挙するEnumerable
57
+ # ==== Return
58
+ # Diva::Conbinable::Combinator
59
+ def |(other)
60
+ if other.is_a? Enumerable
61
+ EnumerableCombinator.new(self, other)
62
+ else
63
+ Combinator.new(self, other)
64
+ end
65
+ end
66
+ end
@@ -3,6 +3,7 @@
3
3
  いろんなリソースの基底クラス
4
4
  =end
5
5
 
6
+ require 'diva/combinator'
6
7
  require 'diva/model_extend'
7
8
  require 'diva/uri'
8
9
  require 'diva/spec'
@@ -11,6 +12,7 @@ require 'securerandom'
11
12
 
12
13
  class Diva::Model
13
14
  include Comparable
15
+ include Diva::Combinable
14
16
  extend Diva::ModelExtend
15
17
 
16
18
  def initialize(args)
@@ -144,8 +144,14 @@ module Diva::Type
144
144
  end
145
145
 
146
146
  def cast(value)
147
- raise Diva::InvalidTypeError, "The value is not a `#{model}'." unless value.is_a?(model)
148
- value
147
+ case value
148
+ when model
149
+ value
150
+ when Hash
151
+ model.new(value)
152
+ else
153
+ raise Diva::InvalidTypeError, "The value #{value.inspect} is not a `#{model}'."
154
+ end
149
155
  end
150
156
 
151
157
  def to_s
@@ -166,7 +172,7 @@ module Diva::Type
166
172
  end
167
173
 
168
174
  def to_s
169
- "Array of #{type.to_s}"
175
+ "Array of #{@type.to_s}"
170
176
  end
171
177
  end
172
178
 
@@ -185,7 +191,7 @@ module Diva::Type
185
191
  end
186
192
 
187
193
  def to_s
188
- "#{type.to_s}|nil"
194
+ "#{@type.to_s}|nil"
189
195
  end
190
196
  end
191
197
 
@@ -1,3 +1,3 @@
1
1
  module Diva
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
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: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Toshiaki Asai
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-02-28 00:00:00.000000000 Z
11
+ date: 2017-05-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -117,6 +117,7 @@ files:
117
117
  - bin/setup
118
118
  - diva.gemspec
119
119
  - lib/diva.rb
120
+ - lib/diva/combinator.rb
120
121
  - lib/diva/datasource.rb
121
122
  - lib/diva/error.rb
122
123
  - lib/diva/field.rb
@@ -147,7 +148,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
147
148
  version: '0'
148
149
  requirements: []
149
150
  rubyforge_project:
150
- rubygems_version: 2.6.8
151
+ rubygems_version: 2.6.11
151
152
  signing_key:
152
153
  specification_version: 4
153
154
  summary: Implementation of expression for handling things.