attrs 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +5 -0
  3. data/VERSION +1 -1
  4. data/lib/attrs.rb +7 -1
  5. data/test/attrs_test.rb +12 -1
  6. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 831e360a852a5219f76a80451787e234b1d2945c
4
- data.tar.gz: c19032b5e3cf6888752e6068efb0770422d77a27
3
+ metadata.gz: ab841e8fc31cdd2ce3e8ab8b7d4eec6cebe8b74a
4
+ data.tar.gz: c144ca728920eebcf32ffed6be95eea5cc364a84
5
5
  SHA512:
6
- metadata.gz: 793ca776c058b0577bec1fc744f4283087b5350371c39d5fd94ec892a385c4820f3c7a4b686ac613e7bf8d6733ab59576f8261a6f14b567ca32c460470816bec
7
- data.tar.gz: 00dc67d08c02a0a3aad66d5adb916dca0115eaf773991035acb2d1bac0c0ad73e6d0fa62c40d436f75e1ec5c0fe50e6b723162d7f51bc619fc8fbdbf2343b22c
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
1
+ 0.0.2
@@ -22,7 +22,13 @@ module Attrs
22
22
 
23
23
  def initialize(attributes)
24
24
  self.class.attribute_names.each do |name|
25
- self.send("#{name}=", attributes.fetch(name))
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
 
@@ -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(age: 26)
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.1
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-10 00:00:00.000000000 Z
11
+ date: 2013-08-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler