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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d156ad2e578a61e6d5b0c35d1c2c3ef3abf1fa78
4
- data.tar.gz: e74817abf9f6e1986d33d837c870f0eb1aa738c8
3
+ metadata.gz: 7f186c4afd56dac50ae839f9bfe6ff1a929be71e
4
+ data.tar.gz: 39bcb7dd73af0bc157c51109e1d7e6dc7d8f56cb
5
5
  SHA512:
6
- metadata.gz: 3a7fe15c3b3b4b9b29229410088c976464ce6d97ffdf4f79dce15c5863fde6764174b323fca141b1fbfb9ba816254f7089e54a2e9b0c4532f478a8f63302bd56
7
- data.tar.gz: c08a3540c7b1b37400310b2e9faac9a77f7c6b4ce439fe8029d4384e0c7429296b0502a6654647260cd7386ec53d4a8b3ac0159bf3d6c57a667fd60e4e819a06
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.
@@ -13,13 +13,15 @@ module Hashme
13
13
 
14
14
  def set_attribute(name, value)
15
15
  property = get_property(name)
16
- if property.nil?
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.
@@ -1,3 +1,3 @@
1
1
  module Hashme
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
@@ -10,62 +10,71 @@ describe Hashme::Properties do
10
10
  Kernel.const_set("Aux", @aux_model)
11
11
  end
12
12
 
13
- before :each do
14
- @model = Class.new do
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
- @obj[:key1] = 'value'
26
- expect(@obj.get_attribute(:key1)).to eql('value')
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
- @obj.set_attribute('key1', 'value1')
33
- @obj.set_attribute(:key2, 'value2')
34
- expect(@obj[:key1]).to eql('value1')
35
- expect(@obj[:key2]).to eql('value2')
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 = @model.send(:properties)[:name]
40
+ property = model.send(:properties)[:name]
40
41
  name = "Fred Flinstone"
41
- expect(property).to receive(:build).with(@obj, name).and_return(name)
42
- @obj.set_attribute(:name, name)
43
- expect(@obj[:name]).to eql(name)
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(@model.properties).to_not be_nil
51
- expect(@model.properties.class).to eql(Hash)
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
- model = Class.new do
64
+ mod = Class.new do
56
65
  include Hashme
57
66
  end
58
- expect(model.properties).to be_empty
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(@model) do
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(@model.properties.keys).to_not include(:surname)
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(@model.properties.length).to eql(1)
85
+ expect(model.properties.length).to eql(1)
77
86
  expect {
78
- @model.property :foobar
87
+ model.property :foobar
79
88
  }.to raise_error(ArgumentError)
80
- expect(@model.properties.length).to eql(1)
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(@model.properties.length).to eql(1)
85
- @model.property :desc, String
86
- expect(@model.properties.length).to eql(2)
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 = @model.properties[:desc]
97
+ prop = model.properties[:desc]
89
98
  expect(prop.class).to eql(Hashme::Property)
90
99
 
91
- expect(@obj).to respond_to(:desc)
92
- expect(@obj).to respond_to(:desc=)
100
+ expect(obj).to respond_to(:desc)
101
+ expect(obj).to respond_to(:desc=)
93
102
 
94
- @obj.desc = "test"
95
- expect(@obj.desc).to eql("test")
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
- @model.property :nickname, String
100
- expect(@obj.nickname).to be_nil
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
- @model.property :name, String, :default => "Sam"
105
- expect(@obj.name).to eql("Sam")
113
+ model.property :name, String, :default => "Sam"
114
+ expect(obj.name).to eql("Sam")
106
115
  end
107
116
 
108
117
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hashme
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Lown