Class: Former::Element
- Inherits:
-
Object
- Object
- Former::Element
- Includes:
- ActionView::Helpers::TagHelper
- Defined in:
- lib/former/element.rb
Instance Attribute Summary collapse
-
#block ⇒ Object
readonly
Returns the value of attribute block.
-
#node ⇒ Object
readonly
Returns the value of attribute node.
-
#query ⇒ Object
readonly
Returns the value of attribute query.
Instance Method Summary collapse
-
#initialize(node, query, index, block) ⇒ Element
constructor
A new instance of Element.
- #set_value(value) ⇒ Object
- #to_form_html ⇒ Object
- #to_html ⇒ Object
- #to_json ⇒ Object
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
#block ⇒ Object (readonly)
Returns the value of attribute block.
7 8 9 |
# File 'lib/former/element.rb', line 7 def block @block end |
#node ⇒ Object (readonly)
Returns the value of attribute node.
7 8 9 |
# File 'lib/former/element.rb', line 7 def node @node end |
#query ⇒ Object (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_html ⇒ Object
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_html ⇒ Object
40 41 42 |
# File 'lib/former/element.rb', line 40 def to_html @node.to_html end |
#to_json ⇒ Object
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 |