hashme 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +5 -0
- data/lib/hashme/properties.rb +5 -3
- data/lib/hashme/version.rb +1 -1
- data/spec/hashme/properties_spec.rb +45 -36
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7f186c4afd56dac50ae839f9bfe6ff1a929be71e
|
4
|
+
data.tar.gz: 39bcb7dd73af0bc157c51109e1d7e6dc7d8f56cb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7066de18c1fcb6173e5bbbd5bcf29bbccd6916b95da7862636731ac0886a8ab54aa9689ab763fb318413a966fac33723b0880f3d8621b5773107c4d139c21184
|
7
|
+
data.tar.gz: 9f5dee6ff7e5c3c0821bd1953f058f54b4cbd62a466d51dfb72ab235eb26cfa62788755464b3fec5b3a3644dc23116934a7e9d138b16a560e8a9b8d9cc247105
|
data/README.md
CHANGED
@@ -118,6 +118,11 @@ u.errors.first # [:email, "can't be blank"]
|
|
118
118
|
|
119
119
|
## History
|
120
120
|
|
121
|
+
### 0.2.2 - 2016-06-04
|
122
|
+
|
123
|
+
* Removing support for setting attributes without a property.
|
124
|
+
* Adding a `attributes=` method.
|
125
|
+
|
121
126
|
### 0.2.1 - 2016-06-03
|
122
127
|
|
123
128
|
* Support for embedded document validation.
|
data/lib/hashme/properties.rb
CHANGED
@@ -13,13 +13,15 @@ module Hashme
|
|
13
13
|
|
14
14
|
def set_attribute(name, value)
|
15
15
|
property = get_property(name)
|
16
|
-
if property
|
17
|
-
self[name.to_sym] = value
|
18
|
-
else
|
16
|
+
if property
|
19
17
|
self[property.name] = property.build(self, value)
|
20
18
|
end
|
21
19
|
end
|
22
20
|
|
21
|
+
def attributes=(attrs = {})
|
22
|
+
set_attributes(attrs)
|
23
|
+
end
|
24
|
+
|
23
25
|
protected
|
24
26
|
|
25
27
|
# Go through each property and make sure it has a default value.
|
data/lib/hashme/version.rb
CHANGED
@@ -10,62 +10,71 @@ describe Hashme::Properties do
|
|
10
10
|
Kernel.const_set("Aux", @aux_model)
|
11
11
|
end
|
12
12
|
|
13
|
-
|
14
|
-
|
13
|
+
let :model do
|
14
|
+
Class.new do
|
15
15
|
include Hashme
|
16
16
|
property :name, String
|
17
17
|
end
|
18
|
-
|
19
|
-
@obj = @model.new
|
20
18
|
end
|
21
19
|
|
20
|
+
let :obj do
|
21
|
+
model.new
|
22
|
+
end
|
22
23
|
|
23
24
|
describe "#get_attribute" do
|
24
25
|
it "should provide object in model" do
|
25
|
-
|
26
|
-
expect(
|
26
|
+
obj[:key1] = 'value'
|
27
|
+
expect(obj.get_attribute(:key1)).to eql('value')
|
27
28
|
end
|
28
29
|
end
|
29
30
|
|
30
31
|
describe "#set_attribute" do
|
31
|
-
it "should be posible to set attribute not defined as property" do
|
32
|
-
|
33
|
-
|
34
|
-
expect(
|
35
|
-
expect(
|
32
|
+
it "should NOT be posible to set attribute not defined as property" do
|
33
|
+
obj.set_attribute('key1', 'value1')
|
34
|
+
obj.set_attribute(:key2, 'value2')
|
35
|
+
expect(obj[:key1]).to be_nil
|
36
|
+
expect(obj[:key2]).to be_nil
|
36
37
|
end
|
37
38
|
|
38
39
|
it "should set and cast attribute with property" do
|
39
|
-
property =
|
40
|
+
property = model.send(:properties)[:name]
|
40
41
|
name = "Fred Flinstone"
|
41
|
-
expect(property).to receive(:build).with(
|
42
|
-
|
43
|
-
expect(
|
42
|
+
expect(property).to receive(:build).with(obj, name).and_return(name)
|
43
|
+
obj.set_attribute(:name, name)
|
44
|
+
expect(obj[:name]).to eql(name)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe "#attribtues=" do
|
49
|
+
it "should set attributes on the model" do
|
50
|
+
expect(obj.name).to be_nil
|
51
|
+
obj.attributes = { name: 'Sam' }
|
52
|
+
expect(obj.name).to eql("Sam")
|
44
53
|
end
|
45
54
|
end
|
46
55
|
|
47
56
|
describe ".properties" do
|
48
57
|
|
49
58
|
it "should be instantiated after property set" do
|
50
|
-
expect(
|
51
|
-
expect(
|
59
|
+
expect(model.properties).to_not be_nil
|
60
|
+
expect(model.properties.class).to eql(Hash)
|
52
61
|
end
|
53
62
|
|
54
63
|
it "should be empty if no properties" do
|
55
|
-
|
64
|
+
mod = Class.new do
|
56
65
|
include Hashme
|
57
66
|
end
|
58
|
-
expect(
|
67
|
+
expect(mod.properties).to be_empty
|
59
68
|
end
|
60
69
|
|
61
70
|
it "should be inherited from parent models" do
|
62
|
-
mod = Class.new(
|
71
|
+
mod = Class.new(model) do
|
63
72
|
property :surname, String
|
64
73
|
end
|
65
74
|
expect(mod.properties.keys).to include(:name)
|
66
75
|
expect(mod.properties.keys).to include(:surname)
|
67
76
|
# Make sure we don't update the parent!
|
68
|
-
expect(
|
77
|
+
expect(model.properties.keys).to_not include(:surname)
|
69
78
|
end
|
70
79
|
|
71
80
|
end
|
@@ -73,36 +82,36 @@ describe Hashme::Properties do
|
|
73
82
|
describe ".property" do
|
74
83
|
|
75
84
|
it "should fail if no type is defined" do
|
76
|
-
expect(
|
85
|
+
expect(model.properties.length).to eql(1)
|
77
86
|
expect {
|
78
|
-
|
87
|
+
model.property :foobar
|
79
88
|
}.to raise_error(ArgumentError)
|
80
|
-
expect(
|
89
|
+
expect(model.properties.length).to eql(1)
|
81
90
|
end
|
82
91
|
|
83
92
|
it "should create a new property with helper methods" do
|
84
|
-
expect(
|
85
|
-
|
86
|
-
expect(
|
93
|
+
expect(model.properties.length).to eql(1)
|
94
|
+
model.property :desc, String
|
95
|
+
expect(model.properties.length).to eql(2)
|
87
96
|
|
88
|
-
prop =
|
97
|
+
prop = model.properties[:desc]
|
89
98
|
expect(prop.class).to eql(Hashme::Property)
|
90
99
|
|
91
|
-
expect(
|
92
|
-
expect(
|
100
|
+
expect(obj).to respond_to(:desc)
|
101
|
+
expect(obj).to respond_to(:desc=)
|
93
102
|
|
94
|
-
|
95
|
-
expect(
|
103
|
+
obj.desc = "test"
|
104
|
+
expect(obj.desc).to eql("test")
|
96
105
|
end
|
97
106
|
|
98
107
|
it "should return nil on property with no default" do
|
99
|
-
|
100
|
-
expect(
|
108
|
+
model.property :nickname, String
|
109
|
+
expect(obj.nickname).to be_nil
|
101
110
|
end
|
102
111
|
|
103
112
|
it "should create helper method with support for default values" do
|
104
|
-
|
105
|
-
expect(
|
113
|
+
model.property :name, String, :default => "Sam"
|
114
|
+
expect(obj.name).to eql("Sam")
|
106
115
|
end
|
107
116
|
|
108
117
|
end
|