hashing 0.0.1.beta.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +210 -0
- data/Rakefile +2 -0
- data/hashing.gemspec +23 -0
- data/lib/hashing.rb +5 -0
- data/lib/hashing/version.rb +3 -0
- metadata +80 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: bd1eb92fa4ef69893588e90462ba6671051ce607
|
4
|
+
data.tar.gz: 22ce96b6cb54a66f481a0f8ecfeda9337e908353
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 008576591fcb187f2bb97ac1524e6c52f13359e814cd8a0370899581c6c2186e45516293ee9de51544fd17caa1fd6854d73bc27535243f87881d86b1c67f6fb1
|
7
|
+
data.tar.gz: 1d37ef8ac7bfd7ffc832add6c6fa0ab754ee5a8079eadaa27bf239008867a73a3eed83637b1a2c63152e30f4dd198f7f42576ca2f3d24f17d9c261007dde9828
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Ricardo Valeriano
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,210 @@
|
|
1
|
+
# Hashing
|
2
|
+
|
3
|
+
Permits you to say which instance variables of your objects should be used to
|
4
|
+
serialize it into a `Hash`. Gives you a nice way to inform this, and facilitates
|
5
|
+
to recreation of your serializable objects from hashes.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'hashing'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install hashing
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Given a `File` class like this:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
class File
|
27
|
+
def initialize(path, commit = nil, content = nil)
|
28
|
+
@path, @commit, @content = path, commit, content
|
29
|
+
end
|
30
|
+
end
|
31
|
+
```
|
32
|
+
|
33
|
+
And I want to turn instances of it in a `Hash` like the one below, so I can, for
|
34
|
+
example, turn this `Hash` into a `YAML` to save it in a file.
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
{
|
38
|
+
path: @path,
|
39
|
+
commit: @commit,
|
40
|
+
content: @content
|
41
|
+
}
|
42
|
+
```
|
43
|
+
|
44
|
+
I just need to include the `Hashing` module, and then indicates which
|
45
|
+
`instance vars` (`ivars`) should be used in the serialization using the class
|
46
|
+
method `hasherize`:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
class File
|
50
|
+
include Hashing
|
51
|
+
hasherize :path, :commit, :content
|
52
|
+
end
|
53
|
+
```
|
54
|
+
|
55
|
+
### #to_h
|
56
|
+
|
57
|
+
Then I will be able to call the `#to_h` method in any instance of `File` to
|
58
|
+
obtain the "hasherized®" version of it:
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
file = File.new 'README.md', 'cfe9aacbc02528b', '#Hashing\n\nWow. Such code...'
|
62
|
+
|
63
|
+
file.to_h
|
64
|
+
# {
|
65
|
+
# path: 'README.md',
|
66
|
+
# commit: 'cfe9aacbc02528b',
|
67
|
+
# content: '#Hashing\n\nWow. Such code...'
|
68
|
+
# }
|
69
|
+
```
|
70
|
+
|
71
|
+
### ::from_hash
|
72
|
+
|
73
|
+
And I can tell `Hashing` how one can create an instance of `File` given a
|
74
|
+
valid `Hash` like the one created by a `#to_h` call:
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
class File
|
78
|
+
include Hashing
|
79
|
+
hasherize :path, :commit, :content
|
80
|
+
|
81
|
+
from_hash ->(hash) {
|
82
|
+
new hash[:path], hash[:commit], hash[:content]
|
83
|
+
}
|
84
|
+
|
85
|
+
# ...
|
86
|
+
end
|
87
|
+
```
|
88
|
+
|
89
|
+
So I can transform instances of any Ruby class into hashes and rebuild objects
|
90
|
+
of any type from hashes, accordingly with my business rules, without expose
|
91
|
+
these rules outside my classes.
|
92
|
+
|
93
|
+
Note that both `#to_h` and `::from_hash` methods are public, and you can and
|
94
|
+
should call them whatever you need in your programs. But the `::from_hash`
|
95
|
+
method will be called by `Hashing` when building your instances from hashes (more
|
96
|
+
about this: [nested hasherizing](#nested-hasherized-objects)).
|
97
|
+
|
98
|
+
#### Hasherize
|
99
|
+
|
100
|
+
`Hashing` comes with an alternative way to indicate what fields should be used to
|
101
|
+
represent your objects as a `Hash`. This can be done by the `Hasherize` class.
|
102
|
+
The previous example can be writen like this:
|
103
|
+
|
104
|
+
```ruby
|
105
|
+
class File
|
106
|
+
include Hasherize.new :path, :commit, :content
|
107
|
+
|
108
|
+
from_hash ->(hash) {
|
109
|
+
new hash[:path], hash[:commit], hash[:content]
|
110
|
+
}
|
111
|
+
|
112
|
+
# ...
|
113
|
+
end
|
114
|
+
```
|
115
|
+
|
116
|
+
It's just a matter of taste. Use the one that is more appealing to you.
|
117
|
+
|
118
|
+
### Custom "hasherizing" and loading strategies
|
119
|
+
|
120
|
+
Many times you will need apply some computation over the contents of your
|
121
|
+
`ivars` transform them in a primitive that can be stored as a `Hash`.
|
122
|
+
|
123
|
+
Following the `File` class example, maybe you want to store the `@content` as a
|
124
|
+
`Base64` enconded string.
|
125
|
+
|
126
|
+
`Hashing` allows you to specify the strategy of serialization and loading when
|
127
|
+
indicating which `ivars` should be part of the final `Hash`:
|
128
|
+
|
129
|
+
```ruby
|
130
|
+
require 'base64'
|
131
|
+
|
132
|
+
class File
|
133
|
+
include Hashing
|
134
|
+
|
135
|
+
hasherize :path, :commit
|
136
|
+
|
137
|
+
hasherize :content,
|
138
|
+
to_hash: ->(content) { Base64.encode64 content },
|
139
|
+
from_hash: ->(content_string) { Base64.decode64 content_string }
|
140
|
+
|
141
|
+
from_hash ->(hash) {
|
142
|
+
new hash[:path], hash[:commit], hash[:content]
|
143
|
+
}
|
144
|
+
|
145
|
+
# ...
|
146
|
+
end
|
147
|
+
```
|
148
|
+
|
149
|
+
|
150
|
+
#### Nested hasherized objects
|
151
|
+
|
152
|
+
But if your transformations are a little more complicated than a simple `Base64`
|
153
|
+
encoding, chances are there that you have a nested objects to be serialized.
|
154
|
+
|
155
|
+
Extending our example, maybe our `File` class have an internal collection of
|
156
|
+
`Annotation`, to allow the user to indicate what is in which line of a file:
|
157
|
+
|
158
|
+
```ruby
|
159
|
+
# annotation.rb
|
160
|
+
|
161
|
+
class Annotation
|
162
|
+
include Hasherize.new :lines, :annotation
|
163
|
+
|
164
|
+
def initialize(lines, annotation)
|
165
|
+
@lines = lines
|
166
|
+
@annotation = annotation
|
167
|
+
end
|
168
|
+
end
|
169
|
+
```
|
170
|
+
|
171
|
+
And the `File` class could have a method to add new annotations:
|
172
|
+
|
173
|
+
```ruby
|
174
|
+
# file.rb
|
175
|
+
|
176
|
+
class File
|
177
|
+
# ...
|
178
|
+
|
179
|
+
def annotate(lines, annotation)
|
180
|
+
@annotations ||= []
|
181
|
+
@annotations << Annotation.new(lines, annotation)
|
182
|
+
end
|
183
|
+
# ...
|
184
|
+
end
|
185
|
+
```
|
186
|
+
|
187
|
+
So in this case, if you wants a file to be `hasherized®` with it's internall
|
188
|
+
`@annotations` preserved, you just indicate this in the `File` class. The
|
189
|
+
example now can be rewriten as:
|
190
|
+
|
191
|
+
```ruby
|
192
|
+
class File
|
193
|
+
include Hashing
|
194
|
+
|
195
|
+
hasherize :path, :commit, :annotations
|
196
|
+
|
197
|
+
# ...
|
198
|
+
end
|
199
|
+
```
|
200
|
+
|
201
|
+
Since the `Annotation` class has it's own notion of `#to_h` and `::from_hash`,
|
202
|
+
this is all that `Hashing` needs to build a `File` instances from a valid `Hash`.
|
203
|
+
|
204
|
+
## Contributing
|
205
|
+
|
206
|
+
1. Fork it ( https://github.com/[my-github-username]/hasher/fork )
|
207
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
208
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
209
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
210
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/hashing.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'hashing/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "hashing"
|
8
|
+
spec.version = Hashing::VERSION
|
9
|
+
spec.authors = ["Ricardo Valeriano"]
|
10
|
+
spec.email = ["ricardo.valeriano@gmail.com"]
|
11
|
+
spec.summary = %q{Serialize your objects as Hashes}
|
12
|
+
#spec.description = %q{TODO: Write a longer description. Optional.}
|
13
|
+
spec.homepage = "http://github.com/ricardovaleriano/hashing"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
data/lib/hashing.rb
ADDED
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hashing
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1.beta.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ricardo Valeriano
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-04-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description:
|
42
|
+
email:
|
43
|
+
- ricardo.valeriano@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- .gitignore
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE.txt
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- hashing.gemspec
|
54
|
+
- lib/hashing.rb
|
55
|
+
- lib/hashing/version.rb
|
56
|
+
homepage: http://github.com/ricardovaleriano/hashing
|
57
|
+
licenses:
|
58
|
+
- MIT
|
59
|
+
metadata: {}
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - '>'
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: 1.3.1
|
74
|
+
requirements: []
|
75
|
+
rubyforge_project:
|
76
|
+
rubygems_version: 2.1.11
|
77
|
+
signing_key:
|
78
|
+
specification_version: 4
|
79
|
+
summary: Serialize your objects as Hashes
|
80
|
+
test_files: []
|