Class: Shaf::Formable::Form
- Inherits:
-
Object
- Object
- Shaf::Formable::Form
- Defined in:
- lib/shaf/formable.rb
Constant Summary collapse
- DEFAULT_TYPE =
'application/json'
Instance Attribute Summary collapse
-
#fields ⇒ Object
Returns the value of attribute fields.
-
#href ⇒ Object
Returns the value of attribute href.
-
#name ⇒ Object
Returns the value of attribute name.
-
#resource ⇒ Object
Returns the value of attribute resource.
-
#self_link ⇒ Object
Returns the value of attribute self_link.
-
#title ⇒ Object
Returns the value of attribute title.
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
- #add_field(name, opts) ⇒ Object
-
#initialize(params = {}) ⇒ Form
constructor
A new instance of Form.
- #method ⇒ Object
- #method=(m) ⇒ Object
- #to_html ⇒ Object
Constructor Details
#initialize(params = {}) ⇒ Form
Returns a new instance of Form.
47 48 49 50 51 52 53 |
# File 'lib/shaf/formable.rb', line 47 def initialize(params = {}) @name = params[:name] @title = params[:title] @method = params[:method] || 'POST' @type = params[:type] || DEFAULT_TYPE @fields = (params[:fields] || {}).map { |name, args| Field.new(name, args) } end |
Instance Attribute Details
#fields ⇒ Object
Returns the value of attribute fields.
45 46 47 |
# File 'lib/shaf/formable.rb', line 45 def fields @fields end |
#href ⇒ Object
Returns the value of attribute href.
44 45 46 |
# File 'lib/shaf/formable.rb', line 44 def href @href end |
#name ⇒ Object
Returns the value of attribute name.
44 45 46 |
# File 'lib/shaf/formable.rb', line 44 def name @name end |
#resource ⇒ Object
Returns the value of attribute resource.
44 45 46 |
# File 'lib/shaf/formable.rb', line 44 def resource @resource end |
#self_link ⇒ Object
Returns the value of attribute self_link.
44 45 46 |
# File 'lib/shaf/formable.rb', line 44 def self_link @self_link end |
#title ⇒ Object
Returns the value of attribute title.
44 45 46 |
# File 'lib/shaf/formable.rb', line 44 def title @title end |
#type ⇒ Object
Returns the value of attribute type.
44 45 46 |
# File 'lib/shaf/formable.rb', line 44 def type @type end |
Instance Method Details
#add_field(name, opts) ⇒ Object
67 68 69 |
# File 'lib/shaf/formable.rb', line 67 def add_field(name, opts) @fields << Field.new(name, opts) end |
#method ⇒ Object
59 60 61 |
# File 'lib/shaf/formable.rb', line 59 def method @method.to_s.upcase end |
#method=(m) ⇒ Object
55 56 57 |
# File 'lib/shaf/formable.rb', line 55 def method=(m) @method = m.to_s.upcase end |
#to_html ⇒ Object
71 72 73 74 75 76 77 78 79 |
# File 'lib/shaf/formable.rb', line 71 def to_html form_element do [ hidden_method_element, fields.map { |f| f.to_html }.join("\n"), submit_element, ].compact.join("\n") end end |