Permalink
Browse files

initial commit

  • Loading branch information...
dburkes committed Aug 24, 2011
0 parents commit 68fcf1ee5df27d63206bccbe55368705f6a4326f
@@ -0,0 +1,4 @@
+pkg/*
+*.gem
+.bundle
+Gemfile.lock
1 .rvmrc
@@ -0,0 +1 @@
+rvm use ruby-1.9.2-p180@databasedotcom-rails --create
@@ -0,0 +1,7 @@
+source "http://rubygems.org"
+
+# Specify your gem's dependencies in databasedotcom_rails.gemspec
+gemspec
+
+gem 'databasedotcom', :git => "https://github.com/heroku/databasedotcom.git"
+
@@ -0,0 +1,7 @@
+require 'bundler'
+require 'rspec/core/rake_task'
+
+Bundler::GemHelper.install_tasks
+RSpec::Core::RakeTask.new(:spec)
+
+task :default => :spec
@@ -0,0 +1,20 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "databasedotcom/rails/version"
+
+Gem::Specification.new do |s|
+ s.name = "databasedotcom-rails"
+ s.version = Databasedotcom::Rails::VERSION
+ s.platform = Gem::Platform::RUBY
+ s.authors = ["Danny Burkes"]
+ s.email = ["dburkes@netable.com"]
+ s.homepage = "http://github.com/dburkes/databasedotcom-rails"
+ s.summary = %q{Convenience classes to make using the databasedotcom gem with Rails apps even easier}
+ s.description = %q{Convenience classes to make using the databasedotcom gem with Rails apps even easier}
+
+ s.files = Dir['README.rdoc', 'MIT-LICENSE', 'lib/**/*']
+ s.require_paths = ["lib"]
+ s.add_dependency('databasedotcom')
+ s.add_development_dependency('rspec', '2.6.0')
+ s.add_development_dependency('rake', '0.8.6')
+end
@@ -0,0 +1 @@
+require 'databasedotcom/rails'
@@ -0,0 +1,3 @@
+require 'databasedotcom'
+require 'databasedotcom/rails/version'
+require 'databasedotcom/rails/controller'
@@ -0,0 +1,54 @@
+module Databasedotcom
+ module Rails
+ module Controller
+ module ClassMethods
+ def dbdc_client
+ unless @dbdc_client
+ config = YAML.load_file(File.join(::Rails.root, 'config', 'databasedotcom.yml'))
+ username = config["username"]
+ password = config["password"]
+ @dbdc_client = Databasedotcom::Client.new(config)
+ @dbdc_client.authenticate(:username => username, :password => password)
+ end
+
+ @dbdc_client
+ end
+
+ def dbdc_client=(client)
+ @dbdc_client = client
+ end
+
+ def sobject_types
+ unless @sobject_types
+ @sobject_types = dbdc_client.list_sobjects
+ end
+
+ @sobject_types
+ end
+
+ def const_missing(sym)
+ if sobject_types.include?(sym.to_s)
+ dbdc_client.materialize(sym.to_s)
+ else
+ super
+ end
+ end
+ end
+
+ module InstanceMethods
+ def dbdc_client
+ self.class.dbdc_client
+ end
+
+ def sobject_types
+ self.class.sobject_types
+ end
+ end
+
+ def self.included(base)
+ base.send(:include, InstanceMethods)
+ base.send(:extend, ClassMethods)
+ end
+ end
+ end
+end
@@ -0,0 +1,5 @@
+module Databasedotcom
+ module Rails
+ VERSION = "0.0.1"
+ end
+end
@@ -0,0 +1,93 @@
+require 'spec_helper'
+
+describe Databasedotcom::Rails::Controller do
+ class TestController
+ include Databasedotcom::Rails::Controller
+
+ def reference_Foo
+ Foo.create
+ end
+
+ def reference_Bar
+ Bar.create
+ end
+ end
+
+ describe ".dbdc_client" do
+ before(:each) do
+ config_hash = { "client_id" => "client_id", "client_secret" => "client_secret", "username" => "foo", "password" => "bar" }
+ YAML.should_receive(:load_file).and_return(config_hash)
+ end
+
+ after(:each) do
+ TestController.dbdc_client = nil
+ end
+
+ it "constructs and authenticates a Databasedotcom::Client" do
+ Databasedotcom::Client.any_instance.should_receive(:authenticate).with(:username => "foo", :password => "bar")
+ TestController.dbdc_client
+ end
+
+ it "is memoized" do
+ Databasedotcom::Client.any_instance.should_receive(:authenticate).exactly(1).times.with(:username => "foo", :password => "bar")
+ TestController.dbdc_client
+ TestController.dbdc_client
+ end
+ end
+
+ describe ".sobject_types" do
+ before(:each) do
+ @client_double = double("client")
+ TestController.should_receive(:dbdc_client).any_number_of_times.and_return(@client_double)
+ end
+
+ after(:each) do
+ TestController.instance_variable_set("@sobject_types", nil)
+ end
+
+ it "requests the sobject types from the client" do
+ @client_double.should_receive(:list_sobjects)
+ TestController.sobject_types
+ end
+
+ it "is memoized" do
+ @client_double.should_receive(:list_sobjects).exactly(1).times.and_return(%w(foo bar))
+ TestController.sobject_types
+ TestController.sobject_types
+ end
+ end
+
+ describe "#dbdc_client" do
+ it "calls .dbdc_client" do
+ TestController.should_receive(:dbdc_client)
+ TestController.new.send(:dbdc_client)
+ end
+ end
+
+ describe "#sobject_types" do
+ it "calls .sobject_types" do
+ TestController.should_receive(:sobject_types)
+ TestController.new.send(:sobject_types)
+ end
+ end
+
+ describe "automatic materialization" do
+ before(:each) do
+ @client_double = double("client")
+ TestController.should_receive(:sobject_types).and_return(%w(Foo))
+ end
+
+ it "attempts to materialize a referenced constant that is a known sobject type" do
+ TestController.should_receive(:dbdc_client).and_return(@client_double)
+ @client_double.should_receive(:materialize).with("Foo").and_return(double("foo", :create => true))
+ TestController.new.reference_Foo
+ end
+
+ it "does not attempt to materialize a referenced constant that is not a known sobject type" do
+ TestController.should_not_receive(:dbdc_client)
+ expect {
+ TestController.new.reference_Bar
+ }.to raise_error(NameError)
+ end
+ end
+end
@@ -0,0 +1,8 @@
+class ApplicationController
+end
+
+module Rails
+ def self.root
+ File.dirname(__FILE__)
+ end
+end
@@ -0,0 +1,3 @@
+require 'bundler/setup'
+require 'rails_mocks'
+require 'databasedotcom/rails'

0 comments on commit 68fcf1e

Please sign in to comment.