Class: Campo::Option

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

Overview

Options for your Selectas!

Constant Summary

Constants inherited from Base

Base::DEFAULT

Instance Attribute Summary

Attributes inherited from Base

#attributes, #attributes The element's html attributes., #fields, #fields The element's child elements.

Attributes included from Childish

#parent

Instance Method Summary collapse

Methods inherited from Base

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

Methods included from Convenience

#bit_of_ruby, #checkbox, #fieldset, #hidden, #input, #literal, #password, #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)
  • inner (String) (defaults to: nil)
  • selected (true, false, nil) (defaults to: nil)
  • attributes (Hash) (defaults to: {})


587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
# File 'lib/campo/campo.rb', line 587

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