Skip to content

Commit

Permalink
add Roar::JSON::Collection and :Hash.
Browse files Browse the repository at this point in the history
  • Loading branch information
apotonick committed Oct 20, 2014
1 parent 527a392 commit 286f714
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 12 deletions.
13 changes: 6 additions & 7 deletions TODO.markdown
@@ -1,14 +1,13 @@
* alias Roar::Representer to Representer
Roar::Hypermedia
# 1.0

* merge client errors request
* simpler link for JSON-API?


* Hyperlink representers => decrators. test hash representer with decorator (rpr)


* Add proxies, so nested models can be lazy-loaded.
* move #prepare_links! call to #_links or something so it doesn't need to be called in #serialize.
* abstract ::links_definition_options and move them out of the generic representers (JSON, XML).
* make 1.8 tests work, again (hash ordering!)

* release 1.0
* representable 1.8, only.
* revise Hypermedia
* work on HAL-Object
6 changes: 3 additions & 3 deletions lib/roar/hypermedia.rb
Expand Up @@ -72,7 +72,7 @@ def compile_links_for(configs, *args)
end

def prepare_link_for(href, options)
options = options.merge(href.is_a?(Hash) ? href : {:href => href})
options = options.merge(href.is_a?(::Hash) ? href : {:href => href})
Hyperlink.new(options)
end

Expand All @@ -86,7 +86,7 @@ def run_link_block(block, *args)
# It is implemented as a hash and keys links by their rel value.
#
# {"self" => <Hyperlink ..>, ..}
class LinkCollection < Hash
class LinkCollection < ::Hash
# The only way to create is LinkCollection[<Hyperlink>, <Hyperlink>]
def self.[](*arr)
super(arr.collect { |link| [link.rel, link] })
Expand Down Expand Up @@ -119,7 +119,7 @@ module ClassMethods
def link(options, &block)
create_links_definition! # this assures the links are rendered at the right position.

options = {:rel => options} unless options.is_a?(Hash)
options = {:rel => options} unless options.is_a?(::Hash)
link_configs << [options, block]
end

Expand Down
3 changes: 3 additions & 0 deletions lib/roar/json/collection.rb
@@ -0,0 +1,3 @@
require "representable/json/collection"

Roar::JSON::Collection = Representable::JSON::Collection
2 changes: 1 addition & 1 deletion lib/roar/json/collection_json.rb
Expand Up @@ -21,7 +21,7 @@ def __template
OpenStruct.new # TODO: handle preset values.
end

collection :queries, :extend => Roar::JSON::HyperlinkRepresenter, :class => lambda { |fragment,*| Hash }
collection :queries, :extend => Roar::JSON::HyperlinkRepresenter, :class => lambda { |fragment,*| ::Hash }
def queries
compile_links_for(representable_attrs.collection_representers[:queries].link_configs)
end
Expand Down
3 changes: 3 additions & 0 deletions lib/roar/json/hash.rb
@@ -0,0 +1,3 @@
require "representable/json/hash"

Roar::JSON::Hash = Representable::JSON::Hash
2 changes: 1 addition & 1 deletion lib/roar/json/json_api.rb
Expand Up @@ -161,7 +161,7 @@ def compile_compound!(linked, compound)
linked.each { |k,v| # {"album"=>{"title"=>"Hackers"}, "musicians"=>[{"name"=>"Eddie Van Halen"}, {"name"=>"Greg Howe"}]}
compound[k] ||= []

if v.is_a?(Hash) # {"title"=>"Hackers"}
if v.is_a?(::Hash) # {"title"=>"Hackers"}
compound[k] << v
else
compound[k].push(*v) # [{"name"=>"Eddie Van Halen"}, {"name"=>"Greg Howe"}]
Expand Down
9 changes: 9 additions & 0 deletions test/lonely_test.rb
@@ -0,0 +1,9 @@
require 'test_helper'

require "roar/json/collection"
require "roar/json/hash"

class LonelyCollectionTest < MiniTest::Spec
it { Roar::JSON::Collection.must_equal Representable::JSON::Collection }
it { Roar::JSON::Hash.must_equal Representable::JSON::Hash }
end

0 comments on commit 286f714

Please sign in to comment.