• Stars
    star
    291
  • Rank 137,438 (Top 3 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 10 years ago
  • Updated over 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Super simple conversion of hashes into plain Ruby objects. Also works in RubyMotion.

Build Status

Dish!

Dish

Very simple conversion of hashes to plain Ruby objects. This is great for consuming JSON API's which is what I use it for. Also works in RubyMotion.

Installation

Add this line to your application's Gemfile:

gem "dish"

Then run:

$ bundle

Or install it yourself:

$ gem install dish

If you want a to_dish helper method added to your Hash and Array objects, you can require dish/ext in your Gemfile:

gem "dish", require: "dish/ext"

Installation in RubyMotion

Dish fully supports RubyMotion, enabling you to easily consume JSON API's in your Ruby iOS apps.

For installation in RubyMotion, add this line to your project's Gemfile:

gem "dish", require: "dish/motion"

Then run:

$ bundle

And you're good to go.

NOTE: If you're using Dish with the BubbleWrap JSON module, please see below.

Example

hash = {
  title: "My Title",
  authors: [
    { id: 1, name: "Mike Anderson" },
    { id: 2, name: "Well D." }
  ],
  active: false
}

book = Dish(hash) # or hash.to_dish if you required "dish/ext"
book.title           # => "My Title"
book.authors.length  # => 2
book.authors[1].name # => "Well D."
book.title?          # => true
book.active?         # => false
book.other           # => nil
book.other?          # => false

Coercion

Values can automatically be coerced, for example into a custom Dish object or a Time, for example if you have an updated_at in the source.

class Author < Dish::Plate; end

class Product < Dish::Plate
  coerce :updated_at, ->(value) { Time.parse(value) }
  coerce :authors, Author
end

source_products = [
  {
    title: "Test Product",
    updated_at: "2013-01-28 13:23:11",
    authors: [
      { id: 1, name: "First Author" },
      { id: 2, name: "Second Author" }
    ]
  },
  {
    title: "Second Product",
    updated_at: "2012-07-11 19:54:07",
    authors: [
      { id: 1, name: "Third Author" },
      { id: 2, name: "Fourth Author" }
    ]
  }
]

products = Dish(source_products, Product)
products.first.updated_at    # => instance of Time (2013-01-28 13:23:11)
products.first.authors.first # => instance of Author

# If you required "dish/ext", you can also:
products = source_products.to_dish(Product)

# The above example uses an array. You can do the same directly on a hash:
hash = { title: "My Product", updated_at: "2014-01-15 09:12:45" }
product = Dish(hash, Product) # => instance of Product
product = hash.to_dish(Product) # => instance of Product when using "dish/ext"

This is inspired by Hashie's coercion methods.

Have fun!

Converting back to Ruby/JSON objects

You can use the Dish::Plate#to_h method for accessing the original hash. In addition Dish::Plate#to_json can be used for marshaling JSON if you are using RubyMotion (NSJSONSerialization is used) or have required the "json" Ruby stdlib.

NOTE: Previously Dish::Plate#to_h was called Dish::Plate#as_hash. The Dish::Plate#as_hash method is now deprecated.

Notes

Using with the BubbleWrap JSON module

When you use the BubbleWrap gem to parse JSON into a hash, you can't use the Hash#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 fixing this, but in the meanwhile you can achieve the same result by doing this:

BW::HTTP.get("http://path.to/api/books/2") do |response|
  json = BW::JSON.parse(response.body.to_s)
  book = Dish(json) # This is the actual conversion

  title_label.text = book.title
  author_label.text = book.authors.map(&:name).join(", ")
end

Contributing

Feature additions are very welcome, but please submit an issue first, so we can discuss it in advance. Thanks.

  1. Fork the project
  2. Create a feature branch (git checkout -b my-new-feature)
  3. Make your changes, including tests so it doesn't break in the future
  4. Commit your changes (git commit -am 'Add feature')
  5. Push to the branch (git push origin my-new-feature)
  6. Create new pull request

More Repositories

1

gretel

Flexible Ruby on Rails breadcrumbs plugin.
Ruby
890
star
2

metamagic

Simple Ruby on Rails plugin for creating meta tags.
Ruby
437
star
3

dynamic_sitemaps

Dynamic sitemap generation plugin for Ruby on Rails.
Ruby
206
star
4

human_power

Easy generation of robots.txt. Force the robots into submission!
Ruby
96
star
5

item

Easy-to-use semantic markup and microdata for Ruby on Rails.
Ruby
17
star
6

api_builder

Ruby on Rails template engine that allows for multiple formats being laid out in a single specification.
Ruby
13
star
7

sitemap_notifier

Ruby on Rails plugin that automatically notifies Google, Bing, and Yahoo of changes to your models, i.e. changes to your sitemap.
Ruby
12
star
8

gretel-trails

Strategies for handling Gretel trails.
Ruby
10
star
9

screenshot-generator

WordPress plugin for taking automatic screenshots of posts for social media etc.
PHP
8
star
10

acts_as_translatable

Ruby on Rails plugin for easy translation of Ruby on Rails models and database tables.
Ruby
7
star
11

webcam_app

Demo on how to get your webcam working with Rails 3
Ruby
5
star
12

checkins_app

Demo of a location aware app that stores check-ins based on current location, shows nearby check-ins, and displays check-ins on a map.
Ruby
5
star
13

wp-infinite-scrolling

Free and simple infinite scrolling plugin for WordPress.
PHP
3
star
14

jquery.sifs

The simplest infinite scrolling library you will ever find.
JavaScript
3
star
15

jquery.truncate

Simple jQuery plugin for doing text truncation.
JavaScript
2
star
16

bing_translate_yaml

Simple Ruby on Rails plugin for translating your YAML files using Bing.
Ruby
2
star
17

bing_translate_acts_as_translatable

Ruby on Rails plugin for easy translation of your acts_as_translatable models.
Ruby
1
star
18

wp-cleaner

Joins WordPress assets into a single CSS and JS file.
PHP
1
star
19

shortie

Shorten URLs with any service
Ruby
1
star
20

translate_acts_as_translatable_models

Ruby on Rails plugin to translate your acts_as_translatable models using Bing.
Ruby
1
star
21

stringex_friendly_id

Gem for using Stringex's ingenious unicode transliterations with FriendlyId's slugging magic.
Ruby
1
star
22

shortservice

A layer for talking to the ShortService API.
Ruby
1
star