Last active
October 3, 2016 21:21
-
-
Save steveh/dec45cb0f4d1289848c473f84a13aae1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "roar/coercion" | |
require "roar/json/hal" | |
require "json" | |
module CategoryRepresenter | |
include Roar::JSON::HAL | |
include Roar::Hypermedia | |
include Roar::Coercion | |
property :name | |
end | |
class Category | |
include Roar::JSON | |
include CategoryRepresenter | |
attr_accessor :name | |
end | |
module PostRepresenter | |
include Roar::JSON::HAL | |
include Roar::Hypermedia | |
include Roar::Coercion | |
property :name | |
property :category, class: Category do | |
property :name | |
end | |
end | |
class Post | |
include Roar::JSON | |
include PostRepresenter | |
attr_accessor :name, :category | |
end | |
module CollectionRepresenter | |
include Roar::JSON::HAL | |
include Roar::Hypermedia | |
include Roar::Coercion | |
property :name | |
property :post, class: Post do | |
property :name | |
end | |
end | |
class Collection | |
include Roar::JSON | |
include CollectionRepresenter | |
attr_accessor :post | |
end | |
post_hash = JSON.load('{"name":"post","category":{"name":"cat"}}') | |
post = Post.new.from_hash(post_hash) | |
puts post_hash["category"]["name"] | |
# => cat | |
puts post.category.name | |
# => cat | |
col_hash = JSON.load('{"post":{"name":"post","category":{"name":"cat"}}}') | |
col = Collection.new.from_hash(col_hash) | |
puts col_hash["post"]["category"]["name"] | |
# => cat | |
puts col.post.category.name | |
# => NoMethodError |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment