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.



248
249
250
251
252
253
# File 'lib/campo/campo.rb', line 248

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

Instance Attribute Details

#attributesArray<Base>

Returns:



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

def attributes
  @attributes
end

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

Returns:

  • (Hash)


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

#fieldsArray<Base>

Returns:



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

def fields
  @fields
end

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

Returns:



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

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.



329
330
331
332
333
334
335
336
337
338
339
# File 'lib/campo/campo.rb', line 329

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)


315
316
317
318
319
320
321
# File 'lib/campo/campo.rb', line 315

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.



305
306
307
308
# File 'lib/campo/campo.rb', line 305

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.



257
258
259
260
261
262
# File 'lib/campo/campo.rb', line 257

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) ⇒ Base

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

Parameters:

  • inner (String) (defaults to: nil)

    The text for the label.

Returns:



284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/campo/campo.rb', line 284

def labelled( inner=nil )
  inner ||= self.attributes[:name].gsub(/\[\]/, "").gsub("_"," ").capitalize
  parent = self.parent
  label = Label.new( %Q!#{@attributes[:id]}!, inner ) << 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.



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

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)


274
275
276
277
278
# File 'lib/campo/campo.rb', line 274

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