Class: Factbook::Attributes

Inherits:
Object
  • Object
show all
Defined in:
lib/factbook/attributes.rb

Defined Under Namespace

Classes: Attribute

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribs) ⇒ Attributes

Returns a new instance of Attributes.



60
61
62
# File 'lib/factbook/attributes.rb', line 60

def initialize( attribs )
  @attribs = attribs
end

Class Method Details

.build_attribs(attribs, category, path, h) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/factbook/attributes.rb', line 31

def self.build_attribs( attribs, category, path, h )  
   
    ## assume it's an attribute definition hash
    ##   note: !! exclude special cases:
    ##      Capital           -- incl. name key itself
    ##      National anthem
   if h.has_key?( 'name' ) &&  ['Capital','National anthem'].include?( path[-1] ) == false
     a = Attribute.new
     a.name     = h['name']
     a.category = category
     a.path     = path

     puts "  adding attribute >#{a.name}< using #{a.category} / #{a.path.inspect}"       
     attribs << a
     
     ## note: make sure a modifable copy (of h) gets passed in
     h.delete( 'name' )
   end

   return  if h.empty?    ## empty hash; nothing (more) to do; return     
   
   ## continue walking (recursive)
   h.each do |k,v|
     new_path = path.dup << k   ## note: create a new array (copy) 
     build_attribs( attribs, category, new_path, v )
  end
end

.from_yaml(path) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/factbook/attributes.rb', line 13

def self.from_yaml( path )

  h = YAML.load_file( path )
  pp h

  attribs = []

  ## note: use a copy (e.g. h.dup) for now (hash gets changed by build_attribs!!)
  new_h = h.dup
  new_h.each do |k,v|
    category = k
    build_attribs( attribs, category, [], v )
  end

  self.new( attribs )
end

Instance Method Details

#eachObject



67
68
69
# File 'lib/factbook/attributes.rb', line 67

def each
  @attribs.each { |attrib| yield( attrib ) }
end

#sizeObject



65
# File 'lib/factbook/attributes.rb', line 65

def size() @attribs.size; end

#to_aObject



64
# File 'lib/factbook/attributes.rb', line 64

def to_a() @attribs; end