hash_out 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/.travis.yml +8 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +20 -0
- data/README.md +94 -0
- data/Rakefile +9 -0
- data/hash_out.gemspec +23 -0
- data/lib/hash_out/version.rb +3 -0
- data/lib/hash_out.rb +48 -0
- data/spec/lib/integration_spec.rb +109 -0
- metadata +83 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 56a567fff415c75fb0bb97f0c334cd4a50272d02
|
4
|
+
data.tar.gz: b5b63f10a8e06a0883c98f23cc24aeada979bb01
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 49bce4736f8707cc08c3ac511dcaa120681524c60de987c91f67580bef6b294e460b762f605fb552a13e83f89447f307e642a310ac3ed9f1c754f78cb280260b
|
7
|
+
data.tar.gz: 8c46716e629007476f50f423acee955e64fd4eaadd3ff06674f7ee5f05d1dbbb8b30d5e50c10fac507d85c93fc3b12e3831403d9e1e48a76be835f079963b3d9
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
hash_out (0.1.0)
|
5
|
+
rake
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
minitest (5.0.6)
|
11
|
+
minitest-spec-expect (0.1.3)
|
12
|
+
minitest (~> 5.0)
|
13
|
+
rake (10.1.0)
|
14
|
+
|
15
|
+
PLATFORMS
|
16
|
+
ruby
|
17
|
+
|
18
|
+
DEPENDENCIES
|
19
|
+
hash_out!
|
20
|
+
minitest-spec-expect (~> 0.1)
|
data/README.md
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
# hash_out
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/davejachimiak/hash_out.png?branch=master)](https://travis-ci.org/davejachimiak/hash_out)
|
4
|
+
|
5
|
+
Include `HashOut` to your class and convert your object to a hash with the `#hash_out` method.
|
6
|
+
|
7
|
+
## Install
|
8
|
+
Add to your Gemfile:
|
9
|
+
```ruby
|
10
|
+
gem 'hash_out', '~> 0.1'
|
11
|
+
```
|
12
|
+
|
13
|
+
Or install it from the command line:
|
14
|
+
```sh
|
15
|
+
$ gem install hash_out
|
16
|
+
```
|
17
|
+
|
18
|
+
## Usage
|
19
|
+
1. `require 'hash_out'`.
|
20
|
+
2. Include `HashOut` in your class.
|
21
|
+
3. If desired, exclude methods from `#hash_out` by adding `exclude_from_hash_out` to them.
|
22
|
+
4. Call `#hash_out` on the instance to return a hash of method names and their values.
|
23
|
+
|
24
|
+
**`#hash_out` automatically excludes private methods and methods that require arguments.**
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
require 'hash_out'
|
28
|
+
|
29
|
+
class Movie
|
30
|
+
include HashOut
|
31
|
+
|
32
|
+
def title
|
33
|
+
'Fire Walk With Me'
|
34
|
+
end
|
35
|
+
|
36
|
+
def director
|
37
|
+
exclude_from_hash_out
|
38
|
+
'David Lynch'
|
39
|
+
end
|
40
|
+
|
41
|
+
def available_instantly? catalog=TerribleStreamingService.new
|
42
|
+
catalog.available_instantly? self
|
43
|
+
end
|
44
|
+
|
45
|
+
def has_actor? actor
|
46
|
+
actors.include? actor
|
47
|
+
end
|
48
|
+
|
49
|
+
def chance_of_sequel? existing_sequels, strategy=FutureSequelStrategy
|
50
|
+
strategy.new(self, existing_sequels).chance > 0.5
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
def actors
|
56
|
+
['Sheryl Lee', 'David Bowie', 'Ray Wise']
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
movie = Movie.new
|
61
|
+
movie.hash_out
|
62
|
+
# => {:title=>"Fire Walk With Me", :available_instantly?=>false}
|
63
|
+
|
64
|
+
```
|
65
|
+
|
66
|
+
## Contribute
|
67
|
+
1. Fork the repo.
|
68
|
+
2. Create a branch.
|
69
|
+
3. Add specs and code.
|
70
|
+
4. Ensure the specs are green (`$ rake`)
|
71
|
+
5. Open a pull request.
|
72
|
+
|
73
|
+
## License
|
74
|
+
The MIT License (MIT)
|
75
|
+
|
76
|
+
Copyright (c) 2013 Dave Jachimiak
|
77
|
+
|
78
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
79
|
+
of this software and associated documentation files (the "Software"), to deal
|
80
|
+
in the Software without restriction, including without limitation the rights
|
81
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
82
|
+
copies of the Software, and to permit persons to whom the Software is
|
83
|
+
furnished to do so, subject to the following conditions:
|
84
|
+
|
85
|
+
The above copyright notice and this permission notice shall be included in
|
86
|
+
all copies or substantial portions of the Software.
|
87
|
+
|
88
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
89
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
90
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
91
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
92
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
93
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
94
|
+
THE SOFTWARE.
|
data/Rakefile
ADDED
data/hash_out.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
$:.push File.expand_path("../lib", __FILE__)
|
4
|
+
require 'hash_out/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = 'hash_out'
|
8
|
+
s.description = 'Easily convert public methods to a hash.'
|
9
|
+
s.summary = "hash_out adds a method to your class' instance and returns hash of public method names and values."
|
10
|
+
s.authors = ['Dave Jachimiak']
|
11
|
+
s.email = 'dave.jachimiak@gmail.com'
|
12
|
+
s.homepage = 'http://github.com/davejachimiak/hash_out'
|
13
|
+
s.version = HashOut::VERSION
|
14
|
+
s.license = 'MIT'
|
15
|
+
s.files = `git ls-files`.split("\n").reject do |file_name|
|
16
|
+
/\.gem$/.match file_name
|
17
|
+
end
|
18
|
+
s.test_files = `git ls-files -- spec`.split("\n")
|
19
|
+
s.require_paths = ['lib']
|
20
|
+
|
21
|
+
s.add_dependency 'rake'
|
22
|
+
s.add_development_dependency 'minitest-spec-expect', '~> 0.1'
|
23
|
+
end
|
data/lib/hash_out.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
module HashOut
|
2
|
+
require 'set'
|
3
|
+
|
4
|
+
def hash_out
|
5
|
+
set_hash_out
|
6
|
+
delete_exclusions
|
7
|
+
@hash_out
|
8
|
+
end
|
9
|
+
|
10
|
+
protected
|
11
|
+
|
12
|
+
def exclude_from_hash_out
|
13
|
+
excluded_method = caller_method_sym caller.first
|
14
|
+
exclusions.add excluded_method
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def set_hash_out
|
20
|
+
@hash_out = Hash[interesting_methods_and_values]
|
21
|
+
end
|
22
|
+
|
23
|
+
def interesting_methods_and_values
|
24
|
+
methods_requiring_no_arguments.map do |method_name|
|
25
|
+
name_value_pair method_name
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def methods_requiring_no_arguments
|
30
|
+
public_methods(false).select { |m| [-1, 0].include? method(m).arity }
|
31
|
+
end
|
32
|
+
|
33
|
+
def name_value_pair method_name
|
34
|
+
[method_name, send(method_name)]
|
35
|
+
end
|
36
|
+
|
37
|
+
def delete_exclusions
|
38
|
+
exclusions.each { |exclusion| @hash_out.delete exclusion }
|
39
|
+
end
|
40
|
+
|
41
|
+
def exclusions
|
42
|
+
@exclusions ||= Set.new
|
43
|
+
end
|
44
|
+
|
45
|
+
def caller_method_sym caller_string
|
46
|
+
caller_string.match(/`(.*)'/)[1].to_sym
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,109 @@
|
|
1
|
+
require_relative '../../lib/hash_out'
|
2
|
+
require 'minitest/autorun'
|
3
|
+
require 'minitest/spec/expect'
|
4
|
+
|
5
|
+
describe 'HashOut#hash_out' do
|
6
|
+
class Baller
|
7
|
+
include HashOut
|
8
|
+
|
9
|
+
def mood
|
10
|
+
:ballin
|
11
|
+
end
|
12
|
+
|
13
|
+
def height
|
14
|
+
:tall
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'returns a hash of name-values' do
|
19
|
+
baller = Baller.new
|
20
|
+
hash_out = {
|
21
|
+
mood: baller.mood,
|
22
|
+
height: baller.height
|
23
|
+
}
|
24
|
+
|
25
|
+
expect(baller.hash_out).to_equal hash_out
|
26
|
+
end
|
27
|
+
|
28
|
+
class ShotCaller
|
29
|
+
include HashOut
|
30
|
+
|
31
|
+
def front
|
32
|
+
:chillin
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def real_mood
|
38
|
+
:nervous
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'ignores private methods' do
|
43
|
+
shot_caller = ShotCaller.new
|
44
|
+
hash_out = { front: shot_caller.front }
|
45
|
+
|
46
|
+
expect(shot_caller.hash_out).to_equal hash_out
|
47
|
+
end
|
48
|
+
|
49
|
+
class Brawler
|
50
|
+
include HashOut
|
51
|
+
|
52
|
+
def fighting?
|
53
|
+
true
|
54
|
+
end
|
55
|
+
|
56
|
+
def pancakes
|
57
|
+
exclude_from_hash_out
|
58
|
+
'yes please'
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'ignores public methods that declare exclusion from public hash' do
|
63
|
+
brawler = Brawler.new
|
64
|
+
hash_out = { fighting?: brawler.fighting? }
|
65
|
+
|
66
|
+
expect(brawler.hash_out).to_equal hash_out
|
67
|
+
end
|
68
|
+
|
69
|
+
class Beans; end;
|
70
|
+
class Grinder
|
71
|
+
require 'ostruct'
|
72
|
+
include HashOut
|
73
|
+
|
74
|
+
def initialize
|
75
|
+
@beans = Beans.new
|
76
|
+
end
|
77
|
+
|
78
|
+
def grind settings, beans=@beans
|
79
|
+
with_settings(settings) do
|
80
|
+
beans.krush
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def clean materials
|
85
|
+
clean_with materials
|
86
|
+
end
|
87
|
+
|
88
|
+
def blade edge=:sharp
|
89
|
+
edge
|
90
|
+
end
|
91
|
+
|
92
|
+
private
|
93
|
+
|
94
|
+
def with_settings settings
|
95
|
+
@settings = settings
|
96
|
+
yield self
|
97
|
+
end
|
98
|
+
|
99
|
+
def clean_with
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
it 'ignores public methods that require arguments' do
|
104
|
+
grinder = Grinder.new
|
105
|
+
hash_out = { blade: grinder.blade }
|
106
|
+
|
107
|
+
expect(grinder.hash_out).to_equal hash_out
|
108
|
+
end
|
109
|
+
end
|
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hash_out
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dave Jachimiak
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-07-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: minitest-spec-expect
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.1'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.1'
|
41
|
+
description: Easily convert public methods to a hash.
|
42
|
+
email: dave.jachimiak@gmail.com
|
43
|
+
executables: []
|
44
|
+
extensions: []
|
45
|
+
extra_rdoc_files: []
|
46
|
+
files:
|
47
|
+
- .gitignore
|
48
|
+
- .travis.yml
|
49
|
+
- Gemfile
|
50
|
+
- Gemfile.lock
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- hash_out.gemspec
|
54
|
+
- lib/hash_out.rb
|
55
|
+
- lib/hash_out/version.rb
|
56
|
+
- spec/lib/integration_spec.rb
|
57
|
+
homepage: http://github.com/davejachimiak/hash_out
|
58
|
+
licenses:
|
59
|
+
- MIT
|
60
|
+
metadata: {}
|
61
|
+
post_install_message:
|
62
|
+
rdoc_options: []
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - '>='
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
requirements: []
|
76
|
+
rubyforge_project:
|
77
|
+
rubygems_version: 2.0.2
|
78
|
+
signing_key:
|
79
|
+
specification_version: 4
|
80
|
+
summary: hash_out adds a method to your class' instance and returns hash of public
|
81
|
+
method names and values.
|
82
|
+
test_files:
|
83
|
+
- spec/lib/integration_spec.rb
|