Class: Campo::Base Abstract

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

Overview

This class is abstract.

Not entirely abstract, but should always be subclassed.

Almost every Campo class inherits from this.

Constant Summary collapse

DEFAULT =

Default attributes.

{ 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, #hidden, #input, #literal, #password, #radio, #select, #submit, #text, #textarea

Methods included from Iding

#id_tag

Methods included from Childish

#push=

Constructor Details

#initialize(name, attributes = {}) { ... } ⇒ Base

Returns a new instance of Base.

Parameters:

  • name (String)

    The value of the element's name attribute.

  • attributes (Hash, optional) (defaults to: {})

    Any attributes for the element. Defaults to a generated tabindex (dependent on the order of form elements).

Yields:

  • Any fields defined in the passed block become children of this element.


250
251
252
253
254
255
256
257
258
259
# File 'lib/campo/campo.rb', line 250

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

  instance_eval( &block ) if block
end

Instance Attribute Details

#attributesArray<Base>

Returns:


245
246
247
# File 'lib/campo/campo.rb', line 245

def attributes
  @attributes
end

#attributes The element's html attributes.(Theelement's html attributes.) ⇒ Hash (readonly)

Returns:

  • (Hash)

# File 'lib/campo/campo.rb', line 239

#fieldsArray<Base>

Returns:


245
246
247
# File 'lib/campo/campo.rb', line 245

def fields
  @fields
end

#fields The element's child elements.(Theelement's child elements.) ⇒ Array<Base> (readonly)

Returns:


245
# File 'lib/campo/campo.rb', line 245

attr_accessor :attributes, :fields

Class Method Details

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

Where the magic of output happens.

Parameters:

  • top (Base)
  • so_far (String) (defaults to: "")
  • depth (Integer) (defaults to: 0)
  • tab (Integer) (defaults to: 2)

    Number of spaces for a tab.


337
338
339
340
341
342
343
344
345
346
347
# File 'lib/campo/campo.rb', line 337

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

  so_far
end

.quotable(s) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Parameters:

  • s (String)

323
324
325
326
327
328
329
# File 'lib/campo/campo.rb', line 323

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, skips = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

TODO:

Make an Attributes class < Hash that deals with this.

Takes a hash and transforms the key value pairs into a stringified version that Haml can consume.

Parameters:

  • hash (Hash)

    The hash to stringify.

  • skips (Array<#to_s>) (defaults to: nil)

    Keys to skip.


313
314
315
316
# File 'lib/campo/campo.rb', line 313

def self.unhash( hash, skips=nil )
  skips = skips.nil? ? [] : skips.map(&:to_sym) # all keys are symbols
  hash.reject{|k,v| v.nil?  }.reject{|k,v| skips.include? k.to_sym }.reduce(""){|mem, (k,v)| mem + %Q!#{k.to_s.include?("-") ? ":\"#{k}\" =>" : "#{k}:"} #{Base.quotable(v)}, !}
end

Instance Method Details

#each(&block) ⇒ Object

Iterates over the fields array.


263
264
265
266
267
268
# File 'lib/campo/campo.rb', line 263

def each(&block)
  block.call self if block
  if respond_to?(:fields) &! fields.empty?
    fields.each{|field| field.each &block }
  end
end

#labelled(inner = nil, attributes = {}) ⇒ Base

Bit of a convenience method for adding a label around any element.

Parameters:

  • :inner (String)

    The text for the label.

  • :attributes (Hash)

    Attributes for the label.

Returns:

See Also:


292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/campo/campo.rb', line 292

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

#on_output(&block) ⇒ Object

Takes a block that handles the rendering.


272
273
274
# File 'lib/campo/campo.rb', line 272

def on_output( &block )
  @output_listener = block
end

#output(n = 0, tab = 2) ⇒ Object Also known as: render

Render to Haml

Parameters:

  • n (Integer) (defaults to: 0)
  • tab (Integer) (defaults to: 2)

280
281
282
283
284
# File 'lib/campo/campo.rb', line 280

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