qtext 0.6.7 → 0.6.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,120 +0,0 @@
1
- # A DSL class that allows options to be collected for a field
2
- # definition using a block and/or a hash.
3
- # hash = { :colour => :red, :hue => 15 }
4
- # collector = HashCollector.new( hash ) do |hc|
5
- # hc.saturation = 17
6
- # hc.opacity = 0.43
7
- # hc.grooviness = 100
8
- # end
9
- # or like this (without the block parameter)
10
- # collector = HashCollector.new( hash ) do
11
- # saturation 17
12
- # opacity 0.43
13
- # grooviness = 100
14
- # end
15
- # either way, a call to collector.to_hash will result in
16
- # { :hue=>15, :saturation=>17, :opacity=>0.43, :grooviness=>100, :colour=>:red }
17
- # and the following accessors will be added
18
- # collector.hue
19
- # collector.hue( some_value )
20
- # collector.hue = some_value
21
- # for hue, saturation, opacity, grooviness and colour.
22
- class HashCollector
23
- # remove unused methods that might clash with user accessors
24
- keep_methods = %w( __send__ __id__ self send class inspect instance_eval instance_variables )
25
- instance_methods.each do |method|
26
- undef_method( method ) unless keep_methods.include?( method )
27
- end
28
-
29
- # Collect values from the hash and the block, using the collect method.
30
- def initialize( hash = {}, &block )
31
- @hash = hash || {}
32
- gather( &block )
33
- end
34
-
35
- # evaluate the block and collect options from args. Even if it's nil.
36
- def collect( args = {}, &block )
37
- @hash.merge!( args || {} )
38
- unless block.nil?
39
- if block.arity == -1
40
- instance_eval &block
41
- else
42
- yield self
43
- end
44
- end
45
- end
46
-
47
- # return a hash of the collected elements
48
- def to_hash
49
- @hash
50
- end
51
-
52
- protected
53
-
54
- # Modified from Jim Freeze's article.
55
- # For each symbol, add accessors to allow:
56
- # instance.symbol as reader
57
- # instance.symbol( value ) as writer
58
- # instance.symbol = value as writer
59
- def self.dsl_accessor( *symbols )
60
- @stripper ||= /^([^\= ]+)\s*\=?\s*$/
61
- symbols.each do |sym|
62
- stripped = @stripper.match( sym.to_s )[1]
63
- line, st = __LINE__, <<-EOF
64
- def #{stripped}(*val)
65
- if val.empty?
66
- @hash[:#{stripped}]
67
- else
68
- @hash[:#{stripped}] = val.size == 1 ? val[0] : val
69
- end
70
- end
71
-
72
- def #{stripped}=(*val)
73
- @hash[:#{stripped}] = val.size == 1 ? val[0] : val
74
- end
75
- EOF
76
- class_eval st, __FILE__, line + 1
77
- end
78
- end
79
-
80
- # Do not allow accessors to be added dynamically. In other words,
81
- # a subclass like this
82
- # class IndexCollector < HashCollector
83
- # dsl_static
84
- # dsl_accessor :row, :column
85
- # end
86
- # will fail if used like this
87
- # collector = IndexCollector.new( :row => 4, :column => 6 ) do
88
- # other 'oops'
89
- # end
90
- # because :other isn't added by dsl_accessor.
91
- def self.dsl_static
92
- @dynamic = false
93
- end
94
-
95
- # Allow accessors to be added dynamically, the default.
96
- def self.dsl_dynamic
97
- @dynamic = true
98
- end
99
-
100
- def self.dynamic?
101
- # don't optimise this to @dynamic ||= true, because it will reset
102
- # an @dynamic of false to true
103
- @dynamic = true if @dynamic.nil?
104
- @dynamic
105
- end
106
-
107
- # Originally from Jim Freeze's article. Add the accessor methods if
108
- # they don't already exist, and if dsl_dynamic is in effect, which is
109
- # the default. If dsl_static is in effect, the normal method_missing
110
- # behaviour will be invoked.
111
- def method_missing(sym, *args)
112
- if self.class.dynamic?
113
- self.class.dsl_accessor sym
114
- send( sym, *args )
115
- else
116
- super
117
- end
118
- end
119
-
120
- end
@@ -1,180 +0,0 @@
1
- require File.dirname(__FILE__) + '/test_helper'
2
-
3
- class OtherThing < HashCollector
4
- dsl_static
5
- dsl_accessor :thing, :other
6
- end
7
-
8
- class BuildOn < HashCollector
9
- dsl_accessor :ook
10
- end
11
-
12
- class TestHashCollector < Test::Unit::TestCase
13
- def setup
14
- @hash = { :colour => :red, :hue => 15 }
15
- @collected_hash = { :saturation => 17, :opacity => 0.43, :grooviness => 100 }
16
- @full_hash = @hash.merge( @collected_hash )
17
- @collector = HashCollector.new
18
- end
19
-
20
- def teardown
21
- end
22
-
23
- context 'static hash collector' do
24
- setup do
25
- @collector = OtherThing.new
26
- end
27
-
28
- should "throw NoMethodError on collection" do
29
- assert_raise( NoMethodError ) do
30
- @collector.collect { thing 2; blah 3 }
31
- end
32
- assert_raise( NoMethodError ) do
33
- @collector.collect {|c| c.blah = 3 }
34
- end
35
- assert_raise( NoMethodError ) { @collector.blah }
36
- assert_raise( NoMethodError ) { @collector.blah = 5 }
37
- end
38
-
39
- should "understand defined attributes" do
40
- assert_nothing_raised do
41
- @collector.collect do
42
- thing 1
43
- other 2
44
- end
45
- end
46
-
47
- assert_nothing_raised { @collector.thing = 2 }
48
- assert_nothing_raised { @collector.other = 4 }
49
- end
50
-
51
- should "be dynamic" do
52
- assert BuildOn.dynamic?
53
- end
54
- end
55
-
56
- def assert_collected( collector )
57
- assert_equal 17, collector.saturation
58
- assert_equal 0.43, collector.opacity
59
- assert_equal 100, collector.grooviness
60
- end
61
-
62
- def assert_hashed( collector )
63
- assert_equal :red, collector.colour
64
- assert_equal 15, collector.hue
65
- end
66
-
67
- should "collect from a hash" do
68
- @collector.collect( @hash )
69
- assert_equal @hash, @collector.to_hash
70
- end
71
-
72
- should 'not fail on a nil hash' do
73
- @collector.collect nil
74
- assert_equal 0, @collector.to_hash.size
75
- end
76
-
77
- should 'collect from a block, with dsl setters' do
78
- @collector.collect do
79
- saturation 17
80
- opacity 0.43
81
- grooviness 100
82
- end
83
- assert_collected( @collector )
84
- assert_equal @collected_hash, @collector.to_hash
85
- end
86
-
87
- should 'collect from a block, with = setters' do
88
- @collector.collect do |hc|
89
- hc.saturation = 17
90
- hc.opacity = 0.43
91
- hc.grooviness = 100
92
- end
93
- assert_collected( @collector )
94
- assert_equal @collected_hash, @collector.to_hash
95
- end
96
-
97
- should 'collect from a hash and block, with dsl setters' do
98
- @collector.collect( @hash ) do
99
- saturation 17
100
- opacity 0.43
101
- grooviness 100
102
- end
103
- assert_collected( @collector )
104
- assert_hashed( @collector )
105
- assert_equal @full_hash, @collector.to_hash
106
- end
107
-
108
- should 'collect from a hash and block with = setters' do
109
- @collector.collect( @hash ) do |hc|
110
- hc.saturation = 17
111
- hc.opacity = 0.43
112
- hc.grooviness = 100
113
- end
114
- assert_collected( @collector )
115
- assert_hashed( @collector )
116
- assert_equal @full_hash, @collector.to_hash
117
- end
118
-
119
- should 'not fail construction with a nil hash' do
120
- collector = HashCollector.new nil
121
- assert_equal 0, @collector.to_hash.size
122
- end
123
-
124
- should 'construct from a hash and block, with dsl setters' do
125
- collector = HashCollector.new( @hash ) do
126
- saturation 17
127
- opacity 0.43
128
- grooviness 100
129
- end
130
- assert_collected( collector )
131
- assert_hashed( collector )
132
- assert_equal @full_hash, collector.to_hash
133
- end
134
-
135
- should 'construct from a hash and bslock with = setters' do
136
- collector = HashCollector.new( @hash ) do |hc|
137
- hc.saturation = 17
138
- hc.opacity = 0.43
139
- hc.grooviness = 100
140
- end
141
- assert_collected( collector )
142
- assert_hashed( collector )
143
- assert_equal @full_hash, collector.to_hash
144
- end
145
-
146
- should 'not conflict with Object methods' do
147
- hc = HashCollector.new do
148
- display 'something'
149
- sample "This is a long string designed to test the code to the very limits of possibility."
150
- end
151
-
152
- assert_equal 'something', hc.display
153
- assert_equal "This is a long string designed to test the code to the very limits of possibility.", hc.sample
154
- end
155
-
156
- should 'work when called with &block instead of a do ... end' do
157
- options = gather_block do
158
- display 'activity'
159
- order 'lower(activity)'
160
- sample 'Troubleshooting'
161
- conditions 'active = true'
162
- end
163
- assert_equal 'activity', options[:display]
164
- end
165
-
166
- should 'work when called with options and &block instead of a do ... end' do
167
- options = gather_block :blah => 'ook' do
168
- display 'activity'
169
- order 'lower(activity)'
170
- sample 'Troubleshooting'
171
- conditions 'active = true'
172
- end
173
- assert_equal 'activity', options[:display]
174
- assert_equal 'ook', options[:blah]
175
- end
176
-
177
- def gather_block( options = {}, &block )
178
- HashCollector.new( options, &block ).to_hash
179
- end
180
- end