constructable 0.3.3 → 0.3.4

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.
data/README.markdown CHANGED
@@ -168,6 +168,7 @@ macro and still get all the validations and stuff by calling super:
168
168
  class Song
169
169
  constructable :length, accessible: true, validate_type: Integer
170
170
 
171
+
171
172
  def length=(length)
172
173
  if length.is_a?(String) && length =~ /(\d{,2}):(\d{,2})/
173
174
  @length = $1.to_i * 60 + $2.to_i
@@ -177,7 +178,7 @@ class Song
177
178
  end
178
179
  end
179
180
 
180
- song = Song.new(length: '3:10')
181
+ song = Song.new(length: 190)
181
182
  #=> #<Song:0x000001010ea040 @length=190>
182
183
 
183
184
  song.length = '1:30'
@@ -186,27 +187,6 @@ song.length
186
187
 
187
188
  song.length = 'abc'
188
189
  # raises AttributeError, ':length must be of type Integer'
189
- ```
190
-
191
- You can also redefine private setters/getters the constructable
192
- class macro set up for you:
193
-
194
- ```ruby
195
- class Song
196
- constructable :name, validate_type: String
197
- attribute_reader :name_history
198
-
199
- def initialize(opts = {})
200
- @name_history = []
201
- end
202
-
203
- private
204
-
205
- def name=(name)
206
- @name_histoy += name
207
- super
208
- end
209
- end
210
190
 
211
191
  song = Song.new(name: 'Aaron', length: '6:01')
212
192
  #=> #<Song:0x0x00000100941528 @length=190 @name="Aaron" @name_history=["Aaron"]>
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.3
1
+ 0.3.4
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{constructable}
8
- s.version = "0.3.3"
8
+ s.version = "0.3.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Manuel Korfmann"]
12
- s.date = %q{2011-06-18}
12
+ s.date = %q{2011-06-19}
13
13
  s.description = %q{Makes constructing objects through an attributes hash easier}
14
14
  s.email = %q{manu@korfmann.info}
15
15
  s.extra_rdoc_files = [
@@ -34,6 +34,10 @@ module Constructable
34
34
  ('@' + self.name.to_s).to_sym
35
35
  end
36
36
 
37
+ def attr_writer_symbol
38
+ (self.name.to_s + '=').to_sym
39
+ end
40
+
37
41
  def check_for_requirement(requirement, value)
38
42
  if self.send requirement[:name]
39
43
  unless self.instance_exec(value,&requirement[:check])
@@ -47,13 +47,11 @@ module Constructable
47
47
  attributes = @attributes
48
48
  attributes.each do |attribute|
49
49
  @module.module_eval do
50
- attr_reader attribute.name
50
+ attr_reader attribute.name if attribute.readable
51
51
 
52
- define_method(:"#{attribute.name}=") do |value|
52
+ define_method(attribute.attr_writer_symbol) do |value|
53
53
  instance_variable_set attribute.ivar_symbol, attribute.process(value)
54
- end
55
- private attribute.name unless attribute.readable
56
- private :"#{attribute.name}=" unless attribute.writable
54
+ end if attribute.writable
57
55
  end
58
56
  end
59
57
  end
@@ -61,7 +59,7 @@ module Constructable
61
59
  def construct(constructor_hash, obj)
62
60
  constructor_hash ||= {}
63
61
  @attributes.each do |attribute|
64
- obj.send :"#{attribute.name}=", constructor_hash[attribute.name]
62
+ obj.instance_variable_set attribute.ivar_symbol, attribute.process(constructor_hash[attribute.name])
65
63
  end
66
64
  end
67
65
 
@@ -18,7 +18,7 @@ describe 'Attribute' do
18
18
 
19
19
  describe 'process' do
20
20
  it 'intepretes nil but not false as undefined' do
21
- attribute = Attribute.new(:foo, default: true)
21
+ attribute = Attribute.new(:foo, default: ->{true})
22
22
  assert_equal false, attribute.process(false)
23
23
  end
24
24
 
@@ -22,14 +22,6 @@ describe 'Constructor' do
22
22
  end
23
23
 
24
24
  describe 'redefining' do
25
- describe 'constructing a class will call redefined setters' do
26
- it 'calls the redefined setters' do
27
- @klass.constructable :bacon, readable: true
28
- @klass.class_eval { private; def bacon=(value);super(:zomg_bacon);end }
29
- instance = @klass.new(bacon: :no_bacon)
30
- assert_equal :zomg_bacon, instance.bacon
31
- end
32
- end
33
25
 
34
26
  describe 'class' do
35
27
  it 'getters ' do
@@ -96,13 +88,6 @@ describe 'Constructor' do
96
88
  instance.integer = :not_an_integer
97
89
  end
98
90
  end
99
-
100
- it 'sets for private setters/getters' do
101
- @klass.constructable :integer, validate_type: Integer, readable: true
102
- @klass.class_eval { private; def integer=(value); super(value.to_i) ;end }
103
- instance = @klass.new(integer: '5')
104
- assert_equal 5, instance.integer
105
- end
106
91
  end
107
92
  end
108
93
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: constructable
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.3.3
5
+ version: 0.3.4
6
6
  platform: ruby
7
7
  authors:
8
8
  - Manuel Korfmann
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-06-18 00:00:00 +02:00
13
+ date: 2011-06-19 00:00:00 +02:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -100,7 +100,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
100
100
  requirements:
101
101
  - - ">="
102
102
  - !ruby/object:Gem::Version
103
- hash: 335237132407385369
103
+ hash: -4277054694638453062
104
104
  segments:
105
105
  - 0
106
106
  version: "0"