Class: Campo::Base
- Inherits:
-
Object
- Object
- Campo::Base
- Includes:
- Childish, Convenience, Iding
- Defined in:
- lib/campo.rb
Direct Known Subclasses
Fieldset, Form, Haml_Ruby_Insert, Input, Label, Legend, Literal, Option, Select, Textarea
Constant Summary collapse
- DEFAULT =
{ tabindex: nil }
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#fields ⇒ Object
Returns the value of attribute fields.
Attributes included from Childish
Class Method Summary collapse
- .output(top, so_far = "", count = 0, tab = 2) ⇒ Object
-
.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.
-
.unhash(hash, skip = nil) ⇒ Object
labelled.
Instance Method Summary collapse
-
#initialize(name, attributes = {}, &block) ⇒ Base
constructor
A new instance of Base.
- #labelled(inner = nil) ⇒ Object
- #on_output(&block) ⇒ Object
- #output(n = 0, tab = 2) ⇒ Object
Methods included from Convenience
#bit_of_ruby, #checkbox, #fieldset, #input, #literal, #radio, #select, #submit, #text, #textarea
Methods included from Iding
Methods included from Childish
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
#attributes ⇒ Object
Returns the value of attribute attributes.
168 169 170 |
# File 'lib/campo.rb', line 168 def attributes @attributes end |
#fields ⇒ Object
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 |
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 |