Class: GreenButtonClasses::GreenButtonEntry

Inherits:
Object
  • Object
show all
Defined in:
lib/greenbutton/gb_classes.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(href_from_parent, entry_node, parent) ⇒ GreenButtonEntry



19
20
21
22
23
24
25
26
27
# File 'lib/greenbutton/gb_classes.rb', line 19

def initialize(href_from_parent, entry_node, parent)
  self.href_from_parent = href_from_parent
  @entry_node = entry_node
  self.related_hrefs = []
  self.other_related = []
  create_attributes
  pre_rule_assignment(parent)
  get_related_links
end

Instance Attribute Details

#href_from_parentObject

Returns the value of attribute href_from_parent



17
18
19
# File 'lib/greenbutton/gb_classes.rb', line 17

def href_from_parent
  @href_from_parent
end

Returns the value of attribute other_related



17
18
19
# File 'lib/greenbutton/gb_classes.rb', line 17

def other_related
  @other_related
end

Returns the value of attribute related_hrefs



17
18
19
# File 'lib/greenbutton/gb_classes.rb', line 17

def related_hrefs
  @related_hrefs
end

Instance Method Details

#add_related_entry(type, parser) ⇒ Object



97
98
99
# File 'lib/greenbutton/gb_classes.rb', line 97

def add_related_entry(type, parser)
  warn self.class + ' does not have any recognized relations.'
end


86
87
88
89
90
91
92
93
94
95
# File 'lib/greenbutton/gb_classes.rb', line 86

def add_related_link(href, entry_node = nil)
  self.related_hrefs << href
  name = get_related_name(href)
  classParser = GreenButtonClasses.const_get(name)
  if !classParser.nil?
    self.add_related_entry(Helper.underscore(name), classParser.new(href, entry_node, self))
  else
    other_related.push([href, entry_node])
  end      
end

#additional_rulesObject



46
47
48
# File 'lib/greenbutton/gb_classes.rb', line 46

def additional_rules
  {}
end

#all_rulesObject



50
51
52
# File 'lib/greenbutton/gb_classes.rb', line 50

def all_rules
  RULES.merge(additional_rules)
end

#assign_rule(attr_name) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/greenbutton/gb_classes.rb', line 62

def assign_rule(attr_name)
  rule = all_rules[attr_name]
  rule_xml = entry_node.xpath(rule.xpath)
  value = rule_xml.empty? ? nil : rule_xml.text
  translated_value = value.nil? ? nil : Helper.translate(rule.type, value)
  self.send(rule.attr_name.to_s+"=", translated_value)
  translated_value
end

#create_attributesObject



29
30
31
32
33
# File 'lib/greenbutton/gb_classes.rb', line 29

def create_attributes
  all_rules.each_pair do |attr, rule|
    create_attr(attr)
  end
end

#docObject



54
55
56
# File 'lib/greenbutton/gb_classes.rb', line 54

def doc
  self.usage_point.doc
end

#entry_nodeObject



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

def entry_node
  if @entry_node.nil?
    @entry_node = find_by_href(self.href_from_parent).remove
  end
  @entry_node
end

#find_by_href(href) ⇒ Object



58
59
60
# File 'lib/greenbutton/gb_classes.rb', line 58

def find_by_href(href)
  doc.xpath("//link[@rel='self' and @href='#{href}']/..")[0]
end


71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/greenbutton/gb_classes.rb', line 71

def get_related_links
  self.related_hrefs = []
  entry_node.xpath("./link[@rel='related']/@href").each do |href|
    if /\/\d+$/i.match(href.text)
      add_related_link(href.text)
    else  
      many_related = doc.xpath("//link[@rel='up' and @href='#{href.text}']")
      many_related = alt_links(href.text) if many_related.length == 0
      many_related.each do |link|
        add_related_link(link.attr('href'), link.parent.remove)
      end
    end
  end
end

#pre_rule_assignment(parent) ⇒ Object



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

def pre_rule_assignment(parent)
  warn self.class + 'failed to implement pre_rule_assignment'
end