Class: Base::Element
- Inherits:
-
Object
- Object
- Base::Element
- 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
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#end_element ⇒ Object
readonly
Returns the value of attribute end_element.
-
#start_element ⇒ Object
readonly
Returns the value of attribute start_element.
-
#string_name ⇒ Object
readonly
Returns the value of attribute string_name.
Instance Method Summary collapse
-
#attribute_set?(attr_name) ⇒ Boolean
Checks to see if an attribute has been set for this element takes the attribute name as parameter.
-
#get_attribute(attr_name) ⇒ Object
Returns the value of an attribute.
-
#initialize(string_name, allowed_attrs = [], attrs = {}) ⇒ Element
constructor
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.
-
#is_attribute_allowed?(attr_name) ⇒ Boolean
Checks to see if a certain attribute is allowed for this element, takes the atribute name as parameter.
-
#set_attribute(attr_name, value) ⇒ Object
Sets a attribute for this element(if it is allowed).
-
#to_html ⇒ Object
Returns the html representation of the Element.
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
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
73 74 75 |
# File 'lib/reparcs/base.rb', line 73 def attributes @attributes end |
#end_element ⇒ Object (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_element ⇒ Object (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_name ⇒ Object (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.
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.
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_html ⇒ Object
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 |