Class: KjDotXml

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title = nil, debug: false) ⇒ KjDotXml

Returns a new instance of KjDotXml.



86
87
88
89
90
91
92
93
# File 'lib/kjdotxml.rb', line 86

def initialize(title=nil, debug: false)
  
  @debug = debug
  
  book(title) if title
  @booklist = BOOKS
  
end

Instance Attribute Details

#booklistObject (readonly)

Returns the value of attribute booklist.



84
85
86
# File 'lib/kjdotxml.rb', line 84

def booklist
  @booklist
end

Instance Method Details

#book(s) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/kjdotxml.rb', line 95

def book(s)
      
  filename = s.downcase.gsub(/\s/,'-') + '.xml'
  filepath = File.join(File.dirname(__FILE__), '..', 'xml', filename)
  
  if File.exists? filepath then
    
    contents = File.read(filepath)
    @doc = Rexle.new(contents)
    
  else
    raise KjDotXmlError, 'book not found'
  end
  
end

#books(ref = nil) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/kjdotxml.rb', line 111

def books(ref=nil)           
  
  return @booklist.map {|x| books(x) } unless ref

  index = ref.to_s[/^\d+$/] ? (ref.to_i - 1) : find_book(ref.downcase)
  puts 'index: ' + index.inspect if @debug
  
  title = @booklist[index]


end

#to_docObject



151
152
153
# File 'lib/kjdotxml.rb', line 151

def to_doc()
  @doc
end

#verses(s) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/kjdotxml.rb', line 123

def verses(s)

  r = s.match(/(?<book>\w+)\s+(?<chapter>\d+)[,\:]?\s*(?<verses>.*)/)
  verses2 = r[:verses] =~ /(?:\.\.|,|-)/ ? Range.new(*r[:verses]\
                      .split(/(?:\.\.|,|-)/).map(&:to_i)).to_a : r[:verses]
  puts 'r[:book]: ' + r[:book].inspect if @debug
  
  doc = book(books(r[:book]))
  
  puts 'verses2: ' + verses2.inspect if @debug
  
  if verses2.is_a? String then
    
    [
      doc.root.element("chapter[@no='#{r[:chapter]}']/verse" + 
                       "[@no='#{verses2}']")
    ]
    
  elsif verses2.is_a? Array
    
    verses2.map do |verse|
      doc.root.element("chapter[@no='#{r[:chapter]}']/verse[@no='#{verse}']")
    end
    
  end
end