campo 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES CHANGED
@@ -1,3 +1,5 @@
1
+ v0.3.0 Improved passing of hash arguments for options to select tags. More specs.
2
+
1
3
  v0.2.0 All Base fields can now take a block and the convenience methodsl, allowing them to nest other elements regardless of whether the parent is a form or a select. This basically makes it a lot easier to use a literal as the root of the document.
2
4
 
3
5
  v0.1.1 Label naming a bit better... possibly! It breaks on _ and capitalises when trying for a default.
data/README.markdown CHANGED
@@ -1,10 +1,15 @@
1
1
  # Campo #
2
2
 
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. http://haml-lang.com/docs/yardoc/file.HAML\_REFERENCE.html#attribute_methods. More on that below.
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.
4
4
 
5
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.
6
6
 
7
- ## Why though? ##
7
+ ## Note! ##
8
+
9
+ 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.
10
+
11
+
12
+ ## Why write this? ##
8
13
 
9
14
  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.
10
15
 
@@ -136,19 +141,25 @@ and that outputs:
136
141
  </fieldset>
137
142
  </form>
138
143
 
144
+ ## Haml attributes ##
139
145
 
140
146
  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.
141
147
 
142
- These get added to the top, to provide sane defaults:
148
+ These get added to the top when calling `Campo.output`, to provide sane defaults:
143
149
 
144
150
  - atts = {} if atts.nil?
145
151
  - atts.default = {} if atts.default.nil?
146
152
  - inners = {} if inners.nil?
147
153
  - inners.default = "" if inners.default.nil?
148
154
 
155
+ Note: if you don't want these added, you can do:
156
+
157
+ Campo.output :partial, your_tag
158
+
159
+
149
160
  In the select tag (below), notice how each tag gets a local variable added to the front. You can either fill that variable with a hash pair, or an empty hash gets passed and nothing happens.
150
161
 
151
- Here's the Campo code:
162
+ Here's some Campo code for a select tag with options:
152
163
 
153
164
  form = Campo.form "best_bands", action: "/best/bands/" do |form|
154
165
  form.select("bands").option("Suede").option("Blur").option("Oasis").option("Echobelly").option("Pulp").option("Supergrass").with_default.labelled("Favourite band:")
@@ -166,11 +177,25 @@ or
166
177
  s.option("Pulp")
167
178
  s.option("Supergrass")
168
179
  end.labelled("Favourite band:")
169
-
170
- (or mix and match blocks, .new, arrays and hashes)
171
180
 
172
- Campo.output form # generate the haml
181
+ or an array of arrays:
173
182
 
183
+ form = Campo.form "best_bands", action: "/best/bands/"
184
+ form.select( "bands", opts: [
185
+ ["Suede"],
186
+ ["Blur"],
187
+ ["Oasis"],
188
+ ["Echobelly"],
189
+ ["Pulp"],
190
+ ["Supergrass"],
191
+ ] ).with_default.labelled("Favourite band:")
192
+
193
+ (or mix and match blocks, .new, arrays and hashes)
194
+
195
+
196
+ Generate the haml:
197
+
198
+ Campo.output form #
174
199
 
175
200
  And the Haml generated:
176
201
 
@@ -203,6 +228,78 @@ You can do this with any kind of attribute you wish to add. For example:
203
228
 
204
229
 
205
230
  atts[:bands_blur] = {not_worth_listening_to: "selected"}
231
+
232
+ ## Be selective ##
233
+
234
+ opts = [
235
+ ["ceylon"],
236
+ ["english_breakfast", :selected],
237
+ ["earl_grey"]
238
+ ]
239
+
240
+ form = Campo.form "selective_example"
241
+ form.select "teas", opts: opts
242
+
243
+ Campo.output form
244
+
245
+ Output:
246
+
247
+ - atts = {} if atts.nil?
248
+ - atts.default = {} if atts.default.nil?
249
+ - inners = {} if inners.nil?
250
+ - inners.default = "" if inners.default.nil?
251
+ - i = 0 # for tabindex
252
+
253
+ %form{ atts[:selective_example], method: "POST", name: "selective_example", }
254
+ %select{ atts[:teas], tabindex: "#{i += 1}", name: "teas", }
255
+ %option{ atts[:teas_ceylon], value: "ceylon", id: "teas_ceylon", name: "teas", }Ceylon
256
+ %option{ atts[:teas_english_breakfast], value: "english_breakfast", selected: "selected", id: "teas_english_breakfast", name: "teas", }English breakfast
257
+ %option{ atts[:teas_earl_grey], value: "earl_grey", id: "teas_earl_grey", name: "teas", }Earl grey
258
+
259
+ ## Pass a hash ##
260
+
261
+ opts = {
262
+ "ceylon"=>"Ceylon",
263
+ "english_breakfast"=>"English Breakfast",
264
+ "earl_grey"=>"Earl Grey"
265
+ }
266
+ # the keys can be symbols too, it makes no difference to the output
267
+
268
+ form = Campo.form "simple_hash_example"
269
+ form.select "teas", opts: opts
270
+
271
+ Campo.output :partial, form
272
+
273
+ Output:
274
+
275
+ %form{ atts[:simple_hash_example], method: "POST", name: "simple_hash_example", }
276
+ %select{ atts[:teas], tabindex: "#{i += 1}", name: "teas", }
277
+ %option{ atts[:teas_ceylon], value: "ceylon", id: "teas_ceylon", name: "teas", }Ceylon
278
+ %option{ atts[:teas_english_breakfast], value: "english_breakfast", id: "teas_english_breakfast", name: "teas", }English Breakfast
279
+ %option{ atts[:teas_earl_grey], value: "earl_grey", id: "teas_earl_grey", name: "teas", }Earl Grey
280
+
281
+ With an array for the value:
282
+
283
+ opts = {
284
+ "ceylon"=>["Ceylon"],
285
+ "english_breakfast"=>["English Breakfast", :selected],
286
+ "earl_grey"=>["Earl Grey"]
287
+ }
288
+
289
+ form = Campo.form "hash_with_array_example"
290
+ form.select "teas", opts: opts
291
+
292
+ Campo.output :partial, form
293
+
294
+ Output:
295
+
296
+ %form{ atts[:hash_with_array_example], method: "POST", name: "hash_with_array_example", }
297
+ %select{ atts[:teas], tabindex: "#{i += 1}", name: "teas", }
298
+ %option{ atts[:teas_ceylon], value: "ceylon", id: "teas_ceylon", name: "teas", }Ceylon
299
+ %option{ atts[:teas_english_breakfast], value: "english_breakfast", selected: "selected", id: "teas_english_breakfast", name: "teas", }English Breakfast
300
+ %option{ atts[:teas_earl_grey], value: "earl_grey", id: "teas_earl_grey", name: "teas", }Earl Grey
301
+
302
+ ## Adding in helpers ##
206
303
 
207
304
  If you want to use helpers in the attributes, like sinatra's `uri` helper, then add a quote to the front:
208
305
 
@@ -221,6 +318,8 @@ outputs:
221
318
  %form{ atts[:best_bands], method: "POST", action: uri("/best/bands/"), name: "best_bands", }
222
319
  = 5 + 1
223
320
 
321
+ ## And literals ##
322
+
224
323
  It's really just a literal:
225
324
 
226
325
 
@@ -273,4 +372,41 @@ You can use literals to wrap forms in divs too:
273
372
  %option{ atts[:teas_ceylon], value: "ceylon", id: "teas_ceylon", name: "teas", }Ceylon
274
373
  %option{ atts[:teas_breakfast], value: "breakfast", id: "teas_breakfast", name: "teas", }Breakfast
275
374
  %option{ atts[:teas_earl_grey], value: "earl grey", id: "teas_earl_grey", name: "teas", }Earl grey
276
- %p= "I like tea!"
375
+ %p= "I like tea!"
376
+
377
+ ## tabindex ##
378
+
379
+ Each field gets `tabindex: "#{i += 1}"` added to its attributes. This will generate a tabindex easily for you.
380
+
381
+ Note: I'm considering making this an instance variable so that it can be passed through easily to nested partials, but I need to test it first.
382
+
383
+ ## Blocks ##
384
+
385
+ 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.
386
+
387
+ form = Campo.literal "%div" do |div|
388
+ div.form "nested" do |form|
389
+ form.fieldset do |f|
390
+ f.select "blurg" do |s|
391
+ s.option "oopsie"
392
+ s.option "daisies"
393
+ end.labelled "splat"
394
+ f.text "blah"
395
+ end
396
+ end
397
+ end
398
+
399
+ ## Licence ##
400
+
401
+ This is under the MIT Licence.
402
+
403
+ Copyright (c) 2011 Iain Barnett
404
+
405
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
406
+
407
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
408
+
409
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
410
+
411
+ In other words, be good.
412
+
data/lib/campo/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Campo
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/campo.rb CHANGED
@@ -128,18 +128,23 @@ module Campo
128
128
  end # Convenience
129
129
 
130
130
  module Helpers
131
+
132
+
131
133
  # [ [id, lookup, selected || false], ... ]
132
134
  def self.options_builder( name, opts )
133
135
  return [] if opts.nil? || opts.empty?
134
-
135
- opts.map do |opt|
136
- id, lookup, selected, atts = opt
137
- selected = selected ? true : false
138
- atts = atts.nil? ? { } : atts
139
-
140
- Campo::Option.new( name, id, lookup, selected, atts )
136
+
137
+ if opts.respond_to? :each_pair
138
+ opts.map do |id, (inner, selected, atts)|
139
+ Campo::Option.new( name, id, inner, selected, atts )
140
+ end
141
+ else
142
+ opts.map do |id, inner, selected, atts|
143
+ Campo::Option.new( name, id, inner, selected, atts )
144
+ end
141
145
  end
142
- end
146
+ end # def
147
+
143
148
 
144
149
  def self.options_outputter( opts=[] )
145
150
  return "" if opts.nil? || opts.empty?
@@ -382,21 +387,29 @@ STR
382
387
 
383
388
 
384
389
  class Option < Base
385
- attr_accessor :value, :checked
386
390
 
391
+ # @param [String] name
392
+ # @param [String] value
387
393
  def initialize( name, value, inner=nil, selected=nil, attributes={} )
388
- attributes ||= {}
389
- if inner.kind_of? TrueClass
390
- selected = attributes
394
+ unless inner.nil? || inner.kind_of?( String )
395
+ attributes = selected
396
+ selected = inner
391
397
  inner = nil
392
398
  end
393
399
 
394
- @inner = inner || value
400
+ unless selected.nil? || selected.kind_of?( TrueClass )
401
+ if selected.respond_to? :each_pair
402
+ attributes = selected
403
+ selected = nil
404
+ else
405
+ selected = true
406
+ @selected = true
407
+ end
408
+ end
409
+
410
+ attributes ||= {}
395
411
 
396
- if selected.kind_of? Hash
397
- attributes = selected
398
- selected = {}
399
- end
412
+ @inner = (inner || value.gsub("_"," ").capitalize)
400
413
 
401
414
  attributes = { id: "#{(name.gsub(/\W/, "_") + id_tag(value).gsub(/\W/, "_")).downcase}" }.merge(attributes) unless value.nil? || value.to_s.empty?
402
415
 
data/spec/campo_spec.rb CHANGED
@@ -201,7 +201,176 @@ let(:expected) {
201
201
  end # describe input
202
202
 
203
203
  end # Convenience
204
-
204
+
205
+ describe Helpers do
206
+ describe "self.options_builder" do
207
+ context "Given a name" do
208
+ let(:name) { "teas" }
209
+ context "and not given an options argument" do
210
+ it "raises error because not given any options" do
211
+ expect { Campo::Helpers.options_builder name }.to raise_error
212
+ end
213
+ end
214
+ context "and given a nil for the options" do
215
+ let(:opts) { nil }
216
+ subject { Campo::Helpers.options_builder name, opts }
217
+ it { should_not be_nil }
218
+ it { should be_a_kind_of Array }
219
+ end
220
+ context "and given a hash" do
221
+ context "that is empty" do
222
+ subject { Campo::Helpers.options_builder name, Hash.new }
223
+ it { should_not be_nil }
224
+ it { should be_a_kind_of Array }
225
+ it { should be_empty }
226
+ end
227
+ context "with keys" do
228
+ context "only" do
229
+ let(:opts) {
230
+ Hash[ ["ceylon", "english_breakfast", "earl_grey"].zip( Array.new(3, nil ) ) ]
231
+ }
232
+ subject { Campo::Helpers.options_builder name, opts }
233
+ it { should_not be_nil }
234
+ it { should be_a_kind_of Array }
235
+ it { should be_full_of Campo::Option }
236
+ end
237
+ context "and a single string value" do
238
+ let(:opts) {
239
+ Hash[ [
240
+ ["ceylon", "Ceylon"],
241
+ ["english_breakfast", "English Breakfast"],
242
+ ["earl_grey", "Earl Grey"],
243
+ ] ]
244
+ }
245
+ subject { Campo::Helpers.options_builder name, opts }
246
+ it { should_not be_nil }
247
+ it { should be_a_kind_of Array }
248
+ it { should be_full_of Campo::Option }
249
+ end
250
+ context "and an array value" do
251
+ let(:opts) {
252
+ {
253
+ "ceylon"=>["Ceylon"],
254
+ "english_breakfast"=>["English Breakfast", :selected],
255
+ "earl_grey"=>["Earl Grey"]
256
+ }
257
+ }
258
+ subject { Campo::Helpers.options_builder name, opts }
259
+ it { should_not be_nil }
260
+ it { should be_a_kind_of Array }
261
+ it { should be_full_of Campo::Option }
262
+ it { should satisfy {|ys|
263
+ y = ys.find do |y|
264
+ y.attributes[:selected]
265
+ end
266
+ y.attributes[:value] == "english_breakfast"
267
+ }
268
+ }
269
+ end
270
+ end
271
+ end
272
+
273
+ context "and given an array" do
274
+ context "that is empty" do
275
+ let(:opts) { [] }
276
+ subject { Campo::Helpers.options_builder name, opts }
277
+ it { should_not be_nil }
278
+ it { should be_a_kind_of Array }
279
+ it { should be_empty }
280
+ end
281
+ context "that contain [String,String]" do
282
+ let(:opts) {
283
+ [
284
+ ["ceylon", "Ceylon"],
285
+ ["english_breakfast", "English Breakfast"],
286
+ ["earl_grey", "Earl Grey"],
287
+ ]
288
+ }
289
+ subject { Campo::Helpers.options_builder name, opts }
290
+ it { should_not be_nil }
291
+ it { should be_a_kind_of Array }
292
+ it { should be_full_of Campo::Option }
293
+ context "with a selected option" do
294
+ let(:opts_selected) {
295
+ [["ceylon", "Ceylon"], ["english_breakfast", "English Breakfast", :selected], ["earl_grey", "Earl Grey"]]
296
+ }
297
+ subject { Campo::Helpers.options_builder name, opts_selected }
298
+ it { should_not be_nil }
299
+ it { should be_a_kind_of Array }
300
+ it { should be_full_of Campo::Option }
301
+ it { should satisfy {|ys|
302
+ ys.find do |y|
303
+ y.attributes[:selected]
304
+ end.attributes[:value] == "english_breakfast"
305
+ }
306
+ }
307
+
308
+ end
309
+ end
310
+ context "that contain [String]" do
311
+ context "formatted for the name attribute (underscores for spaces, lowercase)" do
312
+ let(:opts) {
313
+ [
314
+ ["ceylon"],
315
+ ["english_breakfast"],
316
+ ["earl_grey"],
317
+ ]
318
+ }
319
+ subject { Campo::Helpers.options_builder name, opts }
320
+ it { should_not be_nil }
321
+ it { should be_a_kind_of Array }
322
+ it { should be_full_of Campo::Option }
323
+ context "with a selected option" do
324
+ let(:opts_selected) {
325
+ [["ceylon"], ["english_breakfast", :selected], ["earl_grey"]]
326
+ }
327
+ subject { Campo::Helpers.options_builder name, opts_selected }
328
+ it { should_not be_nil }
329
+ it { should be_a_kind_of Array }
330
+ it { should be_full_of Campo::Option }
331
+ it { should satisfy {|ys|
332
+ ys.find do |y|
333
+ y.attributes[:selected]
334
+ end.attributes[:value] == "english_breakfast"
335
+ }
336
+ }
337
+
338
+ end
339
+ end
340
+ context "formatted for display" do
341
+ let(:opts) {
342
+ [
343
+ ["Ceylon"],
344
+ ["English Breakfast"],
345
+ ["Earl Grey"],
346
+ ]
347
+ }
348
+ subject { Campo::Helpers.options_builder name, opts }
349
+ it { should_not be_nil }
350
+ it { should be_a_kind_of Array }
351
+ it { should be_full_of Campo::Option }
352
+ context "with a selected option" do
353
+ let(:opts_selected) {
354
+ [["Ceylon"], ["English Breakfast", :selected], ["Earl Grey"]]
355
+ }
356
+ subject { Campo::Helpers.options_builder name, opts_selected }
357
+ it { should_not be_nil }
358
+ it { should be_a_kind_of Array }
359
+ it { should be_full_of Campo::Option }
360
+ it { should satisfy {|ys|
361
+ ys.find do |y|
362
+ y.attributes[:selected]
363
+ end.attributes[:value] == "English Breakfast"
364
+ }
365
+ }
366
+
367
+ end
368
+ end
369
+ end
370
+ end
371
+ end
372
+ end
373
+ end
205
374
 
206
375
  describe Label do
207
376
  let(:tag) { Label.new( "abc", "A, B, or C?" ) }
@@ -564,50 +733,94 @@ s.chomp
564
733
  end
565
734
 
566
735
  context "and an array" do
567
- let(:opts) { [["ford", "ford"], ["bmw", "BMW"], ["ferrari", "Ferrari", "checked"]] }
568
- subject { Campo::Select.new( "pqr", {opts: opts} ) }
569
-
570
- it { should_not be_nil }
571
- it { should be_a_kind_of(Select) }
572
- specify { subject.output.should == %q!%select{ atts[:pqr], tabindex: "#{i += 1}", name: "pqr", }! }
573
-
574
- context "with a block with options" do
575
- let(:opts) { [["ford", "Ford"], ["bmw", "BMW"], ["ferrari", "Ferrari", "checked"]] }
576
- let(:tag){
577
- Campo::Select.new( "pqr", {opts: opts} ) do |s|
578
- s.option "volvo", "Volvo"
579
- s.option "saab", "Saab"
580
- s.option "audi", "Audi"
581
- end
582
- }
583
- subject { tag }
584
-
736
+ # context "of type [String]" do
737
+ # let(:opts) { [["ford"], ["bmw"], ["ferrari", "checked"]] }
738
+ # subject { Campo::Select.new( "pqr", {opts: opts} ) }
739
+ #
740
+ # it { should_not be_nil }
741
+ # it { should be_a_kind_of(Select) }
742
+ # specify { subject.output.should == %q!%select{ atts[:pqr], tabindex: "#{i += 1}", name: "pqr", }! }
743
+ # context "Campo.output" do
744
+ # let(:tag){
745
+ # Campo::Select.new( "pqr", {opts: opts} )
746
+ # }
747
+ # let(:expected) {
748
+ # %q!%select{ atts[:pqr], tabindex: "#{i += 1}", name: "pqr", }
749
+ # %option{ atts[:pqr_ford], value: "ford", id: "pqr_ford", name: "pqr", }Ford
750
+ # %option{ atts[:pqr_bmw], value: "bmw", id: "pqr_bmw", name: "pqr", }BMW
751
+ # %option{ atts[:pqr_ferrari], value: "ferrari", selected: "selected", id: "pqr_ferrari", name: "pqr", }Ferrari!.strip + "\n" }
752
+ # subject { Campo.output :partial, tag }
753
+ # it { should_not be_nil }
754
+ # it { should == expected }
755
+ # end
756
+ #
757
+ # end
758
+ context "of type [String, String]" do
759
+ let(:opts) { [["ford", "ford"], ["bmw", "BMW"], ["ferrari", "Ferrari", "checked"]] }
760
+ subject { Campo::Select.new( "pqr", {opts: opts} ) }
761
+
585
762
  it { should_not be_nil }
586
763
  it { should be_a_kind_of(Select) }
587
764
  specify { subject.output.should == %q!%select{ atts[:pqr], tabindex: "#{i += 1}", name: "pqr", }! }
588
-
589
- context "Campo.output" do
590
- let(:expected) { %q!%select{ atts[:pqr], tabindex: "#{i += 1}", name: "pqr", }
765
+
766
+ context "with a block with options" do
767
+ let(:opts) { [["ford", "Ford"], ["bmw", "BMW"], ["ferrari", "Ferrari", "checked"]] }
768
+ let(:tag){
769
+ Campo::Select.new( "pqr", {opts: opts} ) do |s|
770
+ s.option "volvo", "Volvo"
771
+ s.option "saab", "Saab"
772
+ s.option "audi", "Audi"
773
+ end
774
+ }
775
+ subject { tag }
776
+
777
+ it { should_not be_nil }
778
+ it { should be_a_kind_of(Select) }
779
+ specify { subject.output.should == %q!%select{ atts[:pqr], tabindex: "#{i += 1}", name: "pqr", }! }
780
+
781
+ context "Campo.output" do
782
+ let(:expected) { %q!%select{ atts[:pqr], tabindex: "#{i += 1}", name: "pqr", }
591
783
  %option{ atts[:pqr_volvo], value: "volvo", id: "pqr_volvo", name: "pqr", }Volvo
592
784
  %option{ atts[:pqr_saab], value: "saab", id: "pqr_saab", name: "pqr", }Saab
593
785
  %option{ atts[:pqr_audi], value: "audi", id: "pqr_audi", name: "pqr", }Audi
594
786
  %option{ atts[:pqr_ford], value: "ford", id: "pqr_ford", name: "pqr", }Ford
595
787
  %option{ atts[:pqr_bmw], value: "bmw", id: "pqr_bmw", name: "pqr", }BMW
596
788
  %option{ atts[:pqr_ferrari], value: "ferrari", selected: "selected", id: "pqr_ferrari", name: "pqr", }Ferrari!.strip + "\n" }
597
- subject { Campo.output :partial, tag }
598
- it { should_not be_nil }
599
- it { should == expected }
600
- end
601
-
602
- context "and a default" do
603
-
604
- subject { tag.with_default }
605
- it { should_not be_nil }
606
- it { should be_a_kind_of(Select) }
607
- specify { subject.output.should == %q!%select{ atts[:pqr], tabindex: "#{i += 1}", name: "pqr", }! }
608
-
609
- context "Campo.output" do
610
- let(:expected) { %q!%select{ atts[:pqr], tabindex: "#{i += 1}", name: "pqr", }
789
+ subject { Campo.output :partial, tag }
790
+ it { should_not be_nil }
791
+ it { should == expected }
792
+
793
+ context "and some attributes" do
794
+ let(:opts) { [["ford", "Ford",{class: "blue"}], ["bmw", "BMW"], ["ferrari", "Ferrari", "checked", {class: "green"}]] }
795
+ let(:tag){
796
+ Campo::Select.new( "pqr", {opts: opts} ) do |s|
797
+ s.option "volvo", "Volvo"
798
+ s.option "saab", "Saab"
799
+ s.option "audi", "Audi"
800
+ end
801
+ }
802
+ let(:expected) { %q!%select{ atts[:pqr], tabindex: "#{i += 1}", name: "pqr", }
803
+ %option{ atts[:pqr_volvo], value: "volvo", id: "pqr_volvo", name: "pqr", }Volvo
804
+ %option{ atts[:pqr_saab], value: "saab", id: "pqr_saab", name: "pqr", }Saab
805
+ %option{ atts[:pqr_audi], value: "audi", id: "pqr_audi", name: "pqr", }Audi
806
+ %option{ atts[:pqr_ford], value: "ford", id: "pqr_ford", class: "blue", name: "pqr", }Ford
807
+ %option{ atts[:pqr_bmw], value: "bmw", id: "pqr_bmw", name: "pqr", }BMW
808
+ %option{ atts[:pqr_ferrari], value: "ferrari", selected: "selected", id: "pqr_ferrari", class: "green", name: "pqr", }Ferrari!.strip + "\n" }
809
+ subject { Campo.output :partial, tag }
810
+ it { should_not be_nil }
811
+ it { should == expected }
812
+ end
813
+ end
814
+
815
+ context "and a default" do
816
+
817
+ subject { tag.with_default }
818
+ it { should_not be_nil }
819
+ it { should be_a_kind_of(Select) }
820
+ specify { subject.output.should == %q!%select{ atts[:pqr], tabindex: "#{i += 1}", name: "pqr", }! }
821
+
822
+ context "Campo.output" do
823
+ let(:expected) { %q!%select{ atts[:pqr], tabindex: "#{i += 1}", name: "pqr", }
611
824
  %option{ value: "", disabled: "disabled", name: "pqr", }Choose one:
612
825
  %option{ atts[:pqr_volvo], value: "volvo", id: "pqr_volvo", name: "pqr", }Volvo
613
826
  %option{ atts[:pqr_saab], value: "saab", id: "pqr_saab", name: "pqr", }Saab
@@ -615,14 +828,66 @@ s.chomp
615
828
  %option{ atts[:pqr_ford], value: "ford", id: "pqr_ford", name: "pqr", }Ford
616
829
  %option{ atts[:pqr_bmw], value: "bmw", id: "pqr_bmw", name: "pqr", }BMW
617
830
  %option{ atts[:pqr_ferrari], value: "ferrari", selected: "selected", id: "pqr_ferrari", name: "pqr", }Ferrari!.strip + "\n" }
618
- subject { Campo.output :partial, tag.with_default }
619
- it { should == expected }
831
+ subject { Campo.output :partial, tag.with_default }
832
+ it { should == expected }
833
+ end
620
834
  end
835
+
621
836
  end
622
-
623
837
  end
624
838
  end
625
839
 
840
+ context "and a hash" do
841
+ context "with keys" do
842
+ context "only" do
843
+ let(:opts) {
844
+ Hash[ ["ceylon", "english_breakfast", "earl_grey"].zip( Array.new(3, nil ) ) ]
845
+ }
846
+ let(:tag){
847
+ Campo::Select.new( "tea", {opts: opts} )
848
+ }
849
+ subject { tag }
850
+
851
+ it { should_not be_nil }
852
+ it { should be_a_kind_of(Select) }
853
+ specify { Campo.output( :partial, subject ).should == %Q!%select{ atts[:tea], tabindex: "\#{i += 1}", name: "tea", }\n %option{ atts[:tea_ceylon], value: "ceylon", id: "tea_ceylon", name: "tea", }Ceylon\n %option{ atts[:tea_english_breakfast], value: "english_breakfast", id: "tea_english_breakfast", name: "tea", }English breakfast\n %option{ atts[:tea_earl_grey], value: "earl_grey", id: "tea_earl_grey", name: "tea", }Earl grey\n! }
854
+ end
855
+ context "and a single string value" do
856
+ let(:opts) {
857
+ Hash[ [
858
+ ["ceylon", "Ceylon"],
859
+ ["english_breakfast", "English Breakfast"],
860
+ ["earl_grey", "Earl Grey"],
861
+ ] ]
862
+ }
863
+ let(:tag){
864
+ Campo::Select.new( "tea", {opts: opts} )
865
+ }
866
+ subject { tag }
867
+
868
+ it { should_not be_nil }
869
+ it { should be_a_kind_of(Select) }
870
+ specify { Campo.output( :partial, subject ).should == %Q!%select{ atts[:tea], tabindex: "\#{i += 1}", name: "tea", }\n %option{ atts[:tea_ceylon], value: "ceylon", id: "tea_ceylon", name: "tea", }Ceylon\n %option{ atts[:tea_english_breakfast], value: "english_breakfast", id: "tea_english_breakfast", name: "tea", }English Breakfast\n %option{ atts[:tea_earl_grey], value: "earl_grey", id: "tea_earl_grey", name: "tea", }Earl Grey\n! }
871
+ end
872
+ context "and an array value" do
873
+ let(:opts) {
874
+ {
875
+ "ceylon"=>["Ceylon"],
876
+ "english_breakfast"=>["English Breakfast", :selected],
877
+ "earl_grey"=>["Earl Grey"]
878
+ }
879
+ }
880
+ let(:tag){
881
+ Campo::Select.new( "tea", {opts: opts} )
882
+ }
883
+ subject { tag }
884
+
885
+ it { should_not be_nil }
886
+ it { should be_a_kind_of(Select) }
887
+ specify { Campo.output( :partial, subject ).should == %Q!%select{ atts[:tea], tabindex: "\#{i += 1}", name: "tea", }\n %option{ atts[:tea_ceylon], value: "ceylon", id: "tea_ceylon", name: "tea", }Ceylon\n %option{ atts[:tea_english_breakfast], value: "english_breakfast", selected: "selected", id: "tea_english_breakfast", name: "tea", }English Breakfast\n %option{ atts[:tea_earl_grey], value: "earl_grey", id: "tea_earl_grey", name: "tea", }Earl Grey\n! }
888
+ end
889
+ end
890
+ end
626
891
  end
627
892
 
628
893
  end # initialisation
data/spec/spec_helper.rb CHANGED
@@ -1,9 +1,14 @@
1
1
  # encoding: UTF-8
2
2
 
3
3
  require 'rspec'
4
+ Spec_dir = File.expand_path( File.dirname __FILE__ )
4
5
 
5
6
  # code coverage
6
7
  require 'simplecov'
7
8
  SimpleCov.start
8
9
 
9
10
 
11
+ Dir[ File.join( Spec_dir, "/support/**/*.rb")].each do |f|
12
+ puts "requiring #{f}"
13
+ require f
14
+ end
@@ -0,0 +1,7 @@
1
+ # encoding: UTF-8
2
+
3
+ RSpec::Matchers.define :be_full_of do |klass|
4
+ match do |items|
5
+ items.all?{|x| x.kind_of? klass }
6
+ end
7
+ end
data.tar.gz.sig CHANGED
Binary file
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.2.0
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -50,12 +50,12 @@ cert_chain:
50
50
  -----END CERTIFICATE-----
51
51
 
52
52
  '
53
- date: 2011-11-27 00:00:00.000000000 +00:00
53
+ date: 2011-11-29 00:00:00.000000000 +00:00
54
54
  default_executable:
55
55
  dependencies:
56
56
  - !ruby/object:Gem::Dependency
57
57
  name: haml
58
- requirement: &2153204860 !ruby/object:Gem::Requirement
58
+ requirement: &2164742080 !ruby/object:Gem::Requirement
59
59
  none: false
60
60
  requirements:
61
61
  - - ~>
@@ -63,7 +63,7 @@ dependencies:
63
63
  version: 3.1.1
64
64
  type: :runtime
65
65
  prerelease: false
66
- version_requirements: *2153204860
66
+ version_requirements: *2164742080
67
67
  description: ! ' Form builder for Haml
68
68
 
69
69
  '
@@ -81,6 +81,7 @@ files:
81
81
  - lib/campo/version.rb
82
82
  - spec/campo_spec.rb
83
83
  - spec/spec_helper.rb
84
+ - spec/support/matchers/items.rb
84
85
  has_rdoc: true
85
86
  homepage:
86
87
  licenses: []
@@ -109,3 +110,4 @@ summary: Form builder for Haml
109
110
  test_files:
110
111
  - spec/campo_spec.rb
111
112
  - spec/spec_helper.rb
113
+ - spec/support/matchers/items.rb
metadata.gz.sig CHANGED
Binary file