Giter Site home page Giter Site logo

tushar-kumolus / representable Goto Github PK

View Code? Open in Web Editor NEW

This project forked from trailblazer/representable

0.0 0.0 0.0 3.1 MB

Maps representation documents from and to Ruby objects. Includes JSON, XML and YAML support, plain properties and compositions.

Home Page: http://trailblazer.to/2.1/docs/representable.html

License: MIT License

Ruby 100.00%

representable's Introduction

Representable

Representable maps Ruby objects to documents and back.

Gitter Chat TRB Newsletter Build Status Gem Version

In other words: Take an object and decorate it with a representer module. This will allow you to render a JSON, XML or YAML document from that object. But that's only half of it! You can also use representers to parse a document and create or populate an object.

Representable is helpful for all kind of mappings, rendering and parsing workflows. However, it is mostly useful in API code. Are you planning to write a real REST API with representable? Then check out the Roar gem first, save work and time and make the world a better place instead.

Full Documentation

Representable comes with a rich set of options and semantics for parsing and rendering documents. Its full documentation can be found on the Trailblazer site.

Example

What if we're writing an API for music - songs, albums, bands.

class Song < OpenStruct
end

song = Song.new(title: "Fallout", track: 1)

Defining Representations

Representations are defined using representer classes, called _decorator, or modules.

In these examples, let's use decorators

class SongRepresenter < Representable::Decorator
  include Representable::JSON

  property :title
  property :track
end

In the representer the #property method allows declaring represented attributes of the object. All the representer requires for rendering are readers on the represented object, e.g. #title and #track. When parsing, it will call setters - in our example, that'd be #title= and #track=.

Rendering

Mixing in the representer into the object adds a rendering method.

SongRepresenter.new(song).to_json
#=> {"title":"Fallout","track":1}

Parsing

It also adds support for parsing.

song = SongRepresenter.new(song).from_json(%{ {"title":"Roxanne"} })
#=> #<Song title="Roxanne", track=nil>

Note that parsing hashes per default does require string keys and does not pick up symbol keys.

Collections

Let's add a list of composers to the song representation.

class SongRepresenter < Representable::Decorator
  include Representable::JSON

  property :title
  property :track
  collection :composers
end

Surprisingly, #collection lets us define lists of objects to represent.

Song.new(title: "Fallout", composers: ["Stewart Copeland", "Sting"]).
  extend(SongRepresenter).to_json

#=> {"title":"Fallout","composers":["Stewart Copeland","Sting"]}

And again, this works both ways - in addition to the title it extracts the composers from the document, too.

Nesting

Representers can also manage compositions. Why not use an album that contains a list of songs?

class Album < OpenStruct
end

album = Album.new(name: "The Police", songs: [song, Song.new(title: "Synchronicity")])

Here comes the representer that defines the composition.

class AlbumRepresenter < Representable::Decorator
  include Representable::JSON

  property :name
  collection :songs, decorator: SongRepresenter, class: Song
end

Inline Representers

If you don't want to maintain two separate modules when nesting representations you can define the SongRepresenter inline.

class AlbumRepresenter < Representable::Decorator
  include Representable::JSON

  property :name

  collection :songs, class: Song do
    property :title
    property :track
    collection :composers
  end
end

More

Representable has many more features and can literally parse and render any kind of document to an arbitrary Ruby object graph.

Please check the official documentation for more.

Installation

The representable gem runs with all Ruby versions >= 2.4.0. t

gem 'representable'

Dependencies

Representable does a great job with JSON, it also features support for XML, YAML and pure ruby hashes. But Representable did not bundle dependencies for JSON and XML.

If you want to use JSON, add the following to your Gemfile:

gem 'multi_json'

If you want to use XML, add the following to your Gemfile:

gem 'nokogiri'

Copyright

Representable started as a heavily simplified fork of the ROXML gem. Big thanks to Ben Woosley for his extremely inspiring work.

  • Copyright (c) 2011-2020 Nick Sutterer [email protected]
  • ROXML is Copyright (c) 2004-2009 Ben Woosley, Zak Mandhro and Anders Engstrom.

Representable is released under the MIT License.

representable's People

Contributors

apotonick avatar empact avatar abinoam avatar seuros avatar yogeshjain999 avatar yob avatar dblock avatar guiocavalcanti avatar timoschilling avatar myabc avatar vihai avatar dplummer avatar rsutphin avatar shirren avatar d4rky-pl avatar jandudulski avatar fran-worley avatar scharfie avatar and0x000 avatar nashby avatar tylerrick avatar sadjow avatar luxflux avatar parndt avatar nakajima avatar faucct avatar sheax0r avatar mmartinson avatar michaelmior avatar mrkcor 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.