valuable 0.9.3 → 0.9.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +15 -2
- data/lib/valuable.rb +4 -4
- data/test/custom_initializer_test.rb +26 -0
- data/valuable.version +1 -1
- metadata +5 -4
data/README.markdown
CHANGED
@@ -5,7 +5,7 @@ Valuable enables quick modeling... it's attr_accessor on steroids. Its simple i
|
|
5
5
|
|
6
6
|
Valuable provides DRY decoration like attr_accessor, but includes default values and other formatting (like, "2" => 2), and a constructor that accepts an attributes hash. It provides a class-level list of attributes, an instance-level attributes hash, and more.
|
7
7
|
|
8
|
-
Tested with
|
8
|
+
Tested with [Rubinius](http://www.rubini.us "Rubinius"), 1.8.7, 1.9.1, 1.9.2, 1.9.3
|
9
9
|
|
10
10
|
Frequent Uses
|
11
11
|
-------------
|
@@ -117,7 +117,6 @@ _aliases_
|
|
117
117
|
>> xp.name
|
118
118
|
=> "Windows XP"
|
119
119
|
|
120
|
-
|
121
120
|
Formatting Input
|
122
121
|
----------------
|
123
122
|
_aka light-weight type-casting_
|
@@ -217,6 +216,20 @@ Advanced Defaults
|
|
217
216
|
>> seven.designation
|
218
217
|
=> '7 of 9'
|
219
218
|
|
219
|
+
Note -- if you overwrite the constructor, you should call initialize_attributes. Otherwise, your default values won't be set up until the first time the attributes hash is called -- in theory, this could be well after initialization, and could cause unknowable gremlins. Trivial example:
|
220
|
+
|
221
|
+
class Person
|
222
|
+
has_value :created_at, :default => lambda { Time.now }
|
223
|
+
|
224
|
+
def initialize(atts)
|
225
|
+
end
|
226
|
+
end
|
227
|
+
|
228
|
+
>> p = Person.new
|
229
|
+
>> # wait 10 minutes
|
230
|
+
>> p.created_at == Time.now # attributes initialized on first use
|
231
|
+
=> true
|
232
|
+
|
220
233
|
Advanced Input Parsing
|
221
234
|
----------------------
|
222
235
|
|
data/lib/valuable.rb
CHANGED
@@ -41,15 +41,15 @@ class Valuable
|
|
41
41
|
# >> bus.attributes
|
42
42
|
# => {:color => 'yellow', :number => 16}
|
43
43
|
def attributes
|
44
|
-
@attributes
|
45
|
-
end
|
46
|
-
|
47
|
-
def initialize_attributes
|
48
44
|
@attributes ||= Valuable::Utils.initial_copy_of_attributes(self.class.defaults)
|
49
45
|
end
|
46
|
+
alias_method :initialize_attributes, :attributes
|
50
47
|
|
51
48
|
# accepts an optional hash that will be used to populate the
|
52
49
|
# predefined attributes for this class.
|
50
|
+
#
|
51
|
+
# Note: You are free to overwrite the constructor, but you should call
|
52
|
+
# initialize_attributes OR make sure at least one value is stored.
|
53
53
|
def initialize(atts = nil)
|
54
54
|
initialize_attributes
|
55
55
|
self.update_attributes(atts || {})
|
@@ -0,0 +1,26 @@
|
|
1
|
+
$: << File.expand_path(File.dirname(__FILE__) + '/../lib')
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'test/unit'
|
5
|
+
require 'valuable.rb'
|
6
|
+
require 'mocha'
|
7
|
+
|
8
|
+
class Person < Valuable
|
9
|
+
has_value :first_name
|
10
|
+
has_value :last_name
|
11
|
+
|
12
|
+
def initialize(atts={})
|
13
|
+
self.first_name = "Joe"
|
14
|
+
super(atts)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class ParseWithTest < Test::Unit::TestCase
|
19
|
+
|
20
|
+
def test_that_attributes_are_accessible_in_custom_constructor
|
21
|
+
assert_nothing_raised do
|
22
|
+
Person.new(:last_name => 'Smith')
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
data/valuable.version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.9.
|
1
|
+
0.9.4
|
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:
|
4
|
+
hash: 51
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 9
|
9
|
-
-
|
10
|
-
version: 0.9.
|
9
|
+
- 4
|
10
|
+
version: 0.9.4
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Johnathon Wright
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-
|
18
|
+
date: 2012-04-10 00:00:00 -05:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|
@@ -40,6 +40,7 @@ files:
|
|
40
40
|
- lib/valuable/utils.rb
|
41
41
|
- test/alias_test.rb
|
42
42
|
- test/bad_attributes_test.rb
|
43
|
+
- test/custom_initializer_test.rb
|
43
44
|
- test/default_values_from_anon_methods.rb
|
44
45
|
- test/deprecated_test.rb
|
45
46
|
- test/inheritance_test.rb
|