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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 33afa8dfdc936c7e5aa452ddaf8f12d82d4f8b14
4
+ data.tar.gz: 77941ab23bf8ccf44b86464721f4dae049409781
5
+ SHA512:
6
+ metadata.gz: b2816006132608977f643b2dd6b388f25b693fcaa4928a117fde5b68738234ceec6fc67652bc91b21c238858e1eb6d72ff1890141cc03735b02bc953ec1a1ade
7
+ data.tar.gz: c06efe176d18c5b259508d12db35c598776c147dc6b0d9e24c93e5117d1407dff2b845ff6ba454ef9a0e37f81b14a4c90a98b6f0062f22d73f0ecd381bd25eb4
@@ -0,0 +1,8 @@
1
+ vendor/
2
+ tags
3
+ .bundle
4
+ .DS_Store
5
+ tmp/
6
+ pkg
7
+ .byebug_history
8
+ coverage/
@@ -0,0 +1,3 @@
1
+ rvm:
2
+ - 2.2.0
3
+ script: "bundle exec rspec spec/"
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :test do
6
+ gem 'rspec'
7
+ end
@@ -0,0 +1,50 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ hashcast (0.3.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ allocation_stats (0.1.5)
10
+ bixby-bench (0.1.0)
11
+ allocation_stats
12
+ byebug (9.0.6)
13
+ codecov (0.1.9)
14
+ json
15
+ simplecov
16
+ url
17
+ diff-lcs (1.2.5)
18
+ docile (1.1.5)
19
+ json (2.0.2)
20
+ rake (10.1.1)
21
+ rspec (2.14.1)
22
+ rspec-core (~> 2.14.0)
23
+ rspec-expectations (~> 2.14.0)
24
+ rspec-mocks (~> 2.14.0)
25
+ rspec-core (2.14.7)
26
+ rspec-expectations (2.14.4)
27
+ diff-lcs (>= 1.1.3, < 2.0)
28
+ rspec-mocks (2.14.4)
29
+ simplecov (0.12.0)
30
+ docile (~> 1.1.0)
31
+ json (>= 1.8, < 3)
32
+ simplecov-html (~> 0.10.0)
33
+ simplecov-html (0.10.0)
34
+ url (0.3.2)
35
+
36
+ PLATFORMS
37
+ ruby
38
+
39
+ DEPENDENCIES
40
+ allocation_stats
41
+ bixby-bench
42
+ bundler (~> 1.3)
43
+ byebug
44
+ codecov
45
+ hashcast!
46
+ rake
47
+ rspec
48
+
49
+ BUNDLED WITH
50
+ 1.13.6
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Albert Gazizov
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,125 @@
1
+ # HashCast [![Build Status](https://travis-ci.org/ddd-ruby/hashcast.png)](https://travis-ci.org/ddd-ruby/hashcast) [![Code Climate](https://codeclimate.com/github/ddd-ruby/hashcast.png)](https://codeclimate.com/github/ddd-ruby/hashcast) [![codecov](https://codecov.io/gh/ddd-ruby/hashcast/branch/master/graph/badge.svg)](https://codecov.io/gh/ddd-ruby/hashcast)
2
+
3
+
4
+ HashCast is a library for casting hash attributes
5
+
6
+ ### Usage
7
+
8
+ Create caster class and declare hash attributes inside:
9
+
10
+ ```ruby
11
+ class ContactCaster
12
+ include HashCast::Caster
13
+
14
+ attributes do
15
+ hash :contact do
16
+ string :name
17
+ integer :age, optional: true
18
+ float :weight
19
+ date :birthday
20
+ datetime :last_logged_in
21
+ time :last_visited_at
22
+ hash :company do
23
+ string :name
24
+ end
25
+ array :emails, each: :string
26
+ array :social_accounts, each: :hash do
27
+ string :name
28
+ symbol :type
29
+ end
30
+ end
31
+ end
32
+ end
33
+ ```
34
+
35
+ Instantiate the caster and give your hash for casting:
36
+
37
+ ```ruby
38
+ ContactCaster.cast({
39
+ contact: {
40
+ name: "John Smith",
41
+ age: "22",
42
+ weight: "65.5",
43
+ birthday: "2014-02-02",
44
+ last_logged_in: "2014-02-02 10:10:00",
45
+ last_visited_at: "2014-02-02 10:10:00",
46
+ company: {
47
+ name: "MyCo"
48
+ },
49
+ emails: ["test@example.com", "test2@example.com"],
50
+ social_accounts: [
51
+ {
52
+ name: "john_smith",
53
+ type: "twitter"
54
+ },
55
+ {
56
+ name: "John",
57
+ type: :facebook
58
+ }
59
+ ]
60
+ }
61
+ }
62
+ })
63
+ ```
64
+
65
+ The caster will cast your hash attributes to:
66
+
67
+ ```ruby
68
+ {
69
+ contact: {
70
+ name: "John Smith",
71
+ age: 22,
72
+ weight: 65.5,
73
+ birthday: #<Date: 2014-02-02 ((2456691j,0s,0n),+0s,2299161j)>,
74
+ last_logged_in: #<DateTime: 2014-02-02T10:10:00+00:00 ((2456691j,36600s,0n),+0s,2299161j)>,
75
+ last_visited_at: 2014-02-02 10:10:00 +0400,
76
+ company: {
77
+ name: "MyCo"
78
+ },
79
+ emails: ["test@example.com", "test2@example.com"],
80
+ social_accounts: [
81
+ {
82
+ name: "john_smith",
83
+ type: :twitter"
84
+ },
85
+ {
86
+ name: "John",
87
+ type: :facebook
88
+ }
89
+ ]
90
+ }
91
+ }
92
+ ```
93
+
94
+ if some of the attributes can't be casted the HashCast::Errors::CastingError is raised
95
+
96
+
97
+ Also you can provide options to every caster about expected keys type for input/output (:symbol / :string)
98
+
99
+ ```ruby
100
+ class SettingsCaster
101
+ include HashCast::Caster
102
+
103
+ attributes do
104
+ string :account
105
+ end
106
+ end
107
+ SettingsCaster.cast({account: "some"}, input_keys: :symbol, output_keys: :string)
108
+ # => {"account" => "some"}
109
+ ```
110
+
111
+
112
+ ## Configuration
113
+
114
+
115
+ ```ruby
116
+ # expect all input keys to be strings
117
+ HashCast.config.input_keys = :string
118
+
119
+ # expect all output keys to be symbols
120
+ HashCast.config.output_keys = :symbol
121
+ ```
122
+
123
+ ## Authors
124
+ Albert Gazizov, [@deeper4k](https://twitter.com/deeper4k)
125
+ Roman Heinrich, [@mindreframer](https://twitter.com/mindreframer)
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,66 @@
1
+ # run with `ruby benchmark/benchmark.rb`
2
+ require 'bundler'
3
+ Bundler.setup
4
+ require 'benchmark'
5
+ require 'hcast'
6
+ require 'bixby/bench'
7
+
8
+ require_relative "casters"
9
+
10
+ class CastingBenchmark
11
+ def input_contact
12
+ @input_contact ||= {
13
+ contact: {
14
+ name: "John Smith",
15
+ age: "22",
16
+ weight: "65.5",
17
+ birthday: "2014-02-02",
18
+ last_logged_in: "2014-02-02 10:10:00",
19
+ last_visited_at: "2014-02-02 10:10:00",
20
+ company: {
21
+ name: "MyCo",
22
+ },
23
+ emails: [ "test@example.com", "test2@example.com" ],
24
+ social_accounts: [
25
+ {
26
+ name: "john_smith",
27
+ type: 'twitter',
28
+ },
29
+ {
30
+ name: "John",
31
+ type: :facebook,
32
+ },
33
+ ]
34
+ }
35
+ }
36
+ end
37
+
38
+ def input_company
39
+ @input_company ||= {
40
+ name: 'Might & Magic',
41
+ settings: {
42
+ account: :'migthy_lord'
43
+ },
44
+ emails: [
45
+ { address: :'test1@example.com' },
46
+ { address: :'test2@example.com' },
47
+ ]
48
+ }
49
+ end
50
+
51
+ def self.run
52
+ instance = CastingBenchmark.new
53
+ Bixby::Bench.run(10_000) do |b|
54
+ b.sample('Contact Caster') do
55
+ ContactCaster.cast(instance.input_contact)
56
+ end
57
+
58
+ b.sample("CompanyCaster") do
59
+ CompanyCaster.cast(instance.input_company)
60
+ end
61
+ end
62
+ end
63
+ end
64
+
65
+ CastingBenchmark.run
66
+
@@ -0,0 +1,50 @@
1
+ class ContactCaster
2
+ include HashCast::Caster
3
+ attributes do
4
+ hash :contact do
5
+ string :name
6
+ integer :age, optional: true
7
+ float :weight
8
+ date :birthday
9
+ datetime :last_logged_in
10
+ time :last_visited_at
11
+ hash :company do
12
+ string :name
13
+ end
14
+ array :emails, each: :string
15
+ array :social_accounts, each: :hash do
16
+ string :name
17
+ symbol :type
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+
24
+ #########
25
+
26
+ class SettingsCaster
27
+ include HashCast::Caster
28
+
29
+ attributes do
30
+ string :account
31
+ end
32
+ end
33
+
34
+ class EmailCaster
35
+ include HashCast::Caster
36
+
37
+ attributes do
38
+ string :address
39
+ end
40
+ end
41
+
42
+ class CompanyCaster
43
+ include HashCast::Caster
44
+
45
+ attributes do
46
+ string :name
47
+ hash :settings, caster: SettingsCaster
48
+ array :emails, caster: EmailCaster
49
+ end
50
+ end
@@ -0,0 +1 @@
1
+ theme: jekyll-theme-minimal
@@ -0,0 +1,124 @@
1
+ # HashCast [![Build Status](https://travis-ci.org/AlbertGazizov/hcast.png)](https://travis-ci.org/AlbertGazizov/hcast) [![Code Climate](https://codeclimate.com/github/AlbertGazizov/hcast.png)](https://codeclimate.com/github/AlbertGazizov/hcast) [![codecov](https://codecov.io/gh/ddd-ruby/hcast/branch/master/graph/badge.svg)](https://codecov.io/gh/ddd-ruby/hcast)
2
+
3
+
4
+ HashCast is a library for casting hash attributes
5
+
6
+ ### Usage
7
+
8
+ Create caster class and declare hash attributes inside:
9
+
10
+ ```ruby
11
+ class ContactCaster
12
+ include HashCast::Caster
13
+
14
+ attributes do
15
+ hash :contact do
16
+ string :name
17
+ integer :age, optional: true
18
+ float :weight
19
+ date :birthday
20
+ datetime :last_logged_in
21
+ time :last_visited_at
22
+ hash :company do
23
+ string :name
24
+ end
25
+ array :emails, each: :string
26
+ array :social_accounts, each: :hash do
27
+ string :name
28
+ symbol :type
29
+ end
30
+ end
31
+ end
32
+ end
33
+ ```
34
+
35
+ Instantiate the caster and give your hash for casting:
36
+
37
+ ```ruby
38
+ ContactCaster.cast({
39
+ contact: {
40
+ name: "John Smith",
41
+ age: "22",
42
+ weight: "65.5",
43
+ birthday: "2014-02-02",
44
+ last_logged_in: "2014-02-02 10:10:00",
45
+ last_visited_at: "2014-02-02 10:10:00",
46
+ company: {
47
+ name: "MyCo"
48
+ },
49
+ emails: ["test@example.com", "test2@example.com"],
50
+ social_accounts: [
51
+ {
52
+ name: "john_smith",
53
+ type: "twitter"
54
+ },
55
+ {
56
+ name: "John",
57
+ type: :facebook
58
+ }
59
+ ]
60
+ }
61
+ }
62
+ })
63
+ ```
64
+
65
+ The caster will cast your hash attributes to:
66
+
67
+ ```ruby
68
+ {
69
+ contact: {
70
+ name: "John Smith",
71
+ age: 22,
72
+ weight: 65.5,
73
+ birthday: #<Date: 2014-02-02 ((2456691j,0s,0n),+0s,2299161j)>,
74
+ last_logged_in: #<DateTime: 2014-02-02T10:10:00+00:00 ((2456691j,36600s,0n),+0s,2299161j)>,
75
+ last_visited_at: 2014-02-02 10:10:00 +0400,
76
+ company: {
77
+ name: "MyCo"
78
+ },
79
+ emails: ["test@example.com", "test2@example.com"],
80
+ social_accounts: [
81
+ {
82
+ name: "john_smith",
83
+ type: :twitter"
84
+ },
85
+ {
86
+ name: "John",
87
+ type: :facebook
88
+ }
89
+ ]
90
+ }
91
+ }
92
+ ```
93
+
94
+ if some of the attributes can't be casted the HashCast::Errors::CastingError is raised
95
+
96
+
97
+ Also you can provide options to every caster about expected keys type for input/output (:symbol / :string)
98
+
99
+ ```ruby
100
+ class SettingsCaster
101
+ include HashCast::Caster
102
+
103
+ attributes do
104
+ string :account
105
+ end
106
+ end
107
+ SettingsCaster.cast({account: "some"}, input_keys: :symbol, output_keys: :string)
108
+ # => {"account" => "some"}
109
+ ```
110
+
111
+
112
+ ## Configuration
113
+
114
+
115
+ ```ruby
116
+ # expect all input keys to be strings
117
+ HashCast.config.input_keys = :string
118
+
119
+ # expect all output keys to be symbols
120
+ HashCast.config.output_keys = :symbol
121
+ ```
122
+
123
+ ## Author
124
+ Albert Gazizov, [@deeper4k](https://twitter.com/deeper4k)