Class: Campo::Base

Inherits:
Object
  • Object
show all
Includes:
Childish, Convenience, Iding
Defined in:
lib/campo.rb

Constant Summary collapse

DEFAULT =
{ tabindex: nil }

Instance Attribute Summary collapse

Attributes included from Childish

#parent

Class Method Summary collapse

Instance Method Summary collapse

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, attributes = {}, &block) ⇒ Base

Returns a new instance of Base.



170
171
172
173
174
175
# File 'lib/campo.rb', line 170

def initialize( name, attributes={}, &block )
  @attributes = DEFAULT.merge( attributes.merge({name: name}) ).reject{|k,v| v.nil? }
  @fields = []
  block.call( self ) if block
  self
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



168
169
170
# File 'lib/campo.rb', line 168

def attributes
  @attributes
end

#fieldsObject

Returns the value of attribute fields.



168
169
170
# File 'lib/campo.rb', line 168

def fields
  @fields
end

Class Method Details

.output(top, so_far = "", count = 0, tab = 2) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
# File 'lib/campo.rb', line 215

def self.output( top, so_far="", count=0, tab=2 )
  so_far << "#{top.output( count, tab )}\n"
  count += 1
  if top.respond_to?( :fields ) && top.fields.length >= 1
    top.fields.each do |field|
      so_far = Base.output( field, so_far, count, tab ) 
    end
  end

  so_far
end

.quotable(s) ⇒ Object

if the string provided begins with a double quote but does not end in one, make it an unquoted string on output else, wrap it in quotes



206
207
208
209
210
211
212
# File 'lib/campo.rb', line 206

def self.quotable( s )
  retval = if s.respond_to?(:start_with?) && s.start_with?( %Q!"! ) &! s.end_with?( %Q!"! )
    s[1.. -1] # chop the first character
  else
    %Q!"#{s}"! # wrap
  end 
end

.unhash(hash, skip = nil) ⇒ Object

labelled



200
201
202
# File 'lib/campo.rb', line 200

def self.unhash( hash, skip=nil )
  hash.reject{|k,v| v.nil?  }.reject{|k,v| k.to_sym == skip.to_sym unless skip.nil? }.reduce(""){|mem, (k,v)| mem + %Q!#{k}: #{Base.quotable(v)}, !}
end

Instance Method Details

#labelled(inner = nil) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/campo.rb', line 185

def labelled( inner=nil )
  inner ||= self.attributes[:name].gsub("_"," ").capitalize
  parent = self.parent
  label = Label.new( %Q!#{@attributes[:name] + id_tag(@attributes[:value]).gsub(/\W/, "_")}!, inner ) << self
  retval = if parent.nil?
    label
  else
    parent.fields.delete self
    parent << label
    label
  end
  
  retval
end

#on_output(&block) ⇒ Object



177
178
179
# File 'lib/campo.rb', line 177

def on_output( &block )
  @output_listener = block
end

#output(n = 0, tab = 2) ⇒ Object



181
182
183
# File 'lib/campo.rb', line 181

def output( n=0, tab=2 )
  @output_listener.call n, tab
end