va 0.2.0 → 0.3.0
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/lib/va.rb +12 -6
- data/lib/va/version.rb +1 -1
- data/spec/va_spec.rb +27 -4
- 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: 0bf31c73ebe433e7c002db7ab59834a2ad6c87ae
|
4
|
+
data.tar.gz: 14def0ad67e9cf393772f7d450c6b53654586718
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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.
|
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
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
|
-
@
|
19
|
-
@
|
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
|
-
@
|
29
|
-
@
|
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.
|
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
|
+
date: 2014-12-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|