Skip to content

Commit

Permalink
set_property is private for later deprecation (possibly). see Resourc…
Browse files Browse the repository at this point in the history
…e#[]=
  • Loading branch information
locks committed Dec 14, 2013
1 parent 4895991 commit be2827c
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 56 deletions.
2 changes: 1 addition & 1 deletion lib/halibut/adapter/json.rb
Expand Up @@ -80,7 +80,7 @@ def extract_properties
.reject {|k,v| k == '_embedded' }

properties.each_pair do |property, value|
@halibut.set_property(property, value)
@halibut[property] = value
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/halibut/adapter/xml.rb
Expand Up @@ -57,7 +57,7 @@ def extract_properties
properties = @document.xpath '/resource/*[not(self::link) and not(self::resource)]'

properties.each do |property|
@resource.set_property property.name, property.content
@resource[property.name] = property.content
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/halibut/builder.rb
Expand Up @@ -44,7 +44,7 @@ def initialize(resource, &blk)
# @param [String] value value of the property
# @return [Halibut::Core::resource] resource with property set
def property(name, value)
@resource.set_property name, value
@resource[name] = value
end

# Adds a link to the respection relation of the resource.
Expand Down
2 changes: 2 additions & 0 deletions lib/halibut/core/relation_map.rb
Expand Up @@ -25,6 +25,8 @@ def initialize
# @param [String] relation relation that the object belongs to
# @param [Object] item the object to add to the relation
def add(relation, item)
return item if item.nil?

@relations[relation] = @relations.fetch(relation, []) << item
end

Expand Down
52 changes: 31 additions & 21 deletions lib/halibut/core/resource.rb
Expand Up @@ -43,7 +43,7 @@ def initialize(href=nil, properties={}, links={}, embedded={})
@embedded = RelationMap.new
@properties = {}

add_link('self', href) if href
add_link('self', resource_href)
end

# Returns the self link of the resource.
Expand All @@ -62,25 +62,19 @@ def namespaces
@links['curie']
end

# Sets a property in the resource.
# Something something
#
# resource = Halibut::Core::Resource.new
# resource.set_property :name, 'FooBar'
# resource.property :name
# # => "FooBar"
#
# @param [Object] property the key
# @param [Object] value the value
def set_property(property, value)
if property == '_links' || property == '_embedded'
raise ArgumentError, "Argument #{property} is a reserved property"
end

tap { @properties[property] = value }
# @param [Object] property key
# @param [Object] value value
def []=(property, value)
tap { set_property(property, value) }
end

def []=(property, value)
tap { @properties[property] = value }

# Something something
#
def [](property)
@properties.fetch(property, nil)
end

# Returns the value of a property in the resource
Expand Down Expand Up @@ -113,10 +107,8 @@ def add_namespace(name, href)
# link.name
# # => "Foo"
#
# @param [String] relation relation
# @param [String] href href
# @param [Hash] opts options: templated, type, name, profile,
# title, hreflang
# @param [String] relation Link relation
# @param [Link,Object] link A Link object, preferably Halibut::Core::Link
def add_link(relation, link)
@links.add relation, link
end
Expand Down Expand Up @@ -149,5 +141,23 @@ def ==(other)
@links == other.links &&
@embedded == other.embedded
end

private
# Sets a property in the resource.
#
# resource = Halibut::Core::Resource.new
# resource.set_property :name, 'FooBar'
# resource.property :name
# # => "FooBar"
#
# @param [Object] property the key
# @param [Object] value the value
def set_property(property, value)
if property == '_links' || property == '_embedded'
raise ArgumentError, "Argument #{property} is a reserved property"
end

tap { @properties[property] = value }
end
end
end
20 changes: 10 additions & 10 deletions spec/adapter/json_spec.rb
Expand Up @@ -20,16 +20,16 @@
subject = Halibut::Adapter::JSON.parse(load_json "serialize")

order = Halibut::Core::Resource.new Halibut::Core::Link.new("/orders/123")
order.set_property "total", 30.00
order.set_property "currency", "USD"
order.set_property "status", "shipped"
order["total"] = 30.00
order["currency"] = "USD"
order["status"] = "shipped"

resource = Halibut::Core::Resource.new Halibut::Core::Link.new("/orders")
resource.add_link "find", Halibut::Core::Link.new("/orders{?id}", templated: true)
resource.add_link "next", Halibut::Core::Link.new("/orders/1", "name" => 'hotdog')
resource.add_link "next", Halibut::Core::Link.new("/orders/9")
resource.set_property "currentlyProcessing", 14
resource.set_property "shippedToday", 20
resource["currentlyProcessing"] = 14
resource["shippedToday"] = 20
resource.embed_resource "orders", order

subject.must_equal resource
Expand All @@ -40,16 +40,16 @@
json = Halibut::Adapter::JSON.render(json)

order = Halibut::Core::Resource.new Halibut::Core::Link.new("/orders/123")
order.set_property "total", 30.00
order.set_property "currency", "USD"
order.set_property "status", "shipped"
order["total"] = 30.00
order["currency"] = "USD"
order["status"] = "shipped"

resource = Halibut::Core::Resource.new Halibut::Core::Link.new("/orders")
resource.add_link "find", Halibut::Core::Link.new("/orders{?id}", templated: true)
resource.add_link "next", Halibut::Core::Link.new("/orders/1", "name" => 'hotdog')
resource.add_link "next", Halibut::Core::Link.new("/orders/9")
resource.set_property "currentlyProcessing", 14
resource.set_property "shippedToday", 20
resource["currentlyProcessing"] = 14
resource["shippedToday"] = 20
resource.embed_resource "orders", order

resource.to_json.wont_equal json
Expand Down
32 changes: 16 additions & 16 deletions spec/builder_spec.rb
Expand Up @@ -27,7 +27,7 @@
end

resource = Halibut::Core::Resource.new
resource.set_property 'foo', 'bar'
resource['foo'] = 'bar'

builder.resource.properties['foo'].must_equal 'bar'
builder.resource.must_equal resource, diff(builder.resource.to_hash, resource.to_hash)
Expand All @@ -41,9 +41,9 @@
end

resource = Halibut::Core::Resource.new
resource.set_property 'foo', 'bar'
resource.set_property 'baz', 'quux'
resource.set_property 'medals', { gold: 1, silver: 5, bronze: 10 }
resource['foo'] = 'bar'
resource['baz'] = 'quux'
resource['medals'] = { gold: 1, silver: 5, bronze: 10 }

builder.resource.properties['foo'].must_equal 'bar'
builder.resource.properties['baz'].must_equal 'quux'
Expand Down Expand Up @@ -71,7 +71,7 @@
end

resource = Halibut::Core::Resource.new
resource.add_link 'cs:broms', Halibut::Core::Link.new('/broms/1')
resource.add_link 'cs:broms', Halibut::Core::Link.new('/broms/1')
resource.add_link 'cs:search', Halibut::Core::Link.new('/search{?broms,noms}', templated: true)

resource.must_equal builder.resource, diff(builder.resource.to_hash, resource.to_hash)
Expand All @@ -85,8 +85,8 @@
end

resource = Halibut::Core::Resource.new
resource.add_link 'cs:broms', Halibut::Core::Link.new('/broms/1')
resource.add_link 'cs:broms', Halibut::Core::Link.new('/broms/2')
resource.add_link 'cs:broms', Halibut::Core::Link.new('/broms/1')
resource.add_link 'cs:broms', Halibut::Core::Link.new('/broms/2')
resource.add_link 'cs:search', Halibut::Core::Link.new('/search{?broms,noms}', templated: true)

resource.must_equal builder.resource, diff(builder.resource.to_hash, resource.to_hash)
Expand All @@ -103,8 +103,8 @@
end

game = Halibut::Core::Resource.new Halibut::Core::Link.new('/game/1')
game.set_property(:name, 'Crash Bandicoot')
game.set_property(:console, 'PlayStation')
game[:name] = 'Crash Bandicoot'
game[:console] = 'PlayStation'

resource = Halibut::Core::Resource.new
resource.embed_resource('games', game)
Expand All @@ -125,12 +125,12 @@
end

game1 = Halibut::Core::Resource.new Halibut::Core::Link.new('/game/1')
game1.set_property(:name, 'Crash Bandicoot')
game1.set_property(:console, 'PlayStation')
game1[:name] = 'Crash Bandicoot'
game1[:console] = 'PlayStation'

game2 = Halibut::Core::Resource.new Halibut::Core::Link.new('/game/2')
game2.set_property(:name, 'Super Mario Land')
game2.set_property(:console, 'Game Boy')
game2[:name] = 'Super Mario Land'
game2[:console] = 'Game Boy'

resource = Halibut::Core::Resource.new
resource.embed_resource('games', game1)
Expand Down Expand Up @@ -158,14 +158,14 @@
end

user = Halibut::Core::Resource.new Halibut::Core::Link.new('/users/1')
user.set_property :name, "foo"
user.set_property :nick, "bar"
user[:name] = "foo"
user[:nick] = "bar"

resource = Halibut::Core::Resource.new
resource.add_link 'games', Halibut::Core::Link.new('/games/1')
resource.add_link 'games', Halibut::Core::Link.new('/games/2')
resource.add_link 'games', Halibut::Core::Link.new('/games/3')
resource.add_link 'next', Halibut::Core::Link.new('/games/next')
resource.add_link 'next', Halibut::Core::Link.new('/games/next')
resource.embed_resource 'users', user

builder.resource.must_equal resource, diff(builder.resource.to_hash, resource.to_hash)
Expand Down
13 changes: 7 additions & 6 deletions spec/core/resource_spec.rb
Expand Up @@ -10,18 +10,20 @@

describe "Properties" do
it "set property" do
subject.set_property "property", "value"
subject["property"] = "value"
subject['warranty'] = "void"

subject.properties['property'].must_equal "value"
subject.property('property').must_equal "value"
subject['warranty'].must_equal 'void'
end

it "fails to set reserved property" do
-> { subject.set_property "_links", "lol" }.must_raise ArgumentError
-> { subject.set_property "_embedded", "lol" }.must_raise ArgumentError
-> { subject["_links"] = "lol" }.must_raise ArgumentError
-> { subject["_embedded"] = "lol" }.must_raise ArgumentError
end

it "read property" do
subject.set_property "property", "value"
subject["property"] = "value"

subject.property('property').must_equal "value"
end
Expand All @@ -38,7 +40,6 @@
it "default" do
link = Halibut::Core::Link.new(normal_uri)
resource = Halibut::Core::Resource.new link

resource.links.wont_be_empty
resource.links['self'].first.must_equal link
resource.href.must_equal normal_uri
Expand Down

0 comments on commit be2827c

Please sign in to comment.