Class: Ezamar::Element

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content) ⇒ Element

this will be called by #transform, passes along the stuff inside the tags for the element



58
59
60
# File 'lib/ezamar/element.rb', line 58

def initialize(content)
  @content = content
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



53
54
55
# File 'lib/ezamar/element.rb', line 53

def content
  @content
end

Class Method Details

.demunge_passed_variables(template) ⇒ Object

basically processes stuff like

'foo="bar" foobar="baz"'

do NOT pass actual objects that cannot be simply read as a string here, the information will be lost.

Exceptions are true, false, Integers and Floats. They will appear in their real form (this again is also valid for strings that contain these values in a way that makes Integer/Float possible to parse them)

Just remember, walk like a duck, talk like a duck.



146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/ezamar/element.rb', line 146

def demunge_passed_variables(template)
  template.scan(/\s?(.*?)=["'](.*?)["']/).inject({}) do |hash, (key, value)|
    value =
    case value
    when 'true'
      true
    when 'false'
      false
    else
      Integer(value) rescue Float(value) rescue value
    end
  hash.merge key => value
  end
end

.finish_transform(klass, params, content) ⇒ Object

find the element, create an instance, pass it the content check if it responds to :render and sets instance-variables that are named after the keys and hold the values of the parameters you passed to the Element

Parameters look like:

<Page foo="true"> bar </Page>
<Page foo="true" />


116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/ezamar/element.rb', line 116

def finish_transform(klass, params, content)
  scope =
    if Ramaze::Action.current
      Ramaze::Action.current.controller
    else
      self.class
    end
  instance = scope.constant(klass).new(content)

  demunge_passed_variables(params).each do |key, value|
    instance.instance_variable_set("@#{key}", value)
  end

  instance.render
rescue => ex
  Ramaze::Log.debug(ex.message)
  ''
end

.transform(template) ⇒ Object

transforms all <Element> tags within the string, takes also a binding to be compatible to the transform-pipeline, won’t have any use for it though.



77
78
79
80
81
82
83
84
85
# File 'lib/ezamar/element.rb', line 77

def transform template
  matches = template.scan(/<([A-Z][a-zA-Z0-9]*)(.*?)?>/)

  matches.each do |(klass, params)|
    transformer = (params[-1,1] == '/' ? :without : :with)
  template = send("transform_#{transformer}_content", template, klass)
  end
  template
end

.transform_with_content(template, klass) ⇒ Object

transforms elements like:

<Page> some content </Page>


90
91
92
93
94
95
# File 'lib/ezamar/element.rb', line 90

def transform_with_content(template, klass)
  template.gsub(/<#{klass}( .*?)?>(.*?)<\/#{klass}>/m) do |m|
    params, content = $1.to_s, $2.to_s
  finish_transform(klass, params, content)
  end
end

.transform_without_content(template, klass) ⇒ Object

transforms elements like:

<Page />


100
101
102
103
104
105
# File 'lib/ezamar/element.rb', line 100

def transform_without_content(template, klass)
  template.gsub(/<#{klass}( .*?)?\/>/) do |m|
    params = $1.to_s
    finish_transform(klass, params, content = '')
  end
end

Instance Method Details

#render(*args) ⇒ Object

The method that will be called upon to render the things inside the element, you can access #content from here, which contains the contents between the tags.

It should answer with a String.



68
69
70
# File 'lib/ezamar/element.rb', line 68

def render *args
  @content
end