hashme 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +13 -0
- data/lib/hashme.rb +3 -2
- data/lib/hashme/properties.rb +7 -1
- data/lib/hashme/validations.rb +21 -0
- data/lib/hashme/validations/casted_attribute_validator.rb +13 -0
- data/lib/hashme/version.rb +1 -1
- data/spec/hashme/validations_spec.rb +63 -0
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d156ad2e578a61e6d5b0c35d1c2c3ef3abf1fa78
|
4
|
+
data.tar.gz: e74817abf9f6e1986d33d837c870f0eb1aa738c8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3a7fe15c3b3b4b9b29229410088c976464ce6d97ffdf4f79dce15c5863fde6764174b323fca141b1fbfb9ba816254f7089e54a2e9b0c4532f478a8f63302bd56
|
7
|
+
data.tar.gz: c08a3540c7b1b37400310b2e9faac9a77f7c6b4ce439fe8029d4384e0c7429296b0502a6654647260cd7386ec53d4a8b3ac0159bf3d6c57a667fd60e4e819a06
|
data/README.md
CHANGED
@@ -8,6 +8,15 @@ generate hashes that you can serialize and store as you wish.
|
|
8
8
|
Its a bit like ActiveRecord, but without all the messy database stuff,
|
9
9
|
and of course you can nest to you're heart's content.
|
10
10
|
|
11
|
+
A few situations where you might find Hashme useful:
|
12
|
+
|
13
|
+
* parsing incoming data from an API,
|
14
|
+
* defining documents that will be stored and reloaded from a file system, or,
|
15
|
+
* using a "json" field in a relational database with a bit more structure.
|
16
|
+
|
17
|
+
Hashme adheres to ActiveModel and includes support validation, even of nested
|
18
|
+
attributes with a `valid?` method.
|
19
|
+
|
11
20
|
## Installation
|
12
21
|
|
13
22
|
Add this line to your application's Gemfile:
|
@@ -109,6 +118,10 @@ u.errors.first # [:email, "can't be blank"]
|
|
109
118
|
|
110
119
|
## History
|
111
120
|
|
121
|
+
### 0.2.1 - 2016-06-03
|
122
|
+
|
123
|
+
* Support for embedded document validation.
|
124
|
+
|
112
125
|
### 0.2.0 - 2016-06-02
|
113
126
|
|
114
127
|
* Added support for advanced type casting, copy stuff from CouchRest Model.
|
data/lib/hashme.rb
CHANGED
@@ -17,17 +17,18 @@ require "hashme/casted_array"
|
|
17
17
|
require "hashme/properties"
|
18
18
|
require "hashme/property"
|
19
19
|
require "hashme/property_casting"
|
20
|
+
require "hashme/validations/casted_attribute_validator"
|
21
|
+
require "hashme/validations"
|
20
22
|
|
21
23
|
module Hashme
|
22
24
|
extend ActiveSupport::Concern
|
23
25
|
|
24
|
-
include
|
26
|
+
include Validations
|
25
27
|
|
26
28
|
included do
|
27
29
|
include Castable
|
28
30
|
include Attributes
|
29
31
|
include Properties
|
30
|
-
# Eventually! include Validations
|
31
32
|
end
|
32
33
|
|
33
34
|
def initialize(attrs = {})
|
data/lib/hashme/properties.rb
CHANGED
@@ -52,7 +52,7 @@ module Hashme
|
|
52
52
|
property = Property.new(*args)
|
53
53
|
self.properties = properties.merge(property.name => property)
|
54
54
|
define_property_methods(property)
|
55
|
-
|
55
|
+
prepare_validation(property)
|
56
56
|
property
|
57
57
|
end
|
58
58
|
|
@@ -69,6 +69,12 @@ module Hashme
|
|
69
69
|
end
|
70
70
|
end
|
71
71
|
|
72
|
+
def prepare_validation(property)
|
73
|
+
if property.type.method_defined?(:valid?)
|
74
|
+
validates_casted_attributes property.name
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
72
78
|
end
|
73
79
|
|
74
80
|
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Hashme
|
2
|
+
|
3
|
+
# Additional functionality for validation outside the standard ActiveModel stuff.
|
4
|
+
module Validations
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
include ActiveModel::Validations
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
|
10
|
+
# Validates the associated casted model. This method should not be
|
11
|
+
# used within your code as it is automatically included when a CastedModel
|
12
|
+
# is used inside the model.
|
13
|
+
def validates_casted_attributes(*args)
|
14
|
+
validates_with(CastedAttributeValidator, _merge_attributes(args))
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module Hashme
|
2
|
+
module Validations
|
3
|
+
class CastedAttributeValidator < ActiveModel::EachValidator
|
4
|
+
|
5
|
+
def validate_each(document, attribute, value)
|
6
|
+
values = value.is_a?(Array) ? value : [value]
|
7
|
+
return if values.collect {|attr| attr.nil? || attr.valid? }.all?
|
8
|
+
document.errors.add(attribute)
|
9
|
+
end
|
10
|
+
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/lib/hashme/version.rb
CHANGED
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Hashme::Validations do
|
4
|
+
|
5
|
+
context "with simple validation" do
|
6
|
+
subject do
|
7
|
+
Class.new do
|
8
|
+
include Hashme
|
9
|
+
def self.name; "Example"; end
|
10
|
+
property :name, String
|
11
|
+
validates :name, presence: true
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should not fail when valid" do
|
16
|
+
obj = subject.new(name: "Sam")
|
17
|
+
expect(obj).to be_valid
|
18
|
+
expect(obj.errors).to be_empty
|
19
|
+
end
|
20
|
+
it "should add error when invalid" do
|
21
|
+
obj = subject.new
|
22
|
+
expect(obj).to_not be_valid
|
23
|
+
expect(obj.errors).to_not be_empty
|
24
|
+
expect(obj.errors[:name]).to_not be_empty
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context "with embedded object that supports validation" do
|
29
|
+
subject do
|
30
|
+
submod = Class.new do
|
31
|
+
include Hashme
|
32
|
+
def self.name; "SubExample"; end
|
33
|
+
property :email, String
|
34
|
+
validates :email, presence: true
|
35
|
+
end
|
36
|
+
Class.new do
|
37
|
+
include Hashme
|
38
|
+
def self.name; "Example"; end
|
39
|
+
property :sub, submod
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should not error when valid" do
|
44
|
+
obj = subject.new(sub: {email: 'sam@cabify.com'})
|
45
|
+
expect(obj).to be_valid
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should not err if embedded object empty" do
|
49
|
+
obj = subject.new()
|
50
|
+
expect(obj).to be_valid
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should provide errors when invalid" do
|
54
|
+
obj = subject.new(sub: {})
|
55
|
+
expect(obj).to_not be_valid
|
56
|
+
expect(obj.errors[:sub]).to_not be_empty
|
57
|
+
expect(obj.sub.errors).to_not be_empty
|
58
|
+
expect(obj.sub.errors[:email]).to_not be_empty
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hashme
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sam Lown
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-06-
|
11
|
+
date: 2016-06-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -87,6 +87,8 @@ files:
|
|
87
87
|
- lib/hashme/properties.rb
|
88
88
|
- lib/hashme/property.rb
|
89
89
|
- lib/hashme/property_casting.rb
|
90
|
+
- lib/hashme/validations.rb
|
91
|
+
- lib/hashme/validations/casted_attribute_validator.rb
|
90
92
|
- lib/hashme/version.rb
|
91
93
|
- spec/hashme/attributes_spec.rb
|
92
94
|
- spec/hashme/base_spec.rb
|
@@ -94,6 +96,7 @@ files:
|
|
94
96
|
- spec/hashme/properties_spec.rb
|
95
97
|
- spec/hashme/property_casting_spec.rb
|
96
98
|
- spec/hashme/property_spec.rb
|
99
|
+
- spec/hashme/validations_spec.rb
|
97
100
|
- spec/spec_helper.rb
|
98
101
|
homepage: ''
|
99
102
|
licenses:
|
@@ -126,4 +129,5 @@ test_files:
|
|
126
129
|
- spec/hashme/properties_spec.rb
|
127
130
|
- spec/hashme/property_casting_spec.rb
|
128
131
|
- spec/hashme/property_spec.rb
|
132
|
+
- spec/hashme/validations_spec.rb
|
129
133
|
- spec/spec_helper.rb
|