family 0.0.5 → 0.0.6

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: 96d117558c7196a93a330480c9023c3d98400de0
4
- data.tar.gz: 5e5114ede51928e7894471053d1553706b9b9ec5
3
+ metadata.gz: 16a3fb2e66cbbd6aeb0923f7bede14147f3a220b
4
+ data.tar.gz: 2093523459f63cba6bbc1af6dc0ba25701e20dc7
5
5
  SHA512:
6
- metadata.gz: 0c678c06d4fdd2530d9e54fd246fcb2586e997d37aba8b2f2b6bbaa0df59e5b086e8627e5c007af97f886f5f7396fa11b4928c8b54ea01e1c341d294a3d97369
7
- data.tar.gz: d9e9230a423e06fd95dfae063d2cb66bff58bfcfb6668db4d308497c04965edf5958b75a4d27cd8096fd37cb4b163d8b971d88c31992c8b31646a58f8ed40c65
6
+ metadata.gz: e71c0d3707e43d643bc97f6004b4f486cbacc4465c9a4857dc1b38de7b633473111cc16d02408bf067c9da7790e88ef52838a33e8455b4b3bc6bf9e1d1215f3d
7
+ data.tar.gz: e07bd2b3e321af15a17203abe1fc346aa845a6b1f732c1664e7af7a90ba9f14b3ee6d6992010c2b7637381392580b5291ba0ad3acb6c669b79e3626590292617
data/README.md CHANGED
@@ -31,7 +31,7 @@ list = Family.new Integer
31
31
  list << 7 #=> 7
32
32
  list << 1.0 #=> Exception
33
33
  list << 1 #=> 1
34
- list.inspect #=> "Family<Integer>:[7, 1]
34
+ list.inspect #=> "Integer ===: [7, 1]
35
35
  ```
36
36
 
37
37
  ### Not bound by "Type"
@@ -40,13 +40,17 @@ list.inspect #=> "Family<Integer>:[7, 1]
40
40
  list = Family.new /\A\S+\z/
41
41
  list << 'a b c' #=> Exception
42
42
  list << 'abc' #=> "abc"
43
- list.inspect #=> "Family</\A\S+\z/>:["abc"]"
43
+ list.inspect #=> "/\A\S+\z/ ===: ["abc"]"
44
44
  ```
45
45
 
46
46
  ### HighLayer Definition
47
47
 
48
48
  ```ruby
49
- list = Family.new{AND(Symbol, /\A\S+\z/)}
49
+ list = Family.define { AND(Float, 3..6) }
50
+ list << 4 #=> Exception
51
+ list << 2.0 #=> Exception
52
+ list << 4.0 #=> 4.0
53
+ list.inspect #=> a Proc ===: [4.0]
50
54
  ```
51
55
 
52
56
  Requirements
@@ -7,9 +7,11 @@ require_relative '../lib/family'
7
7
  list = Family.new Integer
8
8
  list << 7 #=> 7
9
9
  #~ p list << 1.0 #=> Exception
10
- p list << 1 #=> 1
10
+ list << 1 #=> 1
11
11
  p list.inspect #=> Integer[7, 1]
12
12
 
13
- list = Family.define{AND(String, /\d/)}
14
- #~ list << 'k'
15
- list << '8'
13
+ list = Family.define { AND(Float, 3..6) }
14
+ # list << 4 #=> Exception
15
+ # list << 2.0 #=> Exception
16
+ list << 4.0 #=> 4.0
17
+ p list.inspect #=> a Proc ===: [4.0]
@@ -15,14 +15,18 @@ require_relative 'family/singleton_class'
15
15
  # list << 7 #=> 7
16
16
  # list << 1.0 #=> Exception
17
17
  # list << 1 #=> 1
18
- # list.inspect #=> "Family<Integer>:[7, 1]"
18
+ # list.inspect #=> "Integer ===: [7, 1]"
19
19
  # @example Not bound by "Type"
20
20
  # list = Family.new /\A\S+\z/
21
21
  # list << 'a b c' #=> Exception
22
22
  # list << 'abc' #=> "abc"
23
- # list.inspect #=> "Family</\A\S+\z/>:["abc"]"
23
+ # list.inspect #=> "/\A\S+\z/ ===: ["abc"]"
24
24
  # @example HighLayer definition
25
- # list = Family.new{AND(Symbol, /\A\S+\z/)}
25
+ # list = Family.define { AND(Float, 3..6) }
26
+ # list << 4 #=> Exception
27
+ # list << 2.0 #=> Exception
28
+ # list << 4.0 #=> 4.0
29
+ # list.inspect #=> a Proc ===: [4.0]
26
30
  # @note return self -> Array
27
31
  # * #flatten is different
28
32
  # * #flatten! is none
@@ -46,7 +50,7 @@ class Family
46
50
 
47
51
  attr_reader :proof, :comparison
48
52
 
49
- def initialize(proof, comparison=:===, values=[])
53
+ def initialize(proof, comparison: :===, values: [])
50
54
  @proof, @comparison, @values = proof, comparison, values.to_ary
51
55
 
52
56
  raise MismatchedObject unless valid?
@@ -56,14 +60,14 @@ class Family
56
60
  :rassoc, :delete, :delete_at, :empty?, :fetch, :first, :last, :take, :tail,
57
61
  :flatten, :include?, :index, :to_s, :length, :size, :pack,
58
62
  :pop, :product, :reverse_each, :rindex, :sample,
59
- :slice, :slice!, :transpose, :zip, :bsearch
63
+ :slice, :slice!, :transpose, :zip, :to_h, :bsearch
60
64
 
61
65
  def_enums :@values, :each, :each_index, :cycle, :combination,
62
66
  :repeated_combination, :permutation, :repeated_permutation
63
67
 
64
68
  def_set_operator :&
65
69
  def_set_operator :+
66
- def_set_operator :- # todo
70
+ def_set_operator :-
67
71
  def_set_operator :|
68
72
 
69
73
  # @return [Array]
@@ -76,7 +80,8 @@ class Family
76
80
 
77
81
  # @return [String]
78
82
  def inspect
79
- "#{self.class}<#{@proof.inspect}>:#{@values.inspect}"
83
+ condition = @proof.kind_of?(Proc) ? 'a Proc' : @proof.inspect
84
+ "#{condition} #{@comparison}: #{@values.inspect}"
80
85
  end
81
86
 
82
87
  # @return [self]
@@ -123,7 +128,7 @@ class Family
123
128
  def map(&block)
124
129
  return to_enum(__callee__){size} unless block_given?
125
130
 
126
- self.class.new @proof, @comparison, @values.map(&block)
131
+ self.class.__new__ @proof, @comparison, @values.map(&block)
127
132
  end
128
133
 
129
134
  alias_method :collect, :map
@@ -145,7 +150,7 @@ class Family
145
150
  def *(times_or_delimiter)
146
151
  case times_or_delimiter
147
152
  when Integer
148
- self.class.new @proof, @comparison, @values * times_or_delimiter
153
+ self.class.__new__ @proof, @comparison, @values * times_or_delimiter
149
154
  when String
150
155
  join times_or_delimiter
151
156
  else
@@ -180,7 +185,7 @@ class Family
180
185
 
181
186
  # @return [Family]
182
187
  def compact
183
- self.class.new @proof, @comparison, @values.compact
188
+ self.class.__new__ @proof, @comparison, @values.compact
184
189
  end
185
190
 
186
191
  # @return [self, nil]
@@ -247,7 +252,7 @@ class Family
247
252
 
248
253
  # @return [Family]
249
254
  def reverse
250
- self.class.new @proof, @comparison, @values.reverse
255
+ self.class.__new__ @proof, @comparison, @values.reverse
251
256
  end
252
257
 
253
258
  # @return [self]
@@ -259,7 +264,7 @@ class Family
259
264
  # @param [Integer] pos
260
265
  # @return [Family]
261
266
  def rotate(pos=1)
262
- self.class.new @proof, @comparison, @values.rotate(pos)
267
+ self.class.__new__ @proof, @comparison, @values.rotate(pos)
263
268
  end
264
269
 
265
270
  # @param [Integer] pos
@@ -271,7 +276,7 @@ class Family
271
276
 
272
277
  # @return [Family]
273
278
  def shuffle(options={})
274
- self.class.new @proof, @comparison, @values.shuffle(options)
279
+ self.class.__new__ @proof, @comparison, @values.shuffle(options)
275
280
  end
276
281
 
277
282
  # @return [self]
@@ -282,7 +287,7 @@ class Family
282
287
 
283
288
  # @return [Family]
284
289
  def sort(&block)
285
- self.class.new @proof, @comparison, @values.sort(&block)
290
+ self.class.__new__ @proof, @comparison, @values.sort(&block)
286
291
  end
287
292
 
288
293
  # @return [self, nil]
@@ -292,7 +297,7 @@ class Family
292
297
 
293
298
  # @return [Family]
294
299
  def sort_by(&block)
295
- self.class.new @proof, @comparison, @values.sort_by(&block)
300
+ self.class.__new__ @proof, @comparison, @values.sort_by(&block)
296
301
  end
297
302
 
298
303
  # @return [self, nil]
@@ -302,7 +307,7 @@ class Family
302
307
 
303
308
  # @return [Family]
304
309
  def uniq(&block)
305
- self.class.new @proof, @comparison, @values.uniq(&block)
310
+ self.class.__new__ @proof, @comparison, @values.uniq(&block)
306
311
  end
307
312
 
308
313
  # @return [self, nil]
@@ -313,13 +318,7 @@ class Family
313
318
  # @param [Integer, Range<Integer>] selectors
314
319
  # @return [Family]
315
320
  def values_at(*selectors)
316
- self.class.new @proof, @comparison, @values.values_at(*selectors)
317
- end
318
-
319
- # @return [Hash]
320
- # This definition is adjusting the arity number with Array#to_h.
321
- def to_h
322
- @values.to_h
321
+ self.class.__new__ @proof, @comparison, @values.values_at(*selectors)
323
322
  end
324
323
 
325
324
  protected
@@ -4,10 +4,16 @@ class Family
4
4
 
5
5
  class << self
6
6
 
7
- def define(values=[], &block)
8
- new DSL.new.instance_exec(&block), :===, values
7
+ # @return [Family]
8
+ def define(comparison: :===, values: [], &block)
9
+ __new__ DSL.new.instance_exec(&block), comparison, values
9
10
  end
10
-
11
+
12
+ # @private
13
+ def __new__(proof, comparison, values)
14
+ new proof, comparison: comparison, values: values
15
+ end
16
+
11
17
  private
12
18
 
13
19
  def def_enum(reciever, name)
@@ -27,9 +33,7 @@ class Family
27
33
 
28
34
  define_method operator do |other|
29
35
  other = other.kind_of?(::Family) ? other._values : other.to_ary
30
- raise MismatchedObject unless similar? other
31
-
32
- self.class.new @proof, @comparison, @values.__send__(operator, other)
36
+ self.class.__new__ @proof, @comparison, @values.__send__(operator, other)
33
37
  end
34
38
 
35
39
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  class Family
4
4
 
5
- VERSION = '0.0.5'.freeze
5
+ VERSION = '0.0.6'.freeze
6
6
 
7
7
  end
@@ -19,7 +19,7 @@ The Family.new(Integer) do |list|
19
19
  is [7, 1]
20
20
 
21
21
  The list.inspect do
22
- is 'Family<Integer>:[7, 1]'
22
+ is 'Integer ===: [7, 1]'
23
23
  end
24
24
 
25
25
  The list.to_s do
@@ -55,18 +55,42 @@ The Family.new(Integer) do |list|
55
55
  is 2
56
56
  end
57
57
 
58
- RESCUE ArgumentError do
58
+ CATCH ArgumentError do
59
59
  is.to_h
60
60
  end
61
61
 
62
- RESCUE Exception do
63
- is << 'String'
62
+ CATCH Family::MismatchedObject do
63
+ list << 'String'
64
64
  end
65
65
 
66
66
  The (list & [7]) do
67
67
  is [7]
68
68
  end
69
69
 
70
+ The (list + [3]) do
71
+ is [7, 1, 3]
72
+ end
73
+
74
+ CATCH Family::MismatchedObject do
75
+ list + [3.0]
76
+ end
77
+
78
+ The (list | [3]) do
79
+ is [7, 1, 3]
80
+ end
81
+
82
+ CATCH Family::MismatchedObject do
83
+ list | [3.0]
84
+ end
85
+
86
+ The (list - [1]) do
87
+ is [7]
88
+ end
89
+
90
+ The (list - [1.0]) do
91
+ is [7, 1]
92
+ end
93
+
70
94
  The list.sort.bsearch{|x| x > 4} do
71
95
  is 7
72
96
  end
@@ -79,17 +103,33 @@ The Family.new(Integer) do |list|
79
103
  end
80
104
  end
81
105
 
82
- RESCUE Exception do
106
+ CATCH Family::MismatchedObject do
83
107
  list.unshift 3.0
84
108
  end
85
109
 
86
110
  truthy list.valid?
87
111
  end
88
112
 
113
+ The Family.new(1, comparison: :equal?) do |list|
114
+ list << 1
115
+
116
+ CATCH Family::MismatchedObject do
117
+ list << 2
118
+ end
119
+
120
+ CATCH Family::MismatchedObject do
121
+ list << 1.1
122
+ end
123
+
124
+ The list.inspect do
125
+ is '1 equal?: [1]'
126
+ end
127
+ end
128
+
89
129
  The Family.define{OR(Integer, nil)} do |list|
90
130
  list << nil << 1 << nil << 3 << 4 << nil << nil << 7 << nil
91
131
 
92
- RESCUE Exception do
132
+ CATCH Family::MismatchedObject do
93
133
  list << false
94
134
  end
95
135
 
@@ -104,7 +144,11 @@ end
104
144
  The Family.define{AND(Array, ->ary{ary.size == 2 })} do |list|
105
145
  list << [1, 2]
106
146
 
107
- RESCUE Exception do
147
+ The list.inspect do
148
+ is 'a Proc ===: [[1, 2]]'
149
+ end
150
+
151
+ CATCH Family::MismatchedObject do
108
152
  list << [1, 2, 3]
109
153
  end
110
154
 
@@ -113,10 +157,6 @@ The Family.define{AND(Array, ->ary{ary.size == 2 })} do |list|
113
157
  The list.to_h do
114
158
  is [[1, 2], [10, 20]].to_h
115
159
  end
116
-
117
- The Family.instance_method(:to_h).arity do
118
- is Array.instance_method(:to_h).arity
119
- end
120
160
  end
121
161
 
122
162
  The Family.define{AND(String, /\d/)} do |list|
@@ -124,11 +164,11 @@ The Family.define{AND(String, /\d/)} do |list|
124
164
  is ['8']
125
165
  truthy list.valid?
126
166
 
127
- RESCUE Exception do
167
+ CATCH Family::MismatchedObject do
128
168
  list << 8
129
169
  end
130
170
 
131
- RESCUE Exception do
171
+ CATCH Family::MismatchedObject do
132
172
  list << 'String'
133
173
  end
134
174
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: family
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kenichi Kamiya
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-13 00:00:00.000000000 Z
11
+ date: 2015-06-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: validation