Class: Civil::Set

Inherits:
Set
  • Object
show all
Defined in:
lib/civil/set.rb

Instance Method Summary collapse

Constructor Details

#initialize(enum = nil) ⇒ Set

Returns a new instance of Set.



3
4
5
6
7
# File 'lib/civil/set.rb', line 3

def initialize(enum = nil)
  enum = enum.map { |e| e.is_a?(::Hash) ? Civil::Hash.new.merge(e) : e } if enum

  super
end

Instance Method Details

#<<(o) ⇒ Object



15
16
17
# File 'lib/civil/set.rb', line 15

def <<(o)
  add(o)
end

#add(o) ⇒ Object



9
10
11
12
13
# File 'lib/civil/set.rb', line 9

def add(o)
  o and o.is_a?(::Hash) and o = Civil::Hash.new.merge(o)

  super
end

#pluck(key) ⇒ Object

Raises:

  • (ArgumentError)


27
28
29
30
31
32
33
34
35
# File 'lib/civil/set.rb', line 27

def pluck(key)
  raise ArgumentError, "key must be a symbol" unless key.is_a? Symbol

  self.inject(Civil::Set.new) { |set, item|
    item.is_a?(Civil::Hash) and set.add(item[key])

    set
  }
end

#where(attrs) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/civil/set.rb', line 19

def where(attrs)
  self.inject(Civil::Set.new) { |set, item|
    item.is_a?(Civil::Hash) and item =~ attrs and set.add(item)

    set
  }
end