dish 0.0.1
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 +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +53 -0
- data/Rakefile +10 -0
- data/dish.gemspec +22 -0
- data/lib/dish/ext/array.rb +5 -0
- data/lib/dish/ext/hash.rb +5 -0
- data/lib/dish/ext.rb +3 -0
- data/lib/dish/plate.rb +41 -0
- data/lib/dish/version.rb +3 -0
- data/lib/dish.rb +10 -0
- data/test/dish_test.rb +68 -0
- data/test/test_helper.rb +2 -0
- metadata +89 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ef440675192f5eb0ed6d027a9ed8efc0ac91eb57
|
4
|
+
data.tar.gz: 4715d8232e09e33e6c2fa440e5fcc78459c83735
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6ecd4d47090d3834d67b492b2b33cc85910a816c9bc5ab9adcaa1c155eaa9ba4ab162d58e7ce48829434d293ddbaf3bb633fbc6546de3c183179e33750297acf
|
7
|
+
data.tar.gz: 13258764d91e10de3065eab64e1ffc57a972abcd181f230192dc4d8871263ece0a1ebeb7ee943428b078b4c5b9f57ef807cf24138cbc458341997fe3073f5750
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Lasse Bunk
|
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,53 @@
|
|
1
|
+
# Dish!
|
2
|
+
|
3
|
+
Very simple conversion of hashes to plain Ruby objects.
|
4
|
+
This is great for consuming JSON API's which is what I use it for.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's *Gemfile*:
|
9
|
+
|
10
|
+
gem "dish"
|
11
|
+
|
12
|
+
Then run:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself:
|
17
|
+
|
18
|
+
$ gem install dish
|
19
|
+
|
20
|
+
If you want a `to_dish` helper method added to your Hash and Array objects, you can require `dish/ext` in your *Gemfile*:
|
21
|
+
|
22
|
+
gem "dish", require: "dish/ext"
|
23
|
+
|
24
|
+
# Example
|
25
|
+
|
26
|
+
hash = {
|
27
|
+
title: "My Title",
|
28
|
+
authors: [
|
29
|
+
{ id: 1, name: "Mike Anderson" },
|
30
|
+
{ id: 2, name: "Well D." }
|
31
|
+
],
|
32
|
+
active: false
|
33
|
+
}
|
34
|
+
|
35
|
+
book = Dish(hash) # or hash.to_dish if you required "dish/ext"
|
36
|
+
book.title # => "My Title"
|
37
|
+
book.authors.length # => 2
|
38
|
+
book.authors[1].name # => "Well D."
|
39
|
+
book.title? # => true
|
40
|
+
book.active? # => false
|
41
|
+
book.other # => nil
|
42
|
+
book.other? # => false
|
43
|
+
|
44
|
+
## Contributing
|
45
|
+
|
46
|
+
Feature additions are very welcome, but please submit an issue first, so we can discuss it in advance. Thanks.
|
47
|
+
|
48
|
+
1. Fork the project
|
49
|
+
2. Create a feature branch (`git checkout -b my-new-feature`)
|
50
|
+
3. Make your changes, including tests so it doesn't break in the future
|
51
|
+
4. Commit your changes (`git commit -am 'Add feature'`)
|
52
|
+
5. Push to the branch (`git push origin my-new-feature`)
|
53
|
+
6. Create new pull request
|
data/Rakefile
ADDED
data/dish.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'dish/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "dish"
|
8
|
+
spec.version = Dish::VERSION
|
9
|
+
spec.authors = ["Lasse Bunk"]
|
10
|
+
spec.email = ["lassebunk@gmail.com"]
|
11
|
+
spec.summary = %q{Super simple conversion of hashes to plain Ruby objects}
|
12
|
+
spec.description = %q{Super simple conversion of hashes to plain Ruby objects. Great for consuming JSON API's.}
|
13
|
+
spec.homepage = "https://github.com/lassebunk/dish"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.test_files = spec.files.grep(%r{^test/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
21
|
+
spec.add_development_dependency "rake"
|
22
|
+
end
|
data/lib/dish/ext.rb
ADDED
data/lib/dish/plate.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
module Dish
|
2
|
+
class Plate
|
3
|
+
def initialize(hash)
|
4
|
+
hash.each do |key, value|
|
5
|
+
dish_metaclass.send(:define_method, key) do
|
6
|
+
dish_convert_value(value)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def method_missing(method, *args, &block)
|
12
|
+
method = method.to_s
|
13
|
+
if method.end_with?("?")
|
14
|
+
field = method[0..-2]
|
15
|
+
dish_check_for_presence(send(field))
|
16
|
+
else
|
17
|
+
nil
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def dish_check_for_presence(value)
|
24
|
+
!!value
|
25
|
+
end
|
26
|
+
|
27
|
+
def dish_convert_value(value)
|
28
|
+
case value
|
29
|
+
when Hash then self.class.new(value)
|
30
|
+
when Array then value.map { |v| dish_convert_value(v) }
|
31
|
+
else value
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def dish_metaclass
|
36
|
+
class << self
|
37
|
+
self
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
data/lib/dish/version.rb
ADDED
data/lib/dish.rb
ADDED
data/test/dish_test.rb
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class DishTest < Test::Unit::TestCase
|
4
|
+
def test_conversion
|
5
|
+
hash = {
|
6
|
+
title: "My Title",
|
7
|
+
authors: [
|
8
|
+
{ id: 1, name: "Mike Anderson" },
|
9
|
+
{ id: 2, name: "Well D." }
|
10
|
+
],
|
11
|
+
active: false
|
12
|
+
}
|
13
|
+
|
14
|
+
book = Dish::Plate.new(hash)
|
15
|
+
assert_equal "My Title", book.title
|
16
|
+
assert_equal 2, book.authors.length
|
17
|
+
assert_equal "Well D.", book.authors[1].name
|
18
|
+
assert_equal true, book.title?
|
19
|
+
assert_equal false, book.active?
|
20
|
+
assert_nil book.other
|
21
|
+
assert_equal false, book.other?
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_hash_helper
|
25
|
+
hash = { a: 1, b: 2 }
|
26
|
+
dish = Dish(hash)
|
27
|
+
assert_instance_of Dish::Plate, dish
|
28
|
+
assert_equal 2, dish.b
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_array_helper
|
32
|
+
array = [
|
33
|
+
{ a: 1, b: 2 },
|
34
|
+
{ c: 3, d: 4 }
|
35
|
+
]
|
36
|
+
dish = Dish(array)
|
37
|
+
assert_instance_of Array, dish
|
38
|
+
assert_equal 2, dish.length
|
39
|
+
dish.each do |d|
|
40
|
+
assert_instance_of Dish::Plate, d
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_using_helper_on_other_types
|
45
|
+
assert_instance_of String, Dish("test")
|
46
|
+
assert_instance_of Fixnum, Dish(3)
|
47
|
+
assert_nil Dish(nil)
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_hash_ext
|
51
|
+
hash = { a: 1, b: 2 }
|
52
|
+
dish = hash.to_dish
|
53
|
+
assert_instance_of Dish::Plate, dish
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_array_ext
|
57
|
+
array = [
|
58
|
+
{ a: 1, b: 2 },
|
59
|
+
{ c: 3, d: 4 }
|
60
|
+
]
|
61
|
+
dish = array.to_dish
|
62
|
+
assert_instance_of Array, dish
|
63
|
+
assert_equal 2, dish.length
|
64
|
+
dish.each do |d|
|
65
|
+
assert_instance_of Dish::Plate, d
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dish
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Lasse Bunk
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-01-13 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.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
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: Super simple conversion of hashes to plain Ruby objects. Great for consuming
|
42
|
+
JSON API's.
|
43
|
+
email:
|
44
|
+
- lassebunk@gmail.com
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- ".gitignore"
|
50
|
+
- Gemfile
|
51
|
+
- LICENSE.txt
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- dish.gemspec
|
55
|
+
- lib/dish.rb
|
56
|
+
- lib/dish/ext.rb
|
57
|
+
- lib/dish/ext/array.rb
|
58
|
+
- lib/dish/ext/hash.rb
|
59
|
+
- lib/dish/plate.rb
|
60
|
+
- lib/dish/version.rb
|
61
|
+
- test/dish_test.rb
|
62
|
+
- test/test_helper.rb
|
63
|
+
homepage: https://github.com/lassebunk/dish
|
64
|
+
licenses:
|
65
|
+
- MIT
|
66
|
+
metadata: {}
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
requirements: []
|
82
|
+
rubyforge_project:
|
83
|
+
rubygems_version: 2.2.1
|
84
|
+
signing_key:
|
85
|
+
specification_version: 4
|
86
|
+
summary: Super simple conversion of hashes to plain Ruby objects
|
87
|
+
test_files:
|
88
|
+
- test/dish_test.rb
|
89
|
+
- test/test_helper.rb
|