methlab 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/methlab.rb +26 -1
  2. data/test/test_checks.rb +14 -0
  3. metadata +3 -3
data/lib/methlab.rb CHANGED
@@ -55,7 +55,7 @@
55
55
  #
56
56
  module MethLab
57
57
 
58
- VERSION = "0.0.4"
58
+ VERSION = "0.0.5"
59
59
 
60
60
  # Integrates MethLab into all namespaces. It does this by patching itself
61
61
  # into ::main and Module.
@@ -241,6 +241,31 @@ module MethLab
241
241
  method_name
242
242
  end
243
243
 
244
+ # Similar to MethLab#build_ordered, but builds attributes similar to
245
+ # attr_accessor. Takes a single parameter which is the constraint
246
+ # specification.
247
+ #
248
+ # Example:
249
+ #
250
+ # def_attr :set_me, String
251
+ #
252
+ # # later on..
253
+ # myobj.set_me = 0 # raises
254
+ # myobj.set_me = "String" # valid
255
+ #
256
+ def def_attr(method_name, arg)
257
+ self.send(:define_method, (method_name.to_s + "=").to_sym) do |value|
258
+ signature = [arg]
259
+ params = MethLab.validate_array_params(signature, [value])
260
+ raise params if params.kind_of?(Exception)
261
+ send(:instance_variable_set, "@" + method_name.to_s, value)
262
+ end
263
+
264
+ self.send(:define_method, method_name) do
265
+ instance_variable_get("@" + method_name.to_s)
266
+ end
267
+ end
268
+
244
269
  if $METHLAB_AUTOINTEGRATE
245
270
  integrate
246
271
  end
data/test/test_checks.rb CHANGED
@@ -24,6 +24,8 @@ class CheckedClass
24
24
  params
25
25
  end
26
26
 
27
+ def_attr(:set_me, String)
28
+
27
29
  def_ordered(:proc_nil, proc { |x| x.nil? }) do |params|
28
30
  params
29
31
  end
@@ -142,4 +144,16 @@ class TestChecks < Test::Unit::TestCase
142
144
 
143
145
  assert_equal(@checked.proc_nil(nil), [nil])
144
146
  end
147
+
148
+ def test_05_attr
149
+ assert(@checked.respond_to?(:set_me))
150
+
151
+ assert_raises(ArgumentError.new("value of argument '0' is an invalid type. Requires 'String'")) do
152
+ @checked.set_me = 0
153
+ end
154
+
155
+ @checked.set_me = "Foo"
156
+
157
+ assert_equal(@checked.set_me, "Foo")
158
+ end
145
159
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 4
9
- version: 0.0.4
8
+ - 5
9
+ version: 0.0.5
10
10
  platform: ruby
11
11
  authors:
12
12
  - Erik Hollensbe
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-27 00:00:00 -04:00
17
+ date: 2010-05-01 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies: []
20
20