dish 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ef440675192f5eb0ed6d027a9ed8efc0ac91eb57
4
- data.tar.gz: 4715d8232e09e33e6c2fa440e5fcc78459c83735
3
+ metadata.gz: b80bdc6eac863a3ca01536cb0af8034ebfdab5a3
4
+ data.tar.gz: 993e1729fc762ebbdd7857e1c17f162a3c48d476
5
5
  SHA512:
6
- metadata.gz: 6ecd4d47090d3834d67b492b2b33cc85910a816c9bc5ab9adcaa1c155eaa9ba4ab162d58e7ce48829434d293ddbaf3bb633fbc6546de3c183179e33750297acf
7
- data.tar.gz: 13258764d91e10de3065eab64e1ffc57a972abcd181f230192dc4d8871263ece0a1ebeb7ee943428b078b4c5b9f57ef807cf24138cbc458341997fe3073f5750
6
+ metadata.gz: 7b219fc03d52b0dbdf9610d583cd2e76af17665fefec414dadade3bbf9c8379ef070923d0b4715903e09834bb3538196c2e97156eec41028a53b505b40ac8b86
7
+ data.tar.gz: 7fc189d6b1fee5c77d6ea06a8afeb40f5009be2797fe35a885acf71cd6c6a994170b0a131163cd694e77e47609b1b9b4490460392f900f320a2e09e054c8bbf7
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.0
4
+ - 2.0.0
5
+ - 1.9.3
6
+ notifications:
7
+ email: false
data/README.md CHANGED
@@ -1,5 +1,9 @@
1
+ [![Build Status](https://secure.travis-ci.org/lassebunk/dish.png)](http://travis-ci.org/lassebunk/dish)
2
+
1
3
  # Dish!
2
4
 
5
+ ![Dish](http://i.imgur.com/FP1DJmt.png)
6
+
3
7
  Very simple conversion of hashes to plain Ruby objects.
4
8
  This is great for consuming JSON API's which is what I use it for.
5
9
 
@@ -21,6 +25,22 @@ If you want a `to_dish` helper method added to your Hash and Array objects, you
21
25
 
22
26
  gem "dish", require: "dish/ext"
23
27
 
28
+ ### Installation in RubyMotion
29
+
30
+ Dish fully supports [RubyMotion](http://www.rubymotion.com/), enabling you to easily consume JSON API's in your Ruby iOS apps.
31
+
32
+ For installation in RubyMotion, add this line to your project's *Gemfile*:
33
+
34
+ gem "dish", require: "dish/motion"
35
+
36
+ Then run:
37
+
38
+ $ bundle
39
+
40
+ And you're good to go.
41
+
42
+ Note: If you're using Dish with the BubbleWrap JSON module, please see below.
43
+
24
44
  # Example
25
45
 
26
46
  hash = {
@@ -41,6 +61,22 @@ If you want a `to_dish` helper method added to your Hash and Array objects, you
41
61
  book.other # => nil
42
62
  book.other? # => false
43
63
 
64
+ ## Notes
65
+
66
+ ### Using with the BubbleWrap JSON module
67
+
68
+ When you use the [BubbleWrap](https://github.com/rubymotion/BubbleWrap) gem to parse JSON into a hash, you can't use the
69
+ `to_dish` methods directly because the `BW::JSON` module returns some sort of hash that hasn't got the methods from the real hash. I'm
70
+ fixing this, but in the meanwhile you can achieve the same result by doing this:
71
+
72
+ BW::HTTP.get("http://path.to/api/books/2") do |response|
73
+ json = BW::JSON.parse(response.body.to_s)
74
+ book = Dish(json) # This is the actual conversion
75
+
76
+ title_label.text = book.title
77
+ author_label.text = book.authors.map(&:name).join(", ")
78
+ end
79
+
44
80
  ## Contributing
45
81
 
46
82
  Feature additions are very welcome, but please submit an issue first, so we can discuss it in advance. Thanks.
data/lib/dish.rb CHANGED
@@ -1,10 +1,3 @@
1
1
  require "dish/version"
2
2
  require "dish/plate"
3
-
4
- def Dish(hash_or_array)
5
- case hash_or_array
6
- when Hash then Dish::Plate.new(hash_or_array)
7
- when Array then hash_or_array.map { |v| Dish(v) }
8
- else hash_or_array
9
- end
10
- end
3
+ require "dish/helper"
@@ -0,0 +1,7 @@
1
+ def Dish(hash_or_array)
2
+ case hash_or_array
3
+ when Hash then Dish::Plate.new(hash_or_array)
4
+ when Array then hash_or_array.map { |v| Dish(v) }
5
+ else hash_or_array
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ unless defined?(Motion::Project::Config)
2
+ raise "This file must be required within a RubyMotion project Rakefile."
3
+ end
4
+
5
+ Motion::Project::App.setup do |app|
6
+ Dir[File.join(File.dirname(__FILE__), "**/*.rb")].each do |file|
7
+ app.files.unshift(file) unless file =~ /\/(ext|motion)\.rb$/ # Exclude ext.rb because it uses `require`
8
+ end
9
+ end
data/lib/dish/plate.rb CHANGED
@@ -1,27 +1,30 @@
1
1
  module Dish
2
2
  class Plate
3
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
4
+ @dish_original_hash = Hash[hash.map { |k, v| [k.to_s, v] }]
9
5
  end
10
6
 
11
7
  def method_missing(method, *args, &block)
12
8
  method = method.to_s
13
9
  if method.end_with?("?")
14
- field = method[0..-2]
15
- dish_check_for_presence(send(field))
10
+ key = method[0..-2]
11
+ dish_check_for_presence(key)
16
12
  else
17
- nil
13
+ dish_get_value(method)
18
14
  end
19
15
  end
20
16
 
21
17
  private
22
18
 
23
- def dish_check_for_presence(value)
24
- !!value
19
+ attr_reader :dish_original_hash
20
+
21
+ def dish_get_value(key)
22
+ value = dish_original_hash[key]
23
+ dish_convert_value(value)
24
+ end
25
+
26
+ def dish_check_for_presence(key)
27
+ !!dish_get_value(key)
25
28
  end
26
29
 
27
30
  def dish_convert_value(value)
data/lib/dish/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Dish
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/test/dish_test.rb CHANGED
@@ -16,11 +16,31 @@ class DishTest < Test::Unit::TestCase
16
16
  assert_equal 2, book.authors.length
17
17
  assert_equal "Well D.", book.authors[1].name
18
18
  assert_equal true, book.title?
19
+ assert_equal false, book.active
19
20
  assert_equal false, book.active?
20
21
  assert_nil book.other
21
22
  assert_equal false, book.other?
22
23
  end
23
24
 
25
+ def test_key_type_indifference
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::Plate.new(hash)
36
+ assert_equal "My Title", book.title
37
+ assert_equal 2, book.authors.length
38
+ assert_equal "Well D.", book.authors[1].name
39
+ assert_equal true, book.title?
40
+ assert_equal false, book.active
41
+ assert_equal false, book.active?
42
+ end
43
+
24
44
  def test_hash_helper
25
45
  hash = { a: 1, b: 2 }
26
46
  dish = Dish(hash)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dish
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lasse Bunk
@@ -47,6 +47,7 @@ extensions: []
47
47
  extra_rdoc_files: []
48
48
  files:
49
49
  - ".gitignore"
50
+ - ".travis.yml"
50
51
  - Gemfile
51
52
  - LICENSE.txt
52
53
  - README.md
@@ -56,6 +57,8 @@ files:
56
57
  - lib/dish/ext.rb
57
58
  - lib/dish/ext/array.rb
58
59
  - lib/dish/ext/hash.rb
60
+ - lib/dish/helper.rb
61
+ - lib/dish/motion.rb
59
62
  - lib/dish/plate.rb
60
63
  - lib/dish/version.rb
61
64
  - test/dish_test.rb