Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow underscored attribute names other than the ones reserved by the HAL spec #31

Merged
merged 1 commit into from
May 6, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/hyperclient/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/element.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
14 changes: 14 additions & 0 deletions test/hyperclient/attributes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/hyperclient/collection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down