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(entry_xml, parent) ⇒ GreenButtonEntry

Returns a new instance of GreenButtonEntry.



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

def initialize(entry_xml, parent)
  if !entry_xml.nil?
    @entry_xml = entry_xml
    self.related_hrefs = []
    self.other_related = []
    pre_rule_assignment(parent)
    assign_rules
    find_related_entries
  end
end

Instance Attribute Details

#hrefObject

Returns the value of attribute href.



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

def href
  @href
end

#idObject

Returns the value of attribute id.



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

def id
  @id
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

#parent_hrefObject

Returns the value of attribute parent_href.



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

def parent_href
  @parent_href
end

#publishedObject

Returns the value of attribute published.



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

def published
  @published
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

#titleObject

Returns the value of attribute title.



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

def title
  @title
end

#updatedObject

Returns the value of attribute updated.



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

def updated
  @updated
end

Instance Method Details



86
87
88
# File 'lib/greenbutton/gb_classes.rb', line 86

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

#additional_rulesObject



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

def additional_rules
  []
end

#assign_rulesObject



46
47
48
49
50
51
52
53
54
# File 'lib/greenbutton/gb_classes.rb', line 46

def assign_rules
  (RULES + additional_rules).each do |rule|
    create_attr(rule.attr_name)
    rule_xml = @entry_xml.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)
  end
end

#docObject



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

def doc
  self.usage_point.doc
end

#find_by_href(href) ⇒ Object



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

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


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/greenbutton/gb_classes.rb', line 56

def find_related_entries
  self.related_hrefs = []
  @entry_xml.xpath("./link[@rel='related']/@href").each do |href|
    if /\/\d+$/i.match(href.text)
      related_entry = find_by_href(href.text)
      if related_entry
        parse_related_entry(related_entry)
        self.related_hrefs << href.text
      else
        warn 'no link found for href: ' + href.text
      end
    else  
      doc.xpath("//link[@rel='up' and @href='#{href.text}']").each do |link|
        self.related_hrefs << link.attr('href')
        parse_related_entry(link.parent)
      end
    end
  end
end

#parse_related_entry(entry_xml) ⇒ Object



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

def parse_related_entry(entry_xml)
  name = get_related_name(entry_xml)
  classParser = GreenButtonClasses.const_get(name)
  if !classParser.nil?
    self.add_related(Helper.underscore(name), classParser.new(entry_xml, self))
  else
    other_related.push(xml)
  end
end

#pre_rule_assignment(parent) ⇒ Object



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

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