diff --git a/lib/ruby-box/item.rb b/lib/ruby-box/item.rb index e0b447c..ee15e91 100644 --- a/lib/ruby-box/item.rb +++ b/lib/ruby-box/item.rb @@ -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? @@ -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={}) diff --git a/spec/item_spec.rb b/spec/item_spec.rb index d4a0a14..f68d78c 100644 --- a/spec/item_spec.rb +++ b/spec/item_spec.rb @@ -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' @@ -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