From 12137f177c768e8131335f9a507140ff0d9b7e49 Mon Sep 17 00:00:00 2001 From: karlin Date: Mon, 8 Apr 2013 10:39:52 -0400 Subject: [PATCH] Allow underscored attribute names other than the ones reserved by the HAL spec --- lib/hyperclient/attributes.rb | 4 +++- test/fixtures/element.json | 1 + test/hyperclient/attributes_test.rb | 14 ++++++++++++++ test/hyperclient/collection_test.rb | 2 +- 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/hyperclient/attributes.rb b/lib/hyperclient/attributes.rb index 226b3dc..c57bc38 100644 --- a/lib/hyperclient/attributes.rb +++ b/lib/hyperclient/attributes.rb @@ -9,13 +9,15 @@ module Hyperclient # resource.attributes.title # class Attributes < Collection + RESERVED_PROPERTIES = [/^_links$/, /^_embedded$/] # http://tools.ietf.org/html/draft-kelly-json-hal#section-4.1 + # Public: Initializes the Attributes of a Resource. # # representation - The hash with the HAL representation of the Resource. # def initialize(representation) @collection = if representation.is_a?(Hash) - representation.delete_if {|key, value| key =~ /^_/} + representation.delete_if {|key, value| RESERVED_PROPERTIES.any? {|p| p.match(key) } } else representation end diff --git a/test/fixtures/element.json b/test/fixtures/element.json index 886dc34..90c3844 100644 --- a/test/fixtures/element.json +++ b/test/fixtures/element.json @@ -19,6 +19,7 @@ "title": "Real World ASP.NET MVC3", "description": "In this advanced, somewhat-opinionated production you'll get your very own startup off the ground using ASP.NET MVC 3...", "permitted": true, + "_hidden_attribute": "useful value", "_embedded": { "author": { "_links": { diff --git a/test/hyperclient/attributes_test.rb b/test/hyperclient/attributes_test.rb index da3373a..731103e 100644 --- a/test/hyperclient/attributes_test.rb +++ b/test/hyperclient/attributes_test.rb @@ -19,6 +19,20 @@ module Hyperclient attributes.wont_respond_to :_embedded end + it 'sets normal attributes' do + attributes.must_respond_to :permitted + attributes.permitted.must_equal true + + attributes.must_respond_to :title + attributes.title.must_equal "Real World ASP.NET MVC3" + end + + # Underscores should be allowed per http://tools.ietf.org/html/draft-kelly-json-hal#appendix-B.4 + it 'sets _hidden_attribute as an attribute' do + attributes.must_respond_to :_hidden_attribute + attributes._hidden_attribute.must_equal 'useful value' + end + it 'is a collection' do Attributes.ancestors.must_include Collection end diff --git a/test/hyperclient/collection_test.rb b/test/hyperclient/collection_test.rb index e2704eb..2736b6a 100644 --- a/test/hyperclient/collection_test.rb +++ b/test/hyperclient/collection_test.rb @@ -32,7 +32,7 @@ module Hyperclient name end - names.must_equal ['_links', 'title', 'description', 'permitted', '_embedded'] + names.must_equal ['_links', 'title', 'description', 'permitted', '_hidden_attribute', '_embedded'] end describe '#to_hash' do