Skip to content
Open
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
12 changes: 10 additions & 2 deletions lib/ruby-box/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ def method_missing(method, *args, &block)
# Support has many and paginated has many relationships.
return many(key) if @@has_many.include?(key)
return paginated(key, args[0] || 100, args[1] || 0, args[2]) if @@has_many_paginated.include?(key)

# update @raw_item hash if this appears to be a setter.
setter = method.to_s.end_with?('=')
key = key[0...-1] if setter
@raw_item[key] = args[0] if setter and update_fields.include?(key)

# we may have a mini version of the object loaded, fix this.
reload_meta if @raw_item[key].nil? and has_mini_format?

Expand All @@ -93,6 +93,14 @@ def method_missing(method, *args, &block)
end
end

def respond_to_missing?(method, include_private = false)
method = method.to_s
key = method
key = key[0...-1] if method.end_with?('=')

@raw_item.key?(key) || @@has_many.include?(method) || @@has_many_paginated.include?(method) || super
end

# see http://developers.box.com/docs/#folders-create-a-shared-link-for-a-folder
# for a list of valid options.
def create_shared_link(opts={})
Expand Down
25 changes: 24 additions & 1 deletion spec/item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
end

describe '#factory' do

it 'creates an object from a web_link hash' do
web_link = RubyBox::Item.factory(@session, {
'type' => 'web_link'
Expand All @@ -28,4 +28,27 @@
banana.instance_of?(RubyBox::Item).should == true
end
end

describe '#respond_to?' do
let(:mixtape) { RubyBox::Item.factory(@session, { 'type' => 'mixtape', 'id' => '123' }) }

it 'should not respond to undefined keys' do
mixtape.should_not respond_to(:artist)
end

it 'should respond to methods that are delegated to the raw item' do
mixtape.should respond_to(:type)
mixtape.should respond_to(:id)
end

context "with relationships" do
before do
RubyBox::Item.has_many("tracks")
end

it 'should respond to relationship keys' do
mixtape.should respond_to(:tracks)
end
end
end
end