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.



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

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

Instance Attribute Details

#objectObject

Returns the value of attribute object.



7
8
9
# File 'lib/fattr.rb', line 7

def object
  @object
end

Instance Method Details

#<<(element) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/fattr.rb', line 13

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

#for(object) ⇒ Object



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

def for(object)
  @object = object
  self
end

#include?(element) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
# File 'lib/fattr.rb', line 26

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

#index!Object



21
22
23
24
# File 'lib/fattr.rb', line 21

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

#initializersObject



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

def initializers
  @initializers ||= Hash.new
end

#to_hObject



42
43
44
# File 'lib/fattr.rb', line 42

def to_h
  to_hash
end

#to_hashObject



35
36
37
38
39
40
# File 'lib/fattr.rb', line 35

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