campo 0.9.0 → 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES.markdown CHANGED
@@ -1,3 +1,10 @@
1
+ ## v0.9.1 16th of December 2012 ##
2
+
3
+ * `labelled` can now take an attributes hash, so: `.labelled("Favourite tea:", class: "list")` will output `%label{ class: "list", for: "teas", }`
4
+
5
+ ----
6
+
7
+
1
8
  ## v0.9.0 16th of June 2012 ##
2
9
 
3
10
  * Updated the README with info on using plugins, and how to contribute.
data/Gemfile CHANGED
@@ -3,7 +3,7 @@ source "https://rubygems.org"
3
3
  group :development do
4
4
  gem "rake"
5
5
  gem "yard"
6
- gem "maruku"
6
+ gem "rdiscount"
7
7
  gem "wirble"
8
8
  gem "reek"
9
9
  end
data/README.markdown CHANGED
@@ -1,28 +1,34 @@
1
- [![Build Status for development branch](https://secure.travis-ci.org/yb66/Campo.png?branch=develop)](http://travis-ci.org/yb66/Campo)
2
-
3
- # Campo #
1
+ ## Campo ##
4
2
 
5
3
  A static dynamic form builder into haml. Yep, static _and_ dynamic. Use it to statically create a form into haml, but you may notice it's taken advantage of haml's "add a hash to the front of the attributes and it'll get merged" property. [See Haml docs for more](http://haml-lang.com/docs/yardoc/file.HAML_REFERENCE.html#attribute_methods). More on that below.
6
4
 
7
5
  Btw, I'll be using this with Sinatra, if you're using Rails you'll need to work out how that's done as I don't know.
8
6
 
9
- ## Note! ##
7
+ ### Build status ###
8
+
9
+ Master branch:
10
+ [![Build Status for development branch](https://secure.travis-ci.org/yb66/Campo.png?branch=master)](http://travis-ci.org/yb66/Campo)
11
+
12
+ Develop branch:
13
+ [![Build Status for development branch](https://secure.travis-ci.org/yb66/Campo.png?branch=develop)](http://travis-ci.org/yb66/Campo)
14
+
15
+ ### Note! ###
10
16
 
11
17
  As always, keep in mind this is an open source project (licence below) and you can contribute! If you find a problem or would like a feature changed or added, let me know, or even better, fork the project and send me a pull request. See the "Contributing" section for some notes on how to do that.
12
18
 
13
- ## Double note! ##
19
+ ### Double note! ###
14
20
 
15
21
  I use Campo myself, and I'm trying to improve it. As I don't want to push new stuff out before I've had a chance to give it a whirl and see if it makes sense and works (through experience, specs aren't everything) I'll have several versions of this up here, some unreleased. I tend to append a 'b' to the end of an unreleased version. Please make sure you're reading the documentation for the version you're using!
16
22
 
17
- ## Version numbers ##
23
+ ### Version numbers ###
18
24
 
19
25
  You'll notice this library is well past version 0.0.1. Some people take this to mean something like "it works brilliantly", but in fact, I'm attempting to use the [semver standard](http://semver.org/). In essence, it tells you about changes to the API, not about code quality - that's what the specs/tests are for. It's worth a read.
20
26
 
21
- ## Why write this? ##
27
+ ### Why write this? ###
22
28
 
23
29
  However nice Haml is, it's still a lot of effort to build a form. If you've got lots of forms it's worse. The long term plan is to link this in to Sequel.
24
30
 
25
- ## Example! ##
31
+ ### Example! ###
26
32
 
27
33
  Here's an example form:
28
34
 
@@ -105,7 +111,7 @@ and the output:
105
111
  %input{ atts[:Save], tabindex: "#{@campo_tabindex += 1}", id: "Save", type: "submit", value: "Save", }
106
112
 
107
113
 
108
- ## Haml attributes ##
114
+ ### Haml attributes ###
109
115
 
110
116
  Back to the dynamic attributes mentioned earlier. What does this mean? You can pass in a local to dynamically alter the form based on server side logic.
111
117
 
@@ -215,7 +221,7 @@ You can do this with any kind of attribute you wish to add. For example:
215
221
  But I doubt your name is Noel Gallagher, which makes this a spurious example.
216
222
 
217
223
 
218
- ## Be selective ##
224
+ ### Be selective ###
219
225
 
220
226
  opts = [
221
227
  ["ceylon"],
@@ -241,7 +247,7 @@ Output:
241
247
  %option{ atts[:teas_english_breakfast], id: "teas_english_breakfast", value: "english_breakfast", selected: "selected", name: "teas", }English breakfast
242
248
  %option{ atts[:teas_earl_grey], id: "teas_earl_grey", value: "earl_grey", name: "teas", }Earl grey
243
249
 
244
- ## Pass a hash ##
250
+ ### Pass a hash ###
245
251
 
246
252
  opts = {
247
253
  "ceylon"=>"Ceylon",
@@ -284,7 +290,7 @@ Output:
284
290
  %option{ atts[:teas_english_breakfast], id: "teas_english_breakfast", value: "english_breakfast", selected: "selected", name: "teas", }English Breakfast
285
291
  %option{ atts[:teas_earl_grey], id: "teas_earl_grey", value: "earl_grey", name: "teas", }Earl Grey
286
292
 
287
- ## Adding in helpers ##
293
+ ### Adding in helpers ###
288
294
 
289
295
  If you want to use helpers in the attributes, like sinatra's `uri` helper, then add a double-quote to the front:
290
296
 
@@ -305,7 +311,7 @@ outputs:
305
311
 
306
312
  Although, if you forget the "=" sign it will add it for you.
307
313
 
308
- ## And literals ##
314
+ ### And literals ###
309
315
 
310
316
  `bit_of_ruby` is really just a literal with a shortcut. Here are some examples using literals:
311
317
 
@@ -352,13 +358,13 @@ You can use literals to wrap forms in divs too:
352
358
  %p= "I like tea!"
353
359
 
354
360
 
355
- ## tabindex ##
361
+ ### tabindex ###
356
362
 
357
363
  Each field gets `@campo_tabindex += 1` added to its attributes. This will generate a tabindex easily for you.
358
364
 
359
365
  Since it's an instance variable it can be passed through easily to nested partials and the count will still be right.
360
366
 
361
- ## Blocks ##
367
+ ### Blocks ###
362
368
 
363
369
  Most fields will accept a block, so you can nest whatever you like. Generally I just use this for forms, fieldsets and selects (and those have specs) but if you want to try something new, do it! Let me know if it breaks. You don't have to use the `|var|` notation unless you feel it's helpful.
364
370
 
@@ -396,7 +402,7 @@ Output:
396
402
  Blah
397
403
  %input{ atts[:blah], tabindex: "#{@campo_tabindex += 1}", id: "blah", type: "text", name: "blah", }
398
404
 
399
- ## Plugins ##
405
+ ### Plugins ###
400
406
 
401
407
  I've written a couple of plugins. If you wish to write one yourself you'll need to look for the code for now until I write some proper instructions.
402
408
 
@@ -504,7 +510,7 @@ To contribute some code:
504
510
  * Shrug and get on with your life when it doesn't arrive, but know you helped quite a few people in their life, even in a small way - 1000 raindrops will fill a bucket!
505
511
 
506
512
 
507
- ## Licence ##
513
+ ### Licence ###
508
514
 
509
515
  This is under the MIT Licence.
510
516
 
data/lib/campo/campo.rb CHANGED
@@ -106,6 +106,8 @@ module Campo
106
106
  # # Select using chain of options
107
107
  # form.select("bands").option("Suede").option("Blur").option("Oasis").option("Echobelly").option("Pulp").option("Supergrass").with_default.labelled("Favourite band:")
108
108
  #
109
+ # select( "breads", {opts: [[1, "White"],[2,"Malted"],[3,"Black"],[4,"Wholemeal"], [5,"Rye"] ] })
110
+ #
109
111
  # @see Select#initialize
110
112
  def select( *args, &block )
111
113
  select = Campo::Select.new( *args, &block )
@@ -279,12 +281,14 @@ module Campo
279
281
 
280
282
 
281
283
  # Bit of a convenience method for adding a label around any element.
282
- # @param [String] inner The text for the label.
284
+ # @param [String] :inner The text for the label.
285
+ # @param [Hash] :attributes Attributes for the label.
283
286
  # @return [Base]
284
- def labelled( inner=nil )
287
+ # @see Label#initialize
288
+ def labelled( inner=nil, attributes={} )
285
289
  inner ||= self.attributes[:name].gsub(/\[\]/, "").gsub("_"," ").capitalize
286
290
  parent = self.parent
287
- label = Label.new( %Q!#{@attributes[:id]}!, inner ) << self
291
+ label = Label.new( %Q!#{@attributes[:id]}!, inner, attributes ) << self
288
292
  retval = if parent.nil?
289
293
  label
290
294
  else
@@ -460,7 +464,7 @@ module Campo
460
464
 
461
465
  class Haml_Ruby_Insert < Base
462
466
  def initialize( s )
463
- raise ArgumentError, "you may only pass a string to Haml_Ruby_Insert/bit_of_ruby" unless s.kind_of?( String )
467
+ fail ArgumentError, "you may only pass a string to Haml_Ruby_Insert/bit_of_ruby" unless s.kind_of?( String )
464
468
  super( nil ) # no name needed
465
469
 
466
470
  # @todo Don't enforce the equals sign, as a hyphen is also valid for adding a bit of ruby. Raise an exception
@@ -494,12 +498,18 @@ module Campo
494
498
  end
495
499
  end # Literal
496
500
 
497
-
501
+
502
+ # For building 'select' tags.
498
503
  class Select < Base
504
+
505
+ # @param [String] :name
506
+ # @param [Hash] :params
507
+ # option params [Hash] :attributes
508
+ # option params [#to_s] :haml_insert
499
509
  def initialize( name, params={} )
500
- opts = params[:opts] || []
501
- attributes = params[:attributes] || {}
502
- haml_insert = params[:haml_insert] || nil
510
+ opts = params.fetch :opts, []
511
+ attributes = params.fetch :attributes, {}
512
+ haml_insert = params.delete :haml_insert
503
513
 
504
514
  super( name, { tabindex: %q!#{@campo_tabindex += 1}! }.merge(attributes) )
505
515
 
@@ -515,6 +525,10 @@ module Campo
515
525
  end # initialize
516
526
 
517
527
 
528
+ # @param [#to_s] value The value for attribute 'value'.
529
+ # @param [#to_s] inner The display text.
530
+ # @param [true,false,nil] selected Whether the field is selected. Defaults to false.
531
+ # @param [Hash] attributes Hash of attributes. They'll get added to the element.
518
532
  # @example (see Convenience#select)
519
533
  def option( *args )
520
534
  value = args.shift
@@ -528,7 +542,7 @@ module Campo
528
542
 
529
543
  # Adds a default selection to a select list. By default it is disabled.
530
544
  # @param [String,nil] The display string for the option. Default is "Choose one:".
531
- # @param [Hash,nil] attributes Attributes for the option. Defaults to {disabled: "disabled"}, pass in an empty hash to override (or a filled one), or nil for the default.
545
+ # @param [Hash,nil] attributes Attributes for the option. Defaults to Hash[ :disabled => "disabled" ], pass in an empty hash to override (or a filled one), or nil for the default.
532
546
  # @example
533
547
  # As a default:
534
548
  # form.select("teas").with_default.option("ceylon")
data/lib/campo/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Campo
2
- VERSION = "0.9.0"
2
+ VERSION = "0.9.1"
3
3
  end
data/spec/campo_spec.rb CHANGED
@@ -40,7 +40,7 @@ STR
40
40
  context "Given no form" do
41
41
  context "When given a select field with options" do
42
42
  let(:expected) { %q!
43
- %label{ for: "teas", }
43
+ %label{ class: "list", for: "teas", }
44
44
  Favourite tea:
45
45
  %select{ atts[:teas], tabindex: "#{@campo_tabindex += 1}", id: "teas", name: "teas", }
46
46
  %option{ atts[:teas_default], id: "teas_default", value: "", disabled: "disabled", name: "teas", }Choose one:
@@ -51,7 +51,7 @@ STR
51
51
  }
52
52
  let(:tag) {
53
53
  select = Campo::Select.new( "teas" )
54
- tag = select.with_default.option("ceylon").option("breakfast").option("earl grey").labelled("Favourite tea:")
54
+ tag = select.with_default.option("ceylon").option("breakfast").option("earl grey").labelled("Favourite tea:", class: "list")
55
55
  tag
56
56
  }
57
57
  subject { Campo.output tag, :partial=>true }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: campo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.9.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-16 00:00:00.000000000 Z
12
+ date: 2012-12-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: haml
@@ -109,7 +109,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
109
109
  version: '0'
110
110
  requirements: []
111
111
  rubyforge_project:
112
- rubygems_version: 1.8.23
112
+ rubygems_version: 1.8.24
113
113
  signing_key:
114
114
  specification_version: 3
115
115
  summary: Form builder for Haml
@@ -121,3 +121,4 @@ test_files:
121
121
  - spec/plugins_spec.rb
122
122
  - spec/spec_helper.rb
123
123
  - spec/support/matchers/items.rb
124
+ has_rdoc: