Class: Family

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable, Validation
Defined in:
lib/family.rb,
lib/family/version.rb,
lib/family/singleton_class.rb

Overview

Note:

return self -> Array

  • #flatten is different

  • #flatten! is none

  • #product

Note:

removed from Array

  • #flatten! is none

Examples:

Simplify

list = Family.new Integer
list << 7    #=> 7
list << 1.0  #=> Exception
list << 1    #=> 1
list.inspect #=> "Integer ===: [7, 1]"

Not bound by "Type"

list = Family.new /\A\S+\z/
list << 'a b c' #=> Exception
list << 'abc'   #=> "abc"
list.inspect    #=> "/\A\S+\z/ ===: ["abc"]"

HighLayer definition

list = Family.define { AND(Float, 3..6) }
list << 4       #=> Exception
list << 2.0     #=> Exception
list << 4.0     #=> 4.0
list.inspect    #=> a Proc ===: [4.0]

Defined Under Namespace

Classes: DSL, MismatchedObject

Constant Summary collapse

VERSION =
'0.0.6'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(proof, comparison: :===, values: []) ⇒ Family

Returns a new instance of Family

Raises:


53
54
55
56
57
# File 'lib/family.rb', line 53

def initialize(proof, comparison: :===, values: [])
  @proof, @comparison, @values = proof, comparison, values.to_ary

  raise MismatchedObject unless valid?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object


173
174
175
176
177
178
# File 'lib/family.rb', line 173

def method_missing(name, *args, &block)
  return super unless @values.respond_to? name

  warn "WARN:#{__FILE__}:#{__LINE__}:unexpected method, not cheked any proofs"
  @values.__send__ name, *args, &block
end

Instance Attribute Details

#comparisonObject (readonly)

Returns the value of attribute comparison


51
52
53
# File 'lib/family.rb', line 51

def comparison
  @comparison
end

#proofObject (readonly)

Returns the value of attribute proof


51
52
53
# File 'lib/family.rb', line 51

def proof
  @proof
end

Class Method Details

.__new__(proof, comparison, values) ⇒ Object


13
14
15
# File 'lib/family/singleton_class.rb', line 13

def __new__(proof, comparison, values)
  new proof, comparison: comparison, values: values
end

.define(comparison: :===, values: [], &block) ⇒ Family


8
9
10
# File 'lib/family/singleton_class.rb', line 8

def define(comparison: :===, values: [], &block)
  __new__ DSL.new.instance_exec(&block), comparison, values
end

Instance Method Details

#*(times_or_delimiter) ⇒ Family


150
151
152
153
154
155
156
157
158
159
# File 'lib/family.rb', line 150

def *(times_or_delimiter)
  case times_or_delimiter
  when Integer
    self.class.__new__ @proof, @comparison, @values * times_or_delimiter
  when String
    join times_or_delimiter
  else
    raise ArgumentError
  end
end

#<<(value) ⇒ self Also known as: push

Raises:


88
89
90
91
92
93
# File 'lib/family.rb', line 88

def <<(value)
  raise MismatchedObject unless family? value

  @values << value
  self
end

#clearself


181
182
183
184
# File 'lib/family.rb', line 181

def clear
  @values.clear
  self
end

#compactFamily


187
188
189
# File 'lib/family.rb', line 187

def compact
  self.class.__new__ @proof, @comparison, @values.compact
end

#compact!self?


192
193
194
# File 'lib/family.rb', line 192

def compact!
  @values.compact! && self
end

#concat(list) ⇒ self

Raises:


107
108
109
110
111
112
# File 'lib/family.rb', line 107

def concat(list)
  raise MismatchedObject unless similar? list

  @values.concat list
  self
end

#delete_if(&block) ⇒ self


214
215
216
217
218
219
# File 'lib/family.rb', line 214

def delete_if(&block)
  return to_enum(__callee__) unless block_given?

  reject!(&block)
  self
end

#eql?(other) ⇒ Boolean


201
202
203
204
# File 'lib/family.rb', line 201

def eql?(other)
  other.kind_of?(::Family) &&
    (_comparison_values == other._comparison_values)
end

#family?(value) ⇒ Boolean


114
115
116
# File 'lib/family.rb', line 114

def family?(value)
  @proof.__send__ @comparison, value
end

#fill(*args, &block) ⇒ self

Raises:


236
237
238
239
240
241
242
# File 'lib/family.rb', line 236

def fill(*args, &block)
  filled = @values.dup.fill(*args, &block)
  raise MismatchedObject unless similar? filled

  @values = filled
  self
end

#freezeself


167
168
169
170
# File 'lib/family.rb', line 167

def freeze
  @values.freeze
  super
end

#hashNumber


197
198
199
# File 'lib/family.rb', line 197

def hash
  _comparison_values.hash
end

#inspectString


82
83
84
85
# File 'lib/family.rb', line 82

def inspect
  condition = @proof.kind_of?(Proc) ? 'a Proc' : @proof.inspect
  "#{condition} #{@comparison}: #{@values.inspect}"
end

#keep_if(&block) ⇒ Object


228
229
230
231
232
233
# File 'lib/family.rb', line 228

def keep_if(&block)
  return to_enum(__callee__) unless block_given?

  select!(&block)
  self
end

#map(&block) ⇒ Family Also known as: collect


128
129
130
131
132
# File 'lib/family.rb', line 128

def map(&block)
  return to_enum(__callee__){size} unless block_given?

  self.class.__new__ @proof, @comparison, @values.map(&block)
end

#map!(&block) ⇒ self Also known as: collect!

Raises:

  • (InvalidOperation)

137
138
139
140
141
142
143
144
145
# File 'lib/family.rb', line 137

def map!(&block)
  return to_enum(__callee__){size} unless block_given?

  mapped = @values.map(&block)
  raise InvalidOperation unless similar? mapped

  @values = mapped
  self
end

#reject!(&block) ⇒ self?


207
208
209
210
211
# File 'lib/family.rb', line 207

def reject!(&block)
  return to_enum(__callee__) unless block_given?

  @values.reject!(&block) && self
end

#replace(list) ⇒ self

Raises:


246
247
248
249
250
251
# File 'lib/family.rb', line 246

def replace(list)
  raise MismatchedObject unless similar? list

  @values = list.dup
  self
end

#reverseFamily


254
255
256
# File 'lib/family.rb', line 254

def reverse
  self.class.__new__ @proof, @comparison, @values.reverse
end

#reverse!self


259
260
261
262
# File 'lib/family.rb', line 259

def reverse!
  @values.reverse!
  self
end

#rotate(pos = 1) ⇒ Family


266
267
268
# File 'lib/family.rb', line 266

def rotate(pos=1)
  self.class.__new__ @proof, @comparison, @values.rotate(pos)
end

#rotate!(pos = 1) ⇒ self


272
273
274
275
# File 'lib/family.rb', line 272

def rotate!(pos=1)
  @values.rotate! pos
  self
end

#select!(&block) ⇒ self?


222
223
224
225
226
# File 'lib/family.rb', line 222

def select!(&block)
  return to_enum(__callee__) unless block_given?

  @values.select!(&block) && self
end

#shuffle(options = {}) ⇒ Family


278
279
280
# File 'lib/family.rb', line 278

def shuffle(options={})
  self.class.__new__ @proof, @comparison, @values.shuffle(options)
end

#shuffle!(options = {}) ⇒ self


283
284
285
286
# File 'lib/family.rb', line 283

def shuffle!(options={})
  @values.shuffle! options
  self
end

#similar?(list) ⇒ Boolean


119
120
121
# File 'lib/family.rb', line 119

def similar?(list)
  list.all?{|v|family? v}
end

#sort(&block) ⇒ Family


289
290
291
# File 'lib/family.rb', line 289

def sort(&block)
  self.class.__new__ @proof, @comparison, @values.sort(&block)
end

#sort!(&block) ⇒ self?


294
295
296
# File 'lib/family.rb', line 294

def sort!(&block)
  @values.sort!(&block) && self
end

#sort_by(&block) ⇒ Family


299
300
301
# File 'lib/family.rb', line 299

def sort_by(&block)
  self.class.__new__ @proof, @comparison, @values.sort_by(&block)
end

#sort_by!(&block) ⇒ self?


304
305
306
# File 'lib/family.rb', line 304

def sort_by!(&block)
  @values.sort_by!(&block) && self
end

#to_familyself


162
163
164
# File 'lib/family.rb', line 162

def to_family
  self
end

#uniq(&block) ⇒ Family


309
310
311
# File 'lib/family.rb', line 309

def uniq(&block)
  self.class.__new__ @proof, @comparison, @values.uniq(&block)
end

#uniq!(&block) ⇒ self?


314
315
316
# File 'lib/family.rb', line 314

def uniq!(&block)
  @values.uniq!(&block) && self
end

#unshift(value) ⇒ self

Raises:


98
99
100
101
102
103
# File 'lib/family.rb', line 98

def unshift(value)
  raise MismatchedObject unless family? value

  @values.unshift value
  self
end

#valid?Boolean


123
124
125
# File 'lib/family.rb', line 123

def valid?
  similar? @values
end

#valuesArray Also known as: to_ary, to_a


74
75
76
# File 'lib/family.rb', line 74

def values
  @values.dup
end

#values_at(*selectors) ⇒ Family


320
321
322
# File 'lib/family.rb', line 320

def values_at(*selectors)
  self.class.__new__ @proof, @comparison, @values.values_at(*selectors)
end