Class: Former::Element

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::TagHelper
Defined in:
lib/former/element.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node, query, index, block) ⇒ Element

Returns a new instance of Element.



9
10
11
12
13
14
# File 'lib/former/element.rb', line 9

def initialize(node, query, index, block)
  @node = node
  @query = query
  @block = block
  @index = index
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



7
8
9
# File 'lib/former/element.rb', line 7

def block
  @block
end

#nodeObject (readonly)

Returns the value of attribute node.



7
8
9
# File 'lib/former/element.rb', line 7

def node
  @node
end

#queryObject (readonly)

Returns the value of attribute query.



7
8
9
# File 'lib/former/element.rb', line 7

def query
  @query
end

Instance Method Details

#set_value(value) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/former/element.rb', line 32

def set_value(value)
  if @query == :text
    @node.content = value
  else
    @node[@query] = value
  end
end

#to_form_htmlObject



25
26
27
28
29
30
# File 'lib/former/element.rb', line 25

def to_form_html
  name = "former_#{@index}"
  return @block.call(@node, name) unless @block.nil?
  value = (@query == :text) ? @node.text : @node[@query]
  tag(:input, :type => 'text', :name => name, :value => value)
end

#to_htmlObject



40
41
42
# File 'lib/former/element.rb', line 40

def to_html
  @node.to_html
end

#to_jsonObject



16
17
18
19
20
21
22
23
# File 'lib/former/element.rb', line 16

def to_json
  h = { 
    :value => (@query == :text) ? @node.text : @node[@query],
    :nodename => @node.name
  }
  h[:attr] = @query unless @query == :text
  h.to_json
end