Giter Site home page Giter Site logo

dish's Introduction

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

dish's People

Contributors

aredondo avatar erran avatar lassebunk avatar markusharmsen avatar mbiffara avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.