validated_accessors 0.0.1 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 357f2ee502fc0d2d6f360f9c261c13856365c5a5
4
- data.tar.gz: 083b1f888d44e19bc447f65c230cf474bec41057
3
+ metadata.gz: 0a6c4ee83467065bfbdaa71fac141d0161f21069
4
+ data.tar.gz: fa715ffcb81ff64b6ec60e993977d1f22948f156
5
5
  SHA512:
6
- metadata.gz: ad06285c6586a1ad7a3e6de74f9fc872bef1f1172badfa4801779584acef54a0e248a94ca0a3ea53e6cc6c0658b166865c53bea5743f11dcfa9ce7f433c3a51e
7
- data.tar.gz: bc2708d04dcd116e1ebcfa0fb64c680c28a987939f9093378efc6459b66e638e39b8c5d675e56f12c38730c3bea0bd705262b296b36155c63c833a26eb7f4cf2
6
+ metadata.gz: bebc63e716e077246b5d33de7fb50b470bd940efd8a19ab1778435ead76c3a3f485bf7fbac0e966ec7f902c8cc15d756eac9bb347a7de597a8c1e8317f566653
7
+ data.tar.gz: 6980ab432f0855c97d5b566de2be7337cde3f16fd59ca8f5d010d46b1b5d9437b57b0e51ed19747b75d704b422f28d52bf2fd8e92c4fc0df7922d8305594737f
@@ -2,18 +2,31 @@
2
2
 
3
3
  module ValidatedAccessors
4
4
 
5
- VERSION = "0.0.1"
5
+ VERSION = "0.1.1"
6
6
 
7
- def validated_accessor attribute, options
7
+ def validated_accessor attribute, options, &block
8
+ define_attribute_reader attribute
9
+ define_attribute_writter attribute, options, &block
10
+ end
11
+
12
+ private
8
13
 
14
+ def define_attribute_reader attribute
9
15
  define_method attribute do
10
16
  instance_variable_get "@#{ attribute }"
11
17
  end
18
+ end
12
19
 
20
+ def define_attribute_writter attribute, options, &block
13
21
  define_method "#{ attribute }=" do |value|
22
+ value = yield value if block_given?
23
+ unless options[:valid].include? value
24
+ raise ArgumentError,
25
+ "Invalid value: #{ value } for #{ attribute }."\
26
+ "(Valid values are: #{ options[:valid] })"
27
+ end
14
28
  instance_variable_set "@#{ attribute }", value
15
29
  end
16
-
17
30
  end
18
31
 
19
32
  end
@@ -2,6 +2,7 @@ setup do
2
2
  class Foo
3
3
  extend ValidatedAccessors
4
4
  validated_accessor :bar, valid: [:two, :three]
5
+ validated_accessor :sym_bar, valid: [:two, :three] { |v| v.to_sym }
5
6
  end
6
7
  Foo.new
7
8
  end
@@ -14,3 +15,14 @@ test 'let you assign valid values' do |foo|
14
15
  foo.bar = :two
15
16
  assert foo.bar == :two
16
17
  end
18
+
19
+ test 'raise when you use assign invalid values' do |foo|
20
+ assert_raise ArgumentError do
21
+ foo.bar = :invalid
22
+ end
23
+ end
24
+
25
+ test 'enforce the type provided' do |foo|
26
+ foo.sym_bar = 'two'
27
+ assert foo.sym_bar == :two
28
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validated_accessors
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eloy Espinaco