Class: Base::Element

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

Overview

The daddy of all classes in reparcs, all elements subclass this either directly or indirectly.

Direct Known Subclasses

ContainerElement

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string_name, allowed_attrs = [], attrs = {}) ⇒ Element

Create a new Element, takes the string_name of the element e.g “div” an optional array of allowed attribute names, and an optional hash of attributes as parameters.



14
15
16
17
18
19
20
21
22
23
# File 'lib/reparcs/base.rb', line 14

def initialize(string_name, allowed_attrs=[], attrs={})
  @string_name = string_name
  @attributes = {}
  @allowed_attributes = allowed_attrs
  attrs.each do |key, val|
    self.set_attribute(key, val)
  end
  @start_element = ""
  @end_element = ""
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



73
74
75
# File 'lib/reparcs/base.rb', line 73

def attributes
  @attributes
end

#end_elementObject (readonly)

Returns the value of attribute end_element.



73
74
75
# File 'lib/reparcs/base.rb', line 73

def end_element
  @end_element
end

#start_elementObject (readonly)

Returns the value of attribute start_element.



73
74
75
# File 'lib/reparcs/base.rb', line 73

def start_element
  @start_element
end

#string_nameObject (readonly)

Returns the value of attribute string_name.



73
74
75
# File 'lib/reparcs/base.rb', line 73

def string_name
  @string_name
end

Instance Method Details

#attribute_set?(attr_name) ⇒ Boolean

Checks to see if an attribute has been set for this element takes the attribute name as parameter.

Returns:



58
59
60
61
62
63
64
# File 'lib/reparcs/base.rb', line 58

def attribute_set?(attr_name)
  if @attributes.include?(attr_name)
    return true
  else
    return false
  end
end

#get_attribute(attr_name) ⇒ Object

Returns the value of an attribute



66
67
68
69
70
71
72
# File 'lib/reparcs/base.rb', line 66

def get_attribute(attr_name)
  if attribute_set?(attr_name)
    return @attributes[attr_name]
  else
    return nil
  end
end

#is_attribute_allowed?(attr_name) ⇒ Boolean

Checks to see if a certain attribute is allowed for this element, takes the atribute name as parameter.

Returns:



38
39
40
41
42
43
44
45
46
47
# File 'lib/reparcs/base.rb', line 38

def is_attribute_allowed?(attr_name)
  allowed = false
  @allowed_attributes.each do |a|
    if a == attr_name.to_s
      allowed = true
      break
    end
  end
  return allowed
end

#set_attribute(attr_name, value) ⇒ Object

Sets a attribute for this element(if it is allowed)



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

def set_attribute(attr_name, value)
  if is_attribute_allowed?(attr_name)
    @attributes["#{attr_name}"] = value
  else
    raise "The '#{attr_name}' attribute is not allowed for the '#{@string_name}' element."
  end
end

#to_htmlObject

Returns the html representation of the Element



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/reparcs/base.rb', line 25

def to_html
  html = "#{@start_element}"
  @attributes.each do |key, attri|
    html << " #{key}=\"#{attri}\""
  end
  if @end_element != " />"
    html << ">"
  end
  html << @end_element
  return html
end