Class: Shaf::Formable::Form

Inherits:
Object
  • Object
show all
Defined in:
lib/shaf/formable.rb

Constant Summary collapse

DEFAULT_TYPE =
'application/json'

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#fieldsObject

Returns the value of attribute fields.



45
46
47
# File 'lib/shaf/formable.rb', line 45

def fields
  @fields
end

#hrefObject

Returns the value of attribute href.



44
45
46
# File 'lib/shaf/formable.rb', line 44

def href
  @href
end

#nameObject

Returns the value of attribute name.



44
45
46
# File 'lib/shaf/formable.rb', line 44

def name
  @name
end

#resourceObject

Returns the value of attribute resource.



44
45
46
# File 'lib/shaf/formable.rb', line 44

def resource
  @resource
end

Returns the value of attribute self_link.



44
45
46
# File 'lib/shaf/formable.rb', line 44

def self_link
  @self_link
end

#titleObject

Returns the value of attribute title.



44
45
46
# File 'lib/shaf/formable.rb', line 44

def title
  @title
end

#typeObject

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

#methodObject



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_htmlObject



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