Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Find and load our models

Using the Caller class we can track down our models, require the files,
and figure out the class names based on the file names.
  • Loading branch information...
commit 9b2ba6d6cc12fb61b4516e459e18d513db6820a4 1 parent a6ae0ba
@vanstee authored
Showing with 64 additions and 0 deletions.
  1. +41 −0 lib/hovercraft/loader.rb
  2. +23 −0 spec/hovercraft/loader_spec.rb
View
41 lib/hovercraft/loader.rb
@@ -0,0 +1,41 @@
+require 'hovercraft/caller'
+require 'active_support/inflector'
+require 'pry'

Did you mean to commit this?

@vanstee Owner
vanstee added a note

Oh no! Thanks man. I need that git commit hook that tells me if I've committed anything matching /pry/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+
+module Hovercraft
+ class Loader
+ def initialize
+ @caller = Caller.new
+ end
+
+ def with_each_model
+ models.each do |model_class|
+ model_name = model_class.name.underscore
+ plural_model_name = model_name.pluralize
+ yield(model_class, model_name, plural_model_name)
+ end
+ end
+
+ def models
+ @models ||= require_models
+ end
+
+ private
+
+ def require_models
+ Dir.glob(File.join(models_directory, '**/*.rb')).map do |file|
+ require file
+ class_from(file)
+ end
+ end
+
+ def models_directory
+ File.join(@caller.directory, 'models')
+ end
+
+ def class_from(file)
+ file.gsub!(/#{models_directory}|.rb/, '')
+ file.classify.safe_constantize
+ end
+ end
+end
View
23 spec/hovercraft/loader_spec.rb
@@ -0,0 +1,23 @@
+require 'hovercraft/loader'
+
+describe Hovercraft::Loader do
+ let(:model) { stub(name: 'Robot') }
+ let(:models) { Array.new(3) { model } }
+ let(:params) { models.map { |m| [m, 'robot', 'robots'] } }
+
+ describe '#with_each_model' do
+ before { subject.stub(models: models) }
+
+ it 'yields the class, name, and plural name of each model' do
+ expect { |b| subject.with_each_model(&b) }.to yield_successive_args(*params)
+ end
+ end
+
+ describe '#models' do
+ it 'is memoized' do
+ subject.should_receive(:require_models).once.and_return([])
+
+ 2.times { subject.models }

It seems like this spec is coupled to the implementation of the class. Why is this tested?

@vanstee Owner
vanstee added a note

Don't have a good answer for you here so I should probably remove it. Good question.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ end
+ end
+end
@aubreyrhodes

Did you mean to commit this?

@aubreyrhodes

It seems like this spec is coupled to the implementation of the class. Why is this tested?

@vanstee

Oh no! Thanks man. I need that git commit hook that tells me if I've committed anything matching /pry/

@aubreyrhodes
@vanstee

Don't have a good answer for you here so I should probably remove it. Good question.

Please sign in to comment.
Something went wrong with that request. Please try again.