va 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/lib/va.rb +12 -6
  3. data/lib/va/version.rb +1 -1
  4. data/spec/va_spec.rb +27 -4
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0c2d90020330d06782347f0a78de34f49d02649f
4
- data.tar.gz: 7f35f588cda7e35ef2eb675e39bff9031e8496a6
3
+ metadata.gz: 0bf31c73ebe433e7c002db7ab59834a2ad6c87ae
4
+ data.tar.gz: 14def0ad67e9cf393772f7d450c6b53654586718
5
5
  SHA512:
6
- metadata.gz: b0f2484df8044cbadd7a1a4f0e7131246c31bd80c274fae99f8a68c41d78f4d7b00e4c4475a79d610bb42427dcbbf623cd3c9a908c942dbfe09bde9597cef62a
7
- data.tar.gz: cb2e88857bc6ebf2ada32ce9020eefb9903e2852986bd2e40f9714994ae2f8fcd2cf4569638a245e1ebc8f9665ecb2be71c6c3be4604d55bc532dacd1bb155ec
6
+ metadata.gz: 84d8f492bc1b1426b80e9c18147de0ea48fbfdf1cc83f661fecf7fece1a90fb02c3971fa18b1d8cb86e50eeebcbc58f09cef0be783960a5e9bff6d94743c386d
7
+ data.tar.gz: e54e0ee3b0a6ddf4a865da55a7def06dede7f76e67c631a95af19f397aa2294632495ed448201a3474bf4f48573f598a00e366c515b919073f48351b20c4ba7d
data/lib/va.rb CHANGED
@@ -6,7 +6,7 @@ module Va
6
6
  attr_reader :errors
7
7
 
8
8
  def initialize(args={})
9
- @attributes = {}
9
+ @attributes ||= self.class.defaults.dup
10
10
  args.each do |k, v|
11
11
  key = k.to_sym
12
12
  @attributes[key] = v if self.class.keys.include?(key)
@@ -28,7 +28,7 @@ module Va
28
28
  def message(msg="", *attrs)
29
29
  raise __callee__.inspect
30
30
  end
31
-
31
+
32
32
  def valid?
33
33
  @valid
34
34
  end
@@ -55,16 +55,23 @@ module Va
55
55
  end
56
56
  end
57
57
  end
58
-
58
+
59
59
  def self.keys
60
60
  @keys ||= []
61
61
  end
62
62
 
63
- def self.attribute(attr_name)
63
+ def self.defaults
64
+ @defaults ||= {}
65
+ end
66
+
67
+ def self.attribute(attr_name, options={})
64
68
  name = attr_name.to_sym
65
69
 
66
70
  self.keys << name
67
71
 
72
+ default = options.fetch(:default) { NotSpecified }
73
+ self.defaults[name] = default unless default == NotSpecified
74
+
68
75
  define_method "#{name}=" do |value|
69
76
  attributes[name] = value
70
77
  end
@@ -75,6 +82,5 @@ module Va
75
82
  end
76
83
  end
77
84
  class UnknownAttribute < Exception; end
85
+ class NotSpecified ; end
78
86
  end
79
-
80
-
data/lib/va/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Va
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
data/spec/va_spec.rb CHANGED
@@ -15,8 +15,8 @@ scope do
15
15
 
16
16
  scope "#attributes" do
17
17
  spec "all" do
18
- @va = Login.new(email: "fede@example.com", pass: "123456")
19
- @va.attributes == { email: "fede@example.com", pass: "123456" }
18
+ @attributes = Login.new(email: "fede@example.com", pass: "123456").attributes
19
+ @attributes == { email: "fede@example.com", pass: "123456" }
20
20
  end
21
21
 
22
22
  spec "some" do
@@ -25,8 +25,8 @@ scope do
25
25
  end
26
26
 
27
27
  spec "spureous" do
28
- @va = Login.new(email: "fede@example.com", i_dont_belong_here: "HELLO!")
29
- @va.attributes == { email: "fede@example.com" }
28
+ @attributes = Login.new(email: "fede@example.com", i_dont_belong_here: "HELLO!").attributes
29
+ @attributes == { email: "fede@example.com" }
30
30
  end
31
31
  end
32
32
  end
@@ -91,3 +91,26 @@ scope "custom validations" do
91
91
  end
92
92
  end
93
93
  end
94
+
95
+ scope "default values" do
96
+ class MyDefaults < Va::Model
97
+ attribute :name, default: "N/A"
98
+ attribute :age
99
+ end
100
+
101
+ spec do
102
+ @attributes = MyDefaults.new(age: 30).attributes
103
+ @attributes == {name: "N/A", age: 30}
104
+ end
105
+
106
+ class BooleanDefaults < Va::Model
107
+ attribute :me_true, default: true
108
+ attribute :me_false, default: false
109
+ attribute :not_me
110
+ end
111
+
112
+ spec do
113
+ @attributes = BooleanDefaults.new.attributes
114
+ @attributes == {me_true: true, me_false: false}
115
+ end
116
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: va
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Federico Iachetti
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-28 00:00:00.000000000 Z
11
+ date: 2014-12-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler