valuable 0.8.4 → 0.8.5

Sign up to get free protection for your applications and to get access to all the features.
data/lib/valuable.rb CHANGED
@@ -77,6 +77,10 @@ class Valuable
77
77
  end
78
78
  end
79
79
 
80
+ def write_attribute(name, value)
81
+ self.attributes[name.to_sym] = value
82
+ end
83
+
80
84
  class << self
81
85
 
82
86
  # Returns an array of the attributes available on this object.
@@ -135,8 +139,8 @@ class Valuable
135
139
 
136
140
  create_setter_for(name, options[:klass])
137
141
 
138
- alias_method options[:alias], name if options[:alias]
139
- alias_method "#{options[:alias]}=", "#{name}=" if options[:alias]
142
+ sudo_alias options[:alias], name if options[:alias]
143
+ sudo_alias "#{options[:alias]}=", "#{name}=" if options[:alias]
140
144
 
141
145
  check_options_validity(name, options)
142
146
  end
@@ -152,43 +156,49 @@ class Valuable
152
156
  when NilClass
153
157
 
154
158
  define_method setter_method do |value|
155
- attributes[attribute] = value
159
+ write_attribute(attribute, value)
156
160
  end
157
161
 
158
162
  when :integer
159
163
 
160
164
  define_method setter_method do |value|
161
165
  value_as_integer = value && value.to_i
162
- attributes[attribute] = value_as_integer
166
+ write_attribute(attribute, value_as_integer)
163
167
  end
164
168
 
165
169
  when :string
166
170
 
167
171
  define_method setter_method do |value|
168
172
  value_as_string = value && value.to_s
169
- attributes[attribute] = value_as_string
173
+ write_attribute(attribute, value_as_string)
170
174
  end
171
175
 
172
176
  when :boolean
173
177
 
174
178
  define_method setter_method do |value|
175
- attributes[attribute] = value == '0' ? false : !!value
179
+ write_attribute(attribute, value == '0' ? false : !!value )
176
180
  end
177
181
 
178
182
  else
179
183
 
180
184
  define_method setter_method do |value|
181
185
  if value.nil?
182
- attributes[attribute] = nil
186
+ write_attribute(attribute, nil)
183
187
  elsif value.is_a? klass
184
- attributes[attribute] = value
188
+ write_attribute(attribute, value)
185
189
  else
186
- attributes[attribute] = klass.new(value)
190
+ write_attribute(attribute, klass.new(value))
187
191
  end
188
192
  end
189
193
  end
190
194
  end
191
195
 
196
+ def sudo_alias( alias_name, method_name )
197
+ define_method alias_name do |*atts|
198
+ send(method_name, *atts)
199
+ end
200
+ end
201
+
192
202
  # creates a simple accessor method named after the attribute whose
193
203
  # value it will provide during the life of the instance.
194
204
  def create_accessor_for(name)
data/test/alias_test.rb CHANGED
@@ -8,6 +8,19 @@ class Software < Valuable
8
8
  has_value :enterprise_namespace, :alias => 'EnterpriseNamespace'
9
9
  end
10
10
 
11
+ class BackwardDay < Valuable
12
+ has_value :name, :alias => 'nickname'
13
+ has_value :crazies, :alias => 'funkitated'
14
+
15
+ def name=(value)
16
+ attributes[:name] = value.reverse
17
+ end
18
+
19
+ def crazies=(value, value2)
20
+ attributes[:crazies] = "#{value2.reverse} #{value1.reverse}"
21
+ end
22
+ end
23
+
11
24
  class AliasTest < Test::Unit::TestCase
12
25
 
13
26
  def test_that_values_can_be_set_using_their_alias
@@ -24,4 +37,9 @@ class AliasTest < Test::Unit::TestCase
24
37
  software = Software.new(:title => 'ObtrusiveJavascriptComponent')
25
38
  assert_equal 'ObtrusiveJavascriptComponent', software.name
26
39
  end
40
+
41
+ def test_that_overridden_setters_are_not_overlooked
42
+ assert_equal 'rabuf', BackwardDay.new(:nickname => 'fubar').name
43
+ end
27
44
  end
45
+
@@ -0,0 +1,24 @@
1
+ $: << File.expand_path(File.dirname(__FILE__) + '/../lib')
2
+
3
+ require 'test/unit'
4
+ require 'valuable.rb'
5
+
6
+ class Beer < Valuable
7
+ has_value :name
8
+ end
9
+
10
+ class AliasTest < Test::Unit::TestCase
11
+
12
+ def test_that_values_can_be_set_using_write_attribute
13
+ beer = Beer.new
14
+ beer.write_attribute(:name, 'Red Stripe')
15
+ assert_equal 'Red Stripe', beer.name
16
+ end
17
+
18
+ def test_that_values_can_be_set_using_stringified_attribute
19
+ beer = Beer.new
20
+ beer.write_attribute('name', 'Fosters')
21
+ assert_equal 'Fosters', beer.name
22
+ end
23
+ end
24
+
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: valuable
3
3
  version: !ruby/object:Gem::Version
4
- hash: 55
4
+ hash: 53
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 8
9
- - 4
10
- version: 0.8.4
9
+ - 5
10
+ version: 0.8.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - Johnathon Wright
@@ -33,6 +33,7 @@ files:
33
33
  - rakefile.rb
34
34
  - test/bad_attributes_test.rb
35
35
  - test/alias_test.rb
36
+ - test/write_and_read_attribute_test.rb
36
37
  - test/inheritance_test.rb
37
38
  - test/deprecated_test.rb
38
39
  - test/valuable_test.rb
@@ -73,6 +74,7 @@ summary: attr_accessor on steroids with defaults, constructor, and light casting
73
74
  test_files:
74
75
  - test/bad_attributes_test.rb
75
76
  - test/alias_test.rb
77
+ - test/write_and_read_attribute_test.rb
76
78
  - test/inheritance_test.rb
77
79
  - test/deprecated_test.rb
78
80
  - test/valuable_test.rb