attrs 0.0.1 → 0.0.2
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 +4 -4
- data/README.md +5 -0
- data/VERSION +1 -1
- data/lib/attrs.rb +7 -1
- data/test/attrs_test.rb +12 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ab841e8fc31cdd2ce3e8ab8b7d4eec6cebe8b74a
|
4
|
+
data.tar.gz: c144ca728920eebcf32ffed6be95eea5cc364a84
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 90c611fdf4c48ddad3ebba11394bf94f8f304fc88b19629ee7fca9a6d9052a342e7df66483eedbc57c62467204d30643d2cb37e4f18b0f613a791e27a1e278d0
|
7
|
+
data.tar.gz: d6fa66285ffbdb2568e8b8dcf9c7a2de5d5b58fe1f274afe0ec3b1fa5448adf29bf3b848aabda7b4bc89200b66829f83108c8fcc51aad2d861a23fd332450dbf
|
data/README.md
CHANGED
@@ -28,6 +28,10 @@ require 'attrs'
|
|
28
28
|
class Person < Attrs(:name, :age)
|
29
29
|
private
|
30
30
|
|
31
|
+
def default_name
|
32
|
+
"Anonymous"
|
33
|
+
end
|
34
|
+
|
31
35
|
def age=(new_age)
|
32
36
|
super(new_age.to_i)
|
33
37
|
end
|
@@ -41,6 +45,7 @@ with this code we can:
|
|
41
45
|
* get the attributes hash: `person.attributes # => {:name => "John Doe", :age => 26}`
|
42
46
|
* get the attribute names: `Person.attribute_names # => [:name, :age]`
|
43
47
|
* compare with other objects: `person == {:name => "John Doe", :age => 26} # => true`
|
48
|
+
* Explicitly set default values: `Person.new(age: 26).name # => "Anonymous"
|
44
49
|
|
45
50
|
and more! See: <https://github.com/wojtekmach/attrs/blob/master/test/attrs_test.rb>
|
46
51
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.2
|
data/lib/attrs.rb
CHANGED
@@ -22,7 +22,13 @@ module Attrs
|
|
22
22
|
|
23
23
|
def initialize(attributes)
|
24
24
|
self.class.attribute_names.each do |name|
|
25
|
-
|
25
|
+
send("#{name}=", attributes.fetch(name) {
|
26
|
+
if respond_to?("default_#{name}", true)
|
27
|
+
send "default_#{name}"
|
28
|
+
else
|
29
|
+
raise KeyError, "key not found: #{name.inspect}. Define :default_#{name} for a default value"
|
30
|
+
end
|
31
|
+
})
|
26
32
|
end
|
27
33
|
end
|
28
34
|
|
data/test/attrs_test.rb
CHANGED
@@ -4,6 +4,10 @@ require 'attrs'
|
|
4
4
|
class Person < Attrs(:name, :age)
|
5
5
|
private
|
6
6
|
|
7
|
+
def default_name
|
8
|
+
"Anonymous"
|
9
|
+
end
|
10
|
+
|
7
11
|
def age=(new_age)
|
8
12
|
super(new_age.to_i)
|
9
13
|
end
|
@@ -28,10 +32,14 @@ class PersonTest < Minitest::Spec
|
|
28
32
|
|
29
33
|
it "must be instantiated with all attributes" do
|
30
34
|
proc {
|
31
|
-
klass.new(
|
35
|
+
klass.new(name: 'John')
|
32
36
|
}.must_raise KeyError
|
33
37
|
end
|
34
38
|
|
39
|
+
it "can have default values" do
|
40
|
+
klass.new(age: 26).name.must_equal "Anonymous"
|
41
|
+
end
|
42
|
+
|
35
43
|
it "can access individual attributes" do
|
36
44
|
klass.new(valid_attributes).name.must_equal "John Doe"
|
37
45
|
end
|
@@ -66,6 +74,9 @@ end
|
|
66
74
|
require 'attrs/coercible'
|
67
75
|
|
68
76
|
class Person2 < Attrs(name: String, age: Integer)
|
77
|
+
def default_name
|
78
|
+
"Anonymous"
|
79
|
+
end
|
69
80
|
end
|
70
81
|
|
71
82
|
class Person2Test < PersonTest
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: attrs
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Wojciech Mach
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-08-
|
11
|
+
date: 2013-08-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|