hashcast 0.4.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 +7 -0
- data/.gitignore +8 -0
- data/.travis.yml +3 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +50 -0
- data/LICENSE.txt +22 -0
- data/README.md +125 -0
- data/Rakefile +1 -0
- data/benchmark/benchmark.rb +66 -0
- data/benchmark/casters.rb +50 -0
- data/docs/_config.yml +1 -0
- data/docs/index.md +124 -0
- data/hcast.gemspec +27 -0
- data/lib/hashcast.rb +46 -0
- data/lib/hashcast/attributes_caster.rb +97 -0
- data/lib/hashcast/attributes_parser.rb +63 -0
- data/lib/hashcast/caster.rb +144 -0
- data/lib/hashcast/casters.rb +13 -0
- data/lib/hashcast/casters/array_caster.rb +27 -0
- data/lib/hashcast/casters/boolean_caster.rb +13 -0
- data/lib/hashcast/casters/date_caster.rb +15 -0
- data/lib/hashcast/casters/datetime_caster.rb +15 -0
- data/lib/hashcast/casters/float_caster.rb +14 -0
- data/lib/hashcast/casters/hash_caster.rb +8 -0
- data/lib/hashcast/casters/integer_caster.rb +15 -0
- data/lib/hashcast/casters/string_caster.rb +8 -0
- data/lib/hashcast/casters/symbol_caster.rb +15 -0
- data/lib/hashcast/casters/time_caster.rb +15 -0
- data/lib/hashcast/concern.rb +136 -0
- data/lib/hashcast/config.rb +11 -0
- data/lib/hashcast/errors.rb +49 -0
- data/lib/hashcast/metadata/attribute.rb +30 -0
- data/lib/hashcast/version.rb +3 -0
- data/spec/hcast/caster_spec.rb +471 -0
- data/spec/hcast/casters_spec.rb +245 -0
- data/spec/hcast/hcast_spec.rb +37 -0
- data/spec/spec_helper.rb +21 -0
- metadata +169 -0
@@ -0,0 +1,245 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Casters" do
|
4
|
+
context HashCast::Casters::ArrayCaster do
|
5
|
+
def cast(v)
|
6
|
+
HashCast::Casters::ArrayCaster.cast(v, :attr_name)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "works with real values" do
|
10
|
+
expect(cast([true])).to eq([true])
|
11
|
+
end
|
12
|
+
|
13
|
+
it "works with given caster" do
|
14
|
+
expect(
|
15
|
+
HashCast::Casters::ArrayCaster.cast(["true"], :attr_name, {each: :boolean})
|
16
|
+
).to eq([true])
|
17
|
+
end
|
18
|
+
|
19
|
+
it "raises else with wrong values" do
|
20
|
+
expect{
|
21
|
+
cast("something")
|
22
|
+
}.to raise_error(HashCast::Errors::CastingError)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "raises when given caster does not exist" do
|
26
|
+
expect{
|
27
|
+
HashCast::Casters::ArrayCaster.cast(["true"], :attr_name, {each: :booleans})
|
28
|
+
}.to raise_error(HashCast::Errors::CasterNotFoundError, "caster with name booleans is not found")
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context HashCast::Casters::BooleanCaster do
|
33
|
+
def cast(v)
|
34
|
+
HashCast::Casters::BooleanCaster.cast(v, :attr_name)
|
35
|
+
end
|
36
|
+
it "works with real booleans" do
|
37
|
+
expect(cast(true)).to eq(true)
|
38
|
+
expect(cast(false)).to eq(false)
|
39
|
+
end
|
40
|
+
|
41
|
+
it "works with trueish values" do
|
42
|
+
expect(cast(1)).to eq(true)
|
43
|
+
expect(cast("1")).to eq(true)
|
44
|
+
expect(cast("on")).to eq(true)
|
45
|
+
expect(cast("true")).to eq(true)
|
46
|
+
end
|
47
|
+
|
48
|
+
it "works with falsish values" do
|
49
|
+
expect(cast("0")).to eq(false)
|
50
|
+
expect(cast("false")).to eq(false)
|
51
|
+
expect(cast("off")).to eq(false)
|
52
|
+
expect(cast(0)).to eq(false)
|
53
|
+
end
|
54
|
+
|
55
|
+
it "raises else with wrong values" do
|
56
|
+
expect{
|
57
|
+
cast("something")
|
58
|
+
}.to raise_error(HashCast::Errors::CastingError)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
context HashCast::Casters::DateCaster do
|
63
|
+
def cast(v)
|
64
|
+
HashCast::Casters::DateCaster.cast(v, :attr_name)
|
65
|
+
end
|
66
|
+
it "works with real dates" do
|
67
|
+
value = Date.new
|
68
|
+
expect(cast(value)).to eq(value)
|
69
|
+
end
|
70
|
+
|
71
|
+
it "works with proper string dates" do
|
72
|
+
value = "2016/10/30"
|
73
|
+
expect(cast(value)).to eq(Date.new(2016, 10, 30))
|
74
|
+
end
|
75
|
+
|
76
|
+
it "raises else with wrong string value" do
|
77
|
+
expect{
|
78
|
+
cast("something")
|
79
|
+
}.to raise_error(HashCast::Errors::CastingError)
|
80
|
+
end
|
81
|
+
|
82
|
+
it "raises else" do
|
83
|
+
expect{
|
84
|
+
cast(1)
|
85
|
+
}.to raise_error(HashCast::Errors::CastingError)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
context HashCast::Casters::DateTimeCaster do
|
90
|
+
def cast(v)
|
91
|
+
HashCast::Casters::DateTimeCaster.cast(v, :attr_name)
|
92
|
+
end
|
93
|
+
|
94
|
+
it "works with real datetimes" do
|
95
|
+
value = DateTime.new
|
96
|
+
expect(cast(value)).to eq(value)
|
97
|
+
end
|
98
|
+
|
99
|
+
it "works with proper string datetimes" do
|
100
|
+
value = "2016-10-30T14:20:30+00:00"
|
101
|
+
expect(cast(value)).to eq(DateTime.new(2016, 10, 30, 14, 20, 30))
|
102
|
+
end
|
103
|
+
|
104
|
+
it "raises else with wrong string value" do
|
105
|
+
expect{
|
106
|
+
cast("something")
|
107
|
+
}.to raise_error(HashCast::Errors::CastingError)
|
108
|
+
end
|
109
|
+
|
110
|
+
it "raises else" do
|
111
|
+
expect{
|
112
|
+
cast(1)
|
113
|
+
}.to raise_error(HashCast::Errors::CastingError)
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
context HashCast::Casters::FloatCaster do
|
118
|
+
def cast(v)
|
119
|
+
HashCast::Casters::FloatCaster.cast(v, :attr_name)
|
120
|
+
end
|
121
|
+
|
122
|
+
it "works with real values" do
|
123
|
+
value = 1.4
|
124
|
+
expect(cast(value)).to eq(value)
|
125
|
+
end
|
126
|
+
|
127
|
+
it "works with proper string floats" do
|
128
|
+
value = "1.3"
|
129
|
+
expect(cast(value)).to eq(1.3)
|
130
|
+
end
|
131
|
+
|
132
|
+
it "raises else with wrong string value" do
|
133
|
+
expect{
|
134
|
+
cast("something")
|
135
|
+
}.to raise_error(HashCast::Errors::CastingError)
|
136
|
+
end
|
137
|
+
|
138
|
+
it "raises else" do
|
139
|
+
expect{
|
140
|
+
cast(1)
|
141
|
+
}.to raise_error(HashCast::Errors::CastingError)
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
context HashCast::Casters::HashCast do
|
146
|
+
def cast(v)
|
147
|
+
HashCast::Casters::HashCast.cast(v, :attr_name)
|
148
|
+
end
|
149
|
+
|
150
|
+
it "works with real values" do
|
151
|
+
value = {a: 1}
|
152
|
+
expect(cast(value)).to eq(value)
|
153
|
+
end
|
154
|
+
|
155
|
+
it "raises else" do
|
156
|
+
expect{
|
157
|
+
cast(1)
|
158
|
+
}.to raise_error(HashCast::Errors::CastingError)
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
context HashCast::Casters::IntegerCaster do
|
163
|
+
def cast(v)
|
164
|
+
HashCast::Casters::IntegerCaster.cast(v, :attr_name)
|
165
|
+
end
|
166
|
+
|
167
|
+
it "works with real value" do
|
168
|
+
value = 5
|
169
|
+
expect(cast(value)).to eq(value)
|
170
|
+
end
|
171
|
+
|
172
|
+
it "works with proper string values" do
|
173
|
+
expect(cast("5")).to eq(5)
|
174
|
+
end
|
175
|
+
|
176
|
+
it "raises with invalid string values" do
|
177
|
+
expect{
|
178
|
+
cast("some")
|
179
|
+
}.to raise_error(HashCast::Errors::CastingError)
|
180
|
+
end
|
181
|
+
|
182
|
+
it "raises else" do
|
183
|
+
expect{
|
184
|
+
cast(1.3)
|
185
|
+
}.to raise_error(HashCast::Errors::CastingError)
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
|
190
|
+
context HashCast::Casters::TimeCaster do
|
191
|
+
def cast(v)
|
192
|
+
HashCast::Casters::TimeCaster.cast(v, :attr_name)
|
193
|
+
end
|
194
|
+
|
195
|
+
it "works with real value" do
|
196
|
+
value = Time.new
|
197
|
+
expect(cast(value)).to eq(value)
|
198
|
+
end
|
199
|
+
|
200
|
+
it "works with proper string values" do
|
201
|
+
expect(cast("12:30:15")).to eq(Time.parse("12:30:15"))
|
202
|
+
end
|
203
|
+
|
204
|
+
it "raises with invalid string values" do
|
205
|
+
expect{
|
206
|
+
cast("some")
|
207
|
+
}.to raise_error(HashCast::Errors::CastingError)
|
208
|
+
end
|
209
|
+
|
210
|
+
it "raises else" do
|
211
|
+
expect{
|
212
|
+
cast(1.3)
|
213
|
+
}.to raise_error(HashCast::Errors::CastingError)
|
214
|
+
end
|
215
|
+
end
|
216
|
+
|
217
|
+
|
218
|
+
context HashCast::Casters::SymbolCaster do
|
219
|
+
def cast(v)
|
220
|
+
HashCast::Casters::SymbolCaster.cast(v, :attr_name)
|
221
|
+
end
|
222
|
+
|
223
|
+
it "works with real value" do
|
224
|
+
value = :symbol
|
225
|
+
expect(cast(value)).to eq(value)
|
226
|
+
end
|
227
|
+
|
228
|
+
it "works with proper string values" do
|
229
|
+
expect(cast("symbol")).to eq(:symbol)
|
230
|
+
end
|
231
|
+
|
232
|
+
it "raises with long strings" do
|
233
|
+
expect{
|
234
|
+
cast("a" * (HashCast::Casters::SymbolCaster::MAX_SYMBOL_LENGTH + 1))
|
235
|
+
}.to raise_error(HashCast::Errors::CastingError)
|
236
|
+
end
|
237
|
+
|
238
|
+
it "raises else" do
|
239
|
+
expect{
|
240
|
+
cast(1.3)
|
241
|
+
}.to raise_error(HashCast::Errors::CastingError)
|
242
|
+
end
|
243
|
+
end
|
244
|
+
|
245
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe HashCast do
|
4
|
+
|
5
|
+
describe ".create" do
|
6
|
+
it "should cast hash attributes" do
|
7
|
+
pending "NOT YET IMPLEMENTED"
|
8
|
+
input_hash = {
|
9
|
+
contact: {
|
10
|
+
name: "John Smith",
|
11
|
+
age: "22",
|
12
|
+
company: {
|
13
|
+
name: "MyCo",
|
14
|
+
}
|
15
|
+
}
|
16
|
+
}
|
17
|
+
|
18
|
+
caster = HashCast.create do
|
19
|
+
hash :contact do
|
20
|
+
string :name
|
21
|
+
integer :age
|
22
|
+
hash :company do
|
23
|
+
string :name
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def registered?
|
28
|
+
true
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
casted_hash = caster.cast(input_hash)
|
33
|
+
casted_hash.object_id.should_not == hash.object_id
|
34
|
+
caster_hash.should == hash
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
|
4
|
+
require "byebug"
|
5
|
+
|
6
|
+
|
7
|
+
require 'simplecov'
|
8
|
+
SimpleCov.start do
|
9
|
+
add_filter "/spec/"
|
10
|
+
add_filter "/.direnv/"
|
11
|
+
add_filter "/hcast/concern" # copy from ActiveSupport
|
12
|
+
end
|
13
|
+
if ENV['CI']=='true'
|
14
|
+
require 'codecov'
|
15
|
+
SimpleCov.formatter = SimpleCov::Formatter::Codecov
|
16
|
+
end
|
17
|
+
|
18
|
+
require 'hashcast'
|
19
|
+
RSpec.configure do |config|
|
20
|
+
config.color_enabled = true
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,169 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hashcast
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.4.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Albert Gazizov
|
8
|
+
- Roman Heinrich
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2017-01-05 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '1.3'
|
21
|
+
type: :development
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '1.3'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: byebug
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - "~>"
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "~>"
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: rake
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - "~>"
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - "~>"
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: bixby-bench
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - "~>"
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - "~>"
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: allocation_stats
|
72
|
+
requirement: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - "~>"
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - "~>"
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
- !ruby/object:Gem::Dependency
|
85
|
+
name: codecov
|
86
|
+
requirement: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - "~>"
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
type: :development
|
92
|
+
prerelease: false
|
93
|
+
version_requirements: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - "~>"
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
description: Declarative Hash Caster
|
99
|
+
email:
|
100
|
+
- deeper4k@gmail.com
|
101
|
+
executables: []
|
102
|
+
extensions: []
|
103
|
+
extra_rdoc_files: []
|
104
|
+
files:
|
105
|
+
- ".gitignore"
|
106
|
+
- ".travis.yml"
|
107
|
+
- Gemfile
|
108
|
+
- Gemfile.lock
|
109
|
+
- LICENSE.txt
|
110
|
+
- README.md
|
111
|
+
- Rakefile
|
112
|
+
- benchmark/benchmark.rb
|
113
|
+
- benchmark/casters.rb
|
114
|
+
- docs/_config.yml
|
115
|
+
- docs/index.md
|
116
|
+
- hcast.gemspec
|
117
|
+
- lib/hashcast.rb
|
118
|
+
- lib/hashcast/attributes_caster.rb
|
119
|
+
- lib/hashcast/attributes_parser.rb
|
120
|
+
- lib/hashcast/caster.rb
|
121
|
+
- lib/hashcast/casters.rb
|
122
|
+
- lib/hashcast/casters/array_caster.rb
|
123
|
+
- lib/hashcast/casters/boolean_caster.rb
|
124
|
+
- lib/hashcast/casters/date_caster.rb
|
125
|
+
- lib/hashcast/casters/datetime_caster.rb
|
126
|
+
- lib/hashcast/casters/float_caster.rb
|
127
|
+
- lib/hashcast/casters/hash_caster.rb
|
128
|
+
- lib/hashcast/casters/integer_caster.rb
|
129
|
+
- lib/hashcast/casters/string_caster.rb
|
130
|
+
- lib/hashcast/casters/symbol_caster.rb
|
131
|
+
- lib/hashcast/casters/time_caster.rb
|
132
|
+
- lib/hashcast/concern.rb
|
133
|
+
- lib/hashcast/config.rb
|
134
|
+
- lib/hashcast/errors.rb
|
135
|
+
- lib/hashcast/metadata/attribute.rb
|
136
|
+
- lib/hashcast/version.rb
|
137
|
+
- spec/hcast/caster_spec.rb
|
138
|
+
- spec/hcast/casters_spec.rb
|
139
|
+
- spec/hcast/hcast_spec.rb
|
140
|
+
- spec/spec_helper.rb
|
141
|
+
homepage: http://github.com/ddd-ruby/hashcast
|
142
|
+
licenses:
|
143
|
+
- MIT
|
144
|
+
metadata: {}
|
145
|
+
post_install_message:
|
146
|
+
rdoc_options: []
|
147
|
+
require_paths:
|
148
|
+
- lib
|
149
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
150
|
+
requirements:
|
151
|
+
- - ">="
|
152
|
+
- !ruby/object:Gem::Version
|
153
|
+
version: '0'
|
154
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
155
|
+
requirements:
|
156
|
+
- - ">="
|
157
|
+
- !ruby/object:Gem::Version
|
158
|
+
version: '0'
|
159
|
+
requirements: []
|
160
|
+
rubyforge_project:
|
161
|
+
rubygems_version: 2.5.1
|
162
|
+
signing_key:
|
163
|
+
specification_version: 4
|
164
|
+
summary: Declarative Hash Caster
|
165
|
+
test_files:
|
166
|
+
- spec/hcast/caster_spec.rb
|
167
|
+
- spec/hcast/casters_spec.rb
|
168
|
+
- spec/hcast/hcast_spec.rb
|
169
|
+
- spec/spec_helper.rb
|