Class: Campo::Base Abstract
- Inherits:
-
Object
- Object
- Campo::Base
- Includes:
- Childish, Convenience, Iding, Enumerable
- Defined in:
- lib/campo/campo.rb
Overview
Not entirely abstract, but should always be subclassed.
Almost every Campo class inherits from this.
Direct Known Subclasses
Fieldset, Form, Haml_Ruby_Insert, Input, Label, Legend, Literal, Option, Select, Span, Textarea
Constant Summary collapse
- DEFAULT =
Default attributes.
{ tabindex: nil }
Instance Attribute Summary collapse
- #attributes ⇒ Array<Base>
- #attributes The element's html attributes.(Theelement's html attributes.) ⇒ Hash readonly
- #fields ⇒ Array<Base>
- #fields The element's child elements.(Theelement's child elements.) ⇒ Array<Base> readonly
Attributes included from Childish
Class Method Summary collapse
-
.output(top, so_far = "", depth = 0, tab = 2) ⇒ Object
Where the magic of output happens.
-
.quotable(s) ⇒ Object
private
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.
-
.unhash(hash, skips = nil) ⇒ Object
private
Takes a hash and transforms the key value pairs into a stringified version that Haml can consume.
Instance Method Summary collapse
-
#each(&block) ⇒ Object
Iterates over the fields array.
-
#initialize(name, attributes = {}) { ... } ⇒ Base
constructor
A new instance of Base.
-
#labelled(inner = nil, attributes = {}) ⇒ Base
Bit of a convenience method for adding a label around any element.
-
#on_output(&block) ⇒ Object
Takes a block that handles the rendering.
-
#output(n = 0, tab = 2) ⇒ Object
(also: #render)
Render to Haml.
Methods included from Convenience
#bit_of_ruby, #checkbox, #fieldset, #hidden, #input, #literal, #password, #radio, #select, #submit, #text, #textarea
Methods included from Iding
Methods included from Childish
Constructor Details
#initialize(name, attributes = {}) { ... } ⇒ Base
Returns a new instance of Base.
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
#attributes ⇒ Array<Base>
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)
|
# File 'lib/campo/campo.rb', line 239
|
#fields The element's child elements.(Theelement's child elements.) ⇒ Array<Base> (readonly)
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.
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
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.
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.
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.
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
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 |