Class: Wolfram::Pod

Inherits:
Object
  • Object
show all
Extended by:
Util
Includes:
Enumerable, XmlContainer
Defined in:
lib/wolfram/pod.rb

Defined Under Namespace

Classes: State, Subpod

Instance Attribute Summary collapse

Attributes included from XmlContainer

#xml

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

delegate, module_get, to_param, to_query

Methods included from XmlContainer

#respond_to?

Constructor Details

#initialize(xml, options = {}) ⇒ Pod

Returns a new instance of Pod.



14
15
16
17
18
19
20
21
# File 'lib/wolfram/pod.rb', line 14

def initialize(xml, options = {})
  @query = options[:query]
  @xml = Nokogiri::XML(xml.to_s).search('pod').first
  @subpods = Subpod.collection(@xml.search('subpod'), options)
  @states = State.collection(@xml.search('states'), options)
  @xml or raise MissingNodeError, "<pod> node missing from xml: #{xml[0..20]}..."
  types.each {|type| extend type}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Wolfram::XmlContainer

Instance Attribute Details

#queryObject (readonly)

Returns the value of attribute query.



13
14
15
# File 'lib/wolfram/pod.rb', line 13

def query
  @query
end

#statesObject (readonly)

Returns the value of attribute states.



13
14
15
# File 'lib/wolfram/pod.rb', line 13

def states
  @states
end

#subpodsObject (readonly)

Returns the value of attribute subpods.



13
14
15
# File 'lib/wolfram/pod.rb', line 13

def subpods
  @subpods
end

Class Method Details

.collection(xml, options = {}) ⇒ Object



9
10
11
# File 'lib/wolfram/pod.rb', line 9

def self.collection(xml, options = {})
  Nokogiri::XML(xml.to_s).search('pod').map {|p_xml| new(p_xml, options)}
end

Instance Method Details

#imgObject



39
40
41
# File 'lib/wolfram/pod.rb', line 39

def img
  (e = subpods.detect(&:plaintext)) && e.img
end

#inspectObject



27
28
29
# File 'lib/wolfram/pod.rb', line 27

def inspect
  "#<#{scanner}: #{to_s}>"
end

#markupObject



43
44
45
# File 'lib/wolfram/pod.rb', line 43

def markup
  @markup ||= (xml.search('markup').text || '')
end

#plaintextObject



35
36
37
# File 'lib/wolfram/pod.rb', line 35

def plaintext
  (e = subpods.detect(&:plaintext)) && e.plaintext
end

#structured?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/wolfram/pod.rb', line 47

def structured?
  subpods.any?
end

#to_sObject



23
24
25
# File 'lib/wolfram/pod.rb', line 23

def to_s
  "#{title}: #{structured? ? plaintext : "'#{markup[0..20]}...'"} #{states.join(", ") if states.any?}"
end

#typesObject



31
32
33
# File 'lib/wolfram/pod.rb', line 31

def types
  @types ||= scanner.split(',').map {|type| Util.module_get(Result, type)}
end