Skip to content

Commit

Permalink
Merge branch 'refresh_link'
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermesilveira committed Oct 24, 2010
2 parents 01b2563 + 14d28fa commit c973bf1
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 5 deletions.
3 changes: 2 additions & 1 deletion lib/restfulie/common.rb
Expand Up @@ -4,14 +4,15 @@
require 'rubygems'
require 'active_support'
require 'action_controller'
require 'restfulie/common/core_ext'

module Restfulie
module Common
autoload :Error, 'restfulie/common/error'
autoload :Links, 'restfulie/common/links'
autoload :Logger, 'restfulie/common/logger'
autoload :Representation, 'restfulie/common/representation'
autoload :Converter, 'restfulie/common/converter'
end
end

require 'restfulie/common/core_ext'
6 changes: 6 additions & 0 deletions lib/restfulie/common/core_ext/hash.rb
@@ -1,12 +1,18 @@
class Hash

def links(*args)
links = fetch("link", [])
Restfulie::Common::Converter::Xml::Links.new(links)
end

include Restfulie::Common::Links

def method_missing(sym, *args)
self[sym.to_s].nil? ? super(sym, args) : self[sym.to_s]
end

def respond_to?(sym)
include?(sym.to_s) || super(sym)
end

end
9 changes: 9 additions & 0 deletions lib/restfulie/common/links.rb
@@ -0,0 +1,9 @@
module Restfulie
module Common
module Links
def refresh
links.self.follow.get
end
end
end
end
5 changes: 1 addition & 4 deletions lib/restfulie/common/representation.rb
@@ -1,6 +1,3 @@
module Restfulie::Common::Representation
autoload :Atom, 'restfulie/common/representation/atom'
autoload :Generic, 'restfulie/common/representation/generic'
autoload :Json, 'restfulie/common/representation/json'
autoload :Links, 'restfulie/common/representation/links'
Dir["#{File.dirname(__FILE__)}/representation/*.rb"].each {|f| autoload File.basename(f)[0..-4].camelize.to_sym, f }
end
2 changes: 2 additions & 0 deletions lib/restfulie/common/representation/atom/base.rb
Expand Up @@ -104,6 +104,8 @@ def contributors
@contributors
end

include Restfulie::Common::Links

# It has one required attribute, href, and five optional attributes: rel, type, hreflang, title, and length
def links
return @links if @links
Expand Down
2 changes: 2 additions & 0 deletions lib/restfulie/common/representation/json/keys_as_methods.rb
Expand Up @@ -44,6 +44,8 @@ def __metaclass__
class << self; self; end
end

include Restfulie::Common::Links

# easy accessors to links
def links
some_links = self["link"]
Expand Down
46 changes: 46 additions & 0 deletions spec/unit/common/links_spec.rb
@@ -0,0 +1,46 @@
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')

describe Restfulie::Common::Links do

class RefreshieLinks
include Restfulie::Common::Links
def initialize(what)
@what = what
end
def links
RefreshieSelf.new(@what)
end
end

class RefreshieSelf
def initialize(what)
@what = what
end
def self
RefreshieFollow.new(@what)
end
end

class RefreshieGet
def initialize(what)
@what = what
end
def get
@what
end
end

class RefreshieFollow
def initialize(what)
@what = what
end
def follow
RefreshieGet.new(@what)
end
end

it "should get self when refreshing itself" do
RefreshieLinks.new("myself").refresh.should == "myself"
end

end

0 comments on commit c973bf1

Please sign in to comment.