diff --git a/lib/halibut/adapter/json.rb b/lib/halibut/adapter/json.rb index 2b18c40..cf6d4c9 100644 --- a/lib/halibut/adapter/json.rb +++ b/lib/halibut/adapter/json.rb @@ -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 diff --git a/lib/halibut/adapter/xml.rb b/lib/halibut/adapter/xml.rb index d51caec..4867065 100644 --- a/lib/halibut/adapter/xml.rb +++ b/lib/halibut/adapter/xml.rb @@ -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 diff --git a/lib/halibut/builder.rb b/lib/halibut/builder.rb index 3069458..9bcf460 100644 --- a/lib/halibut/builder.rb +++ b/lib/halibut/builder.rb @@ -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. diff --git a/lib/halibut/core/relation_map.rb b/lib/halibut/core/relation_map.rb index 7cbe1e4..b94be6a 100644 --- a/lib/halibut/core/relation_map.rb +++ b/lib/halibut/core/relation_map.rb @@ -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 diff --git a/lib/halibut/core/resource.rb b/lib/halibut/core/resource.rb index 49fc7cc..8c65e30 100644 --- a/lib/halibut/core/resource.rb +++ b/lib/halibut/core/resource.rb @@ -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. @@ -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 @@ -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 @@ -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 \ No newline at end of file diff --git a/spec/adapter/json_spec.rb b/spec/adapter/json_spec.rb index bd70c6e..134fac2 100644 --- a/spec/adapter/json_spec.rb +++ b/spec/adapter/json_spec.rb @@ -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 @@ -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 diff --git a/spec/builder_spec.rb b/spec/builder_spec.rb index 3a82f7d..2d917f4 100644 --- a/spec/builder_spec.rb +++ b/spec/builder_spec.rb @@ -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) @@ -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' @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) diff --git a/spec/core/resource_spec.rb b/spec/core/resource_spec.rb index 2fc6df6..c1e7686 100644 --- a/spec/core/resource_spec.rb +++ b/spec/core/resource_spec.rb @@ -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 @@ -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