Class: Campo::Option

Inherits:
Base
  • Object
show all
Defined in:
lib/campo.rb

Constant Summary

Constants inherited from Base

Base::DEFAULT

Instance Attribute Summary

Attributes inherited from Base

#attributes, #fields

Attributes included from Childish

#parent

Instance Method Summary collapse

Methods inherited from Base

#labelled, #on_output, #output, output, quotable, unhash

Methods included from Convenience

#bit_of_ruby, #checkbox, #fieldset, #input, #literal, #radio, #select, #submit, #text, #textarea

Methods included from Iding

#id_tag

Methods included from Childish

#push=

Constructor Details

#initialize(name, value, inner = nil, selected = nil, attributes = {}) ⇒ Option

Returns a new instance of Option.

Parameters:

  • name (String)
  • value (String)


395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# File 'lib/campo.rb', line 395

def initialize( name, value, inner=nil, selected=nil, attributes={} )
  unless inner.nil? || inner.kind_of?( String )
    attributes = selected
    selected = inner
    inner = nil
  end
  
  unless selected.nil? || selected.kind_of?( TrueClass )
    if selected.respond_to? :each_pair
      attributes = selected
      selected = nil   
    else
      selected = true  
      @selected = true   
    end
  end
  
  attributes ||= {}
  
  @inner = (inner || value.gsub("_"," ").capitalize)
  
  attributes = { id: "#{(name.gsub(/\W/, "_") + id_tag(value).gsub(/\W/, "_")).downcase}" }.merge(attributes) unless value.nil? || value.to_s.empty?
  
  super( name, {
    value: value,
    selected: (selected ? "selected" : nil)
  }.merge( attributes ) )
  
  atts_string = "atts[:#{@attributes[:id]}]," unless @attributes[:id].nil?
  
  self.on_output do |n=0, tab=2|
    %Q!#{" " * n * tab}%option{ #{atts_string} #{Base.unhash( @attributes )} }#{@inner}!
  end

end