Parent

Class/Module Index [+]

Quicksearch

Ezamar::Element

Attributes

content[RW]

Public Class Methods

demunge_passed_variables(template) click to toggle source

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.

# 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) click to toggle source

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" />
# 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
new(content) click to toggle source

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

# File lib/ezamar/element.rb, line 58
def initialize(content)
  @content = content
end
transform(template) click to toggle source

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.

# 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) click to toggle source

transforms elements like:

<Page> some content </Page>
# File lib/ezamar/element.rb, line 90
def transform_with_content(template, klass)
  template.gsub(/<#{klass}( .*?)?>(.*?)<\/#{klass}>/) do |m|
    params, content = $1.to_s, $2.to_s
  finish_transform(klass, params, content)
  end
end
transform_without_content(template, klass) click to toggle source

transforms elements like:

<Page />
# 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

Public Instance Methods

render(*args) click to toggle source

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.

# File lib/ezamar/element.rb, line 68
def render *args
  @content
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.