Class: Fattr::List

Inherits:
Array show all
Defined in:
lib/fattr.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ List

Returns a new instance of List.



12
13
14
# File 'lib/fattr.rb', line 12

def initialize(*args, &block)
  super(*args, &block)
end

Instance Attribute Details

#objectObject

Returns the value of attribute object.



10
11
12
# File 'lib/fattr.rb', line 10

def object
  @object
end

Instance Method Details

#<<(element) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/fattr.rb', line 16

def << element
  super
  self
ensure
  uniq!
  index!
end

#for(object) ⇒ Object



49
50
51
52
# File 'lib/fattr.rb', line 49

def for(object)
  @object = object
  self
end

#include?(element) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
# File 'lib/fattr.rb', line 29

def include?(element)
  @index ||= Hash.new
  @index[element.to_s] ? true : false
end

#index!Object



24
25
26
27
# File 'lib/fattr.rb', line 24

def index!
  @index ||= Hash.new
  each{|element| @index[element.to_s] = true}
end

#initializersObject



34
35
36
# File 'lib/fattr.rb', line 34

def initializers
  @initializers ||= Hash.new
end

#to_hObject



45
46
47
# File 'lib/fattr.rb', line 45

def to_h
  to_hash
end

#to_hashObject



38
39
40
41
42
43
# File 'lib/fattr.rb', line 38

def to_hash
  if @object
    list = @object.class.fattrs + @object.fattrs
    list.inject(Hash.new){|hash, fattr| hash.update(fattr => @object.send(fattr))}
  end
end