Class: Mathemagical::Element

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

Direct Known Subclasses

Break, Fenced, Frac, Identifier, Math, None, Number, Operator, Over, Root, Row, Space, Sqrt, SubSup, Table, Td, Text, Tr, Under

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Element

Returns a new instance of Element.



6
7
8
9
10
# File 'lib/mathemagical/element.rb', line 6

def initialize(name, options = {})
	@name = name
	@attributes = options
	@children = []
end

Instance Attribute Details

#attributes ⇒ Object (readonly)

Returns the value of attribute attributes.



3
4
5
# File 'lib/mathemagical/element.rb', line 3

def attributes
  @attributes
end

#children ⇒ Object (readonly)

Returns the value of attribute children.



3
4
5
# File 'lib/mathemagical/element.rb', line 3

def children
  @children
end

#display_style ⇒ Object (readonly)

Returns the value of attribute display_style.



3
4
5
# File 'lib/mathemagical/element.rb', line 3

def display_style
  @display_style
end

#name ⇒ Object

Returns the value of attribute name.



4
5
6
# File 'lib/mathemagical/element.rb', line 4

def name
  @name
end

Instance Method Details

#<<(*obj) ⇒ Object



25
26
27
28
29
# File 'lib/mathemagical/element.rb', line 25

def <<(*obj)
	@children += obj.flatten.map {|o| o.is_a?(String) ? Mathemagical.encode(o) : o }

	self
end

#[](key) ⇒ Object



21
22
23
# File 'lib/mathemagical/element.rb', line 21

def [](key)
	@attributes[key.to_sym]
end

#[]=(key, value) ⇒ Object



17
18
19
# File 'lib/mathemagical/element.rb', line 17

def []=(key, value)
	@attributes[key.to_sym] = value
end

#as_display_style ⇒ Object



12
13
14
15
# File 'lib/mathemagical/element.rb', line 12

def as_display_style
	@display_style = true
	self
end

#build_attributes_string ⇒ Object



49
50
51
52
53
54
55
# File 'lib/mathemagical/element.rb', line 49

def build_attributes_string
	return if @attributes.empty?

	" " + @attributes.map do |key, value|
		"#{key}='#{value}'"
	end.join(" ")
end

#child_ml ⇒ Object



45
46
47
# File 'lib/mathemagical/element.rb', line 45

def child_ml
	@children.map(&:to_s).join("\n")
end

#pop ⇒ Object



57
58
59
# File 'lib/mathemagical/element.rb', line 57

def pop
	@children.pop
end

#self_closing_xml ⇒ Object



35
36
37
# File 'lib/mathemagical/element.rb', line 35

def self_closing_xml
	"<#{@name}#{build_attributes_string} />"
end

#to_s ⇒ Object



31
32
33
# File 'lib/mathemagical/element.rb', line 31

def to_s
	children.empty? ? self_closing_xml : xml
end

#xml ⇒ Object



39
40
41
42
43
# File 'lib/mathemagical/element.rb', line 39

def xml
	"<#{@name}#{build_attributes_string}>
		#{child_ml}
	</#{@name}>".gsub(/[\n\t]/, '')
end