Class: Aurita::GUI::Element

Inherits:
Object
  • Object
show all
Defined in:
lib/aurita-gui/element.rb

Overview

GUI::Element is the base class for any rendering implementation. It consists of the following members:

* @tag: The HTML tag to render. 
* @attrib: A hash storing tag attributes, like 
           { :href => '/link/to/somewhere' }
* @content: Content this element is wrapping. 
            Content can be set in the constructor 
            via parameter :content or using a 
            block or by #content and #content=.

Most methods invoked on an Element instance are redirected to return or set a tag attribute. Example:

link = Element(:tag => :a) { 'klick me' }
link.href = '/link/to/somewhere'

Same as

link = Element(:tag => :a, 
               :content => 'click me', 
               :href => '/link/to/somewhere')

An Element instance can wrap one or more other elements:

image_link = Element.new(:tag => :a, :href => '/link/') { 
               Element.new(:tag => :img, :src => '/an_image.png')
             }

In case an element has no content, it will render a self-closing tag, like <img … />.

In most cases you won’t use class Element directly, but by using a factory like Aurita::GUI::HTML or by any derived class like Aurita::GUI::Form or Aurita::GUI::Table.

Constant Summary collapse

@@element_count =
0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}, &block) ⇒ Element

Returns a new instance of Element.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/aurita-gui/element.rb', line 51

def initialize(params={}, &block) 
  @@element_count += 1
  @id      = @@element_count
  @parent  = params[:parent]
  params.delete(:parent)

  params[:tag] = :div if params[:tag].nil?

  if block_given? then
    @content = yield
  else
    @content = params[:content]
    @content = [ @content ] unless @content.kind_of? Array
  end
  params.delete(:content)
  @tag     = params[:tag]
  params.delete(:tag)
  # params[:id] = self.class.to_s.split('::')[-1].downcase + '_' << @@element_count.to_s if params[:id].nil?
  params[:onclick] << ';' unless params[:onclick].nil? or params[:onclick].include?(';')
  
  @attrib = params
  # @attrib[:id] = @attrib[:id].to_s 
  
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, value = nil) ⇒ Object

Redirect methods to setting or retreiving tag attributes.



101
102
103
104
# File 'lib/aurita-gui/element.rb', line 101

def method_missing(meth, value=nil)
  return @attrib[meth] unless value or meth.to_s.include? '='
  @attrib[meth.to_s.gsub('=','').intern] = value
end

Instance Attribute Details

#attribObject

Returns the value of attribute attrib.



49
50
51
# File 'lib/aurita-gui/element.rb', line 49

def attrib
  @attrib
end

#contentObject

Returns the value of attribute content.



49
50
51
# File 'lib/aurita-gui/element.rb', line 49

def content
  @content
end

#parentObject

Returns the value of attribute parent.



49
50
51
# File 'lib/aurita-gui/element.rb', line 49

def parent
  @parent
end

#tagObject

Returns the value of attribute tag.



49
50
51
# File 'lib/aurita-gui/element.rb', line 49

def tag
  @tag
end

#typeObject

Returns the value of attribute type.



49
50
51
# File 'lib/aurita-gui/element.rb', line 49

def type
  @type
end

Instance Method Details

#+(other) ⇒ Object Also known as: <<

Render this element to a string and append another element.



87
88
89
90
# File 'lib/aurita-gui/element.rb', line 87

def +(other)
#     return string << other.string if other.kind_of? Element
  return [ self, other ]
end

#[](index) ⇒ Object

Do not redirect random access operators.



123
124
125
126
# File 'lib/aurita-gui/element.rb', line 123

def [](index)
  return @content[index]
   #   raise ::Exception.new('Undefined method [] for ' << self.class.to_s)
end

#[]=(index, element) ⇒ Object

Do not redirect random access operators.



128
129
130
131
# File 'lib/aurita-gui/element.rb', line 128

def []=(index,element)
  @content[index] = element
   #  raise ::Exception.new('Undefined method []= for ' << self.class.to_s)
end

#clear_floatingObject

Static helper definition for clearing CSS floats.



141
142
143
# File 'lib/aurita-gui/element.rb', line 141

def clear_floating
  '<div style="clear: both;" />'
end

#dom_idObject

Return DOM id of this element.



77
78
79
# File 'lib/aurita-gui/element.rb', line 77

def dom_id
  @attrib[:id]
end

#dom_id=(value) ⇒ Object

Set DOM id of this element.



81
82
83
# File 'lib/aurita-gui/element.rb', line 81

def dom_id=(value)
  @attrib[:id] = value
end

#each(&block) ⇒ Object



172
173
174
# File 'lib/aurita-gui/element.rb', line 172

def each(&block)
  @content.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/aurita-gui/element.rb', line 135

def empty? 
  @content.length == 0
end

#idObject

Alias definition for #dom_id()



118
119
120
# File 'lib/aurita-gui/element.rb', line 118

def id
  @attrib[:id]
end

#id=(value) ⇒ Object

Alias definition for #dom_id=(value) Define explicitly so built-in method #id is not invoked instead



114
115
116
# File 'lib/aurita-gui/element.rb', line 114

def id=(value)
  @attrib[:id] = value
end

#lengthObject

raise ::Exception.new(‘Undefined method []= for ’ << self.class.to_s)



132
133
134
# File 'lib/aurita-gui/element.rb', line 132

def length
  @content.length
end

#stringObject Also known as: to_s

Render this element to a string.



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/aurita-gui/element.rb', line 146

def string
  attrib_string = ''
  @attrib.each_pair { |name,value|
    if value.instance_of?(Array) then
      value = value.join(' ')
    elsif
      value.instance_of?(TrueClass) then
      value = name
    end
    if !value.nil? then
      value = value.to_s
      attrib_string << name.to_s + '="' << value + '" '
    end
  }
  
  if content.to_s != '' then
    '<' << @tag.to_s << ' ' << attrib_string << '>' << "\n" << 
    content.to_s + 
    '</' << @tag.to_s << '>' << "\n" 
  else
    '<' << @tag.to_s << ' ' << attrib_string << '/>' << "\n" 
  end
  
end

#to_aryObject Also known as: to_a



93
94
95
# File 'lib/aurita-gui/element.rb', line 93

def to_ary
  [ self ]
end