I have a couple rake tasks and I want to write specs for them. Yes, I should extract the programming logic from the rake tasks and encapsulate that logic into appropriate class methods. However this doesn't mean that I shouldn't be able to RSpec rake tasks.
I found an older post that got me 90% of the way to what I needed. The problem is that I suspect some sort of static file name caching is going on behind the scenes in rake so using `#rake_require` in an RSpec context isn't performing the functionality I was expecting. I tried moving between `before :all do` and `before` blocks, nesting `describe` blocks and all sorts of other craziness. I had 3 different specs and the first spec would always pass and the other two would fail; I could freely re-arrange the spec order and always the spec defined first would pass. This was maddening.
Turns out I just had to use `load` instead of `#rake_require`.
Here's what my now working spec ended up looking like, for the curious:
#./spec/lib/tasks/rake_tasks_spec.rb
require 'spec_helper'
require 'rake'
describe "rake" do
before do
@rake = Rake::Application.new
Rake.application = @rake
# previously was trying
# Rake.application.rake_require "tasks/get_comic"
load Rails.root + 'lib/tasks/get_comic.rake'
Rake::Task.define_task(:environment)
end
describe "task get_comic" do
it "should have 'environment' as a prerequisite" do
@rake['get_comic'].prerequisites.should include("environment")
end
it "works when expected input is received" do
comic = Comic.new {|c| c.comic_id = 12}
comic.should_receive(:save).and_return(true)
result = Ilk::Parser::Result.new { |r| r.comic_id = 12 }
result.should_receive(:to_model).and_return(comic)
Ilk::Parser.should_receive(:parse).with(Ilk::Constants::COMIC_URL + '12').and_return(result)
Comic.should_receive(:find_by_comic_id).with(12).and_return(nil)
@rake['get_comic'].invoke('12')
end
it "raises an error when there was a problem parsing" do
Ilk::Parser.should_receive(:parse).with(Ilk::Constants::COMIC_URL + '12').and_return(nil)
-> { @rake['get_comic'].invoke('12') }.should raise_error
end
end
end
If you're wondering why your growlnotify isn't working and you see this error message:
#console
... could not find local GrowlApplicationBridgePathway, falling back to NSDNC
Its because Growl isn't running. (System Preferences > Growl, check "Start Growl at login".
Whoops.
Pretty much all my CLI utilities are written in Ruby now. Sometimes a `Rakefile` is all I need, and as a programmer I love breaking things up into separate files. That is if I have a task named "say_hello" in my Rakefile I may actually want it in a`lib/tasks/say_hello.rake` file.
Coercing Rake to load these tasks outside of a Rails environment is actually surprisingly easy - though as a Ruby novice it took me some hunting to get the syntax correct.
#Rakefile
$:.unshift 'lib'
Dir[File.dirname(__FILE__) + '/lib/tasks/*.rake'].each { |file| load file }
`$:` is shorthand for $LOAD_PATH and unshift prepends the `lib` directory to the front of the $LOAD_PATH. Then we enumerate all of the `*.rake` files found in `./lib/tasks` and load them (not require).
One of the things I really like about ActiveRecord is using a block when I new something up...
Person.new do |p|
p.first_name = "John"
p.last_name = "Brown"
p.dob = Date.parse("5/9/1800")
end
Fortunately this behavior is extremely easy to implement outside of ActiveRecord - by stealing from `ActiveRecord::Base`'s source code of course!
class Person
attr_accessor :first_name, :last_name, :dob
def initialize
yield self if block_given?
end
end
Not rocket surgery but now that I have written about it I'm much less likely to forget how to do it.
Warning: not programming related.
As a fledgling Ruby on Rails programmer searching to devour all knowledge of Ruby I of course came across the Ruby on Rails Podcast hosted by Peepcode founder Geoffrey Grosenbach. The very first podcast I listened to from the series was John Medina on Brain Rules for Baby parts one and two. I found the interview very interesting and entertaining, so much that after listening to them my desire to have a child greatly increased. Its like the ultimate hacking challenge: generate a partial clone and then do everything you can to program morals, education, and behavior into this offspring. The hitch is, of course, no keyboard or touchpad, just environmental stimuli, patience, understanding, and sacrifice.
When my wife became pregnant my interest in Dr. Medina's work was rekindled. I was very happy to discover Brain Rules for Baby... along with Brain Rules... both on Audible. I have since made it through most of the way of Brain Rules for Baby... and just finished Brain Rules... tonight - and I fully intend on listening again.
Brain Rules
This book has a subtitle, 12 Principles for Surviving and Thriving at Work, Home, and School which may lead you to think that perhaps its a self help or business theory book. This book is neither of those. Instead its a highly approachable, interesting, and fun romp through what humanity knows about the human brain. Medina filters what we know about our mental organ through what he calls his "grump factor"; that is, "the supporting research for each of [the] points must first be published in a peer-reviewed journal and then successfully be replicated."[1] I found this approach actually makes me more receptive to the material - that is those facts that may seem weird have a lot of science backing them up.
There is also some amount of visionary writing in the book where Medina suggests experiments and ideas as to how we could learn more about the brain and how it learns. This, I think, is important for a few reasons. I believe that public education in our country needs to be reformed from the ground up - no Federal government mandates. Knowing more about how the human brain internalizes information and the most efficient means of doing so could go a long way towards giving our children the competitive edge necessary to keep the US as a leader in science and technology. To achieve this special learning institutions would need to be created that operate similarly to our medical campuses but focus on teaching and education instead. Finally the enlightening fact that by engaging our bodies physically not only greatly reduces the chance of having a heart attack or stroke, but also improves out mental abilities markably. Clearly our schools could benefit from more physical education, not less.
Medina breaks the twelve rules into twelve chapters and you can read a synopsis of each online. Every chapter contains a lot of great, interesting, and memorable information. (In fact, rule #6, long-term memory/remember to repeat, is why I'll be listening to this audiobook again while I am painting. Three times a charm!)
Another factor I find very important to the audiobook presentation is that the author narrates his work. I really appreciate when an author who is capable of doing so does this. Medina has very few moments in the book that I would say are below what you expect from a professional narrator. The narration certainly helps bring the book to life, and hearing the author's voice get excited when driving home a point brings out an oft missing personal touch.
I will be purchasing a few copies of the dead tree book for my friends teaching in the public school system. I hope they find it even more interesting than I have!
And I'd like to note that if there is anyone I would really like to see give a TED talk, its John Medina.
I think its fitting to end this review obeying rule #7: sleep/sleep well, think well.
Brain Rules: 12 Principles for Surviving and Thriving at Work, Home, and School
(paperback)
Brain Rules: 12 Principles for Surviving and Thriving at Work, Home, and School
(Kindle)
Brain Rules for Baby: How to Raise a Smart and Happy Child from Zero to Five
(paperback)
Brain Rules for Baby: How to Raise a Smart and Happy Child from Zero to Five
(Kindle)
1. brainrules.net:
"Read the Introduction"