Class: Cukehead::FreemindReader

Inherits:
Object
  • Object
show all
Defined in:
lib/cukehead/freemind_reader.rb

Instance Method Summary collapse

Constructor Details

#initialize(filename = nil) ⇒ FreemindReader

Returns a new instance of FreemindReader.



8
9
10
11
# File 'lib/cukehead/freemind_reader.rb', line 8

def initialize(filename = nil)
  @mmdoc = nil
  read_file filename unless filename.nil?
end

Instance Method Details

#cucumber_features_nodeObject

Returns the first REXML::Element containing a TEXT attribute that matches ‘Cucumber features:’. Returns nil if no match is found.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/cukehead/freemind_reader.rb', line 29

def cucumber_features_node
  # XPath is case-sensitive so I'm using the translate function as
  # described in a blog post titled "Performing a Case In-sensitive
  # search in an XML Document" by Harish Ranganathan
  # http://geekswithblogs.net/ranganh/archive/2005/09/12/53520.aspx
  #
  # There may be functions in XPath version 2 that provide a better way
  # to do case-insensitive search but as of this writing REXML only
  # implements XPath 1.0.
  #
  # Because the search is for a specific string, only those characters
  # need translated to lower case.
  REXML::XPath.first(@mmdoc, '//node[translate(attribute::TEXT, "CUMBERFATS", "cumberfats")="cucumber features:"]')
end

#get_feature_nodesObject



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/cukehead/freemind_reader.rb', line 45

def get_feature_nodes
  node = cucumber_features_node
  feature_nodes = []
  node.each {|e|
    if e.is_a? REXML::Element
      text = e.attributes["TEXT"]
      feature_nodes << FeatureNode.new(e) if text =~ /^feature:.*/i
    end
  } unless node.nil?
  feature_nodes
end

#get_featuresObject



58
59
60
61
62
63
64
65
# File 'lib/cukehead/freemind_reader.rb', line 58

def get_features
  result = Hash.new
  feature_nodes = get_feature_nodes
  feature_nodes.each {|feature|
    result[feature.filename] = feature.to_text
  }
  result
end

#load_xml(xml) ⇒ Object

Loads the given XML string into a new REXML::Document.



21
22
23
# File 'lib/cukehead/freemind_reader.rb', line 21

def load_xml(xml)
  @mmdoc = REXML::Document.new(xml)
end

#read_file(filename) ⇒ Object

Loads the text from the specified file into a new REXML::Document



15
16
17
# File 'lib/cukehead/freemind_reader.rb', line 15

def read_file(filename)
  File.open(filename, "r") {|f| load_xml(f.read)}
end