Archive
My first Redcar plugin
As a small thank you to the Redcar team for such a great editor and in the spirit of giving a little bit back I’m documenting the process of creating my first plugin.
Introduction
I really like the idea of the Redcar editor. Its written in Ruby, its possible to write your own plugins what’s not to like? Though I must confess I don’t use it as my day to day editor, I wonder if writing my own plugins will change that.
Let see how easy it is to write a plugin for Redcar.
What’s da big idea
The idea for my first plugin is rather ambitious considering I’ve no idea how to write a plugin. I’d like to replicate some of the functionality of Live Reload – basically refresh a browser web page whenever I save a file in Redcar. This will save me constantly hitting refresh in the browser when working with Backbone.js.
I’m hoping to trap the save event of Redcar and call out to my browser (Chrome) and tell it to refresh the page.
Install Redcar
I first followed the install instructions:
http://github.com/redcar/redcar/wiki/installation
All good no problems there (I’m running on a Mac OSX 10.7.1)
Plugin Guides
I then read the plugin guides I found here:
- http://simplic.it/blog/my-first-redcar-plugin.html
- http://quickwebdesign.net/documents/20110710_redcar_plugins.pdf
Plugin.rb
Setting up a plugin is really easy and well described in the guides listed above. Redcar itself is made up of a bunch of plugin’s so there are no end of great examples to follow in the source code (http://github.com/redcar/redcar)
For trapping the save event I dug into the source code and came across the method project_refresh_task_type which is called on all plugins after a save. To hook into this process my plugin simply defines the method:
def self.project_refresh_task_type
RefreshBrowser
end
This may not be the best place to hook into save, Redcar has the concept of events and that may be a better place to look but for now this is working.
Browser refresh script
RefreshBrowser is a Redcar::Task which I have created to run the browser refresh script.
I managed to find a script to refresh the browser here:
http://brettterpstra.com/watch-for-file-changes-and-refresh-your-browser-automatically/
The script uses a keyword to identify which tab to refresh in the browser so I needed this to be saved and configurable somewhere. Luckily Redcar has a storage mechanism built right in.
Storage
Redcar’s storage mechanism allows you to easily store configuration data into a yaml file.
def storage
@storage ||= Plugin::Storage.new('live_reload_plugin')
@storage.set_default('keyword', '')
@storage
end
Here I am using this mechanism to store the ‘keyword’ used by the RefreshBrowser task. This allows the user to edit the yaml file via the Redcar plugin preferences. It’s also possible to setup a nice edit page but for now this will do.
Source
You can find the source for this plugin here:
http://github.com/justinramel/redcar_plugin_live_reload
Was it easy?
YES! The Redcar team have done a great job and made it incredibly easy to create your own plugins. In a couple of hours I made a useful plugin, useful to me at least. I’ve spent more time fussing over this blog post than developing the plugin.
So what you waiting for? Go give it a try!
Rails new app workflow
These are the steps I take when creating a new Rails app, documented here for when I forget.
Create app
rails new <app_name>
cd <app_name>
git
git init
git add .
git commit –m ‘Initial commit’
Remember: Rails generates a .gitignore file for us.
rvm and gemsets
rvm gemset create <app_name>
echo ‘rvm use 1.9.2@<app_name>’ >> .rvmrc
echo ‘.rvmrc’ >> .gitignore
Creates a .rvmrc file in current directory that way when you cd into this directory rvm automatically switches to the correct version of ruby and the gemset we just created.
Change in and out of the directory to get rvm to kick in:
cd ..
cd <app_name>
Gemfile
Edit ./Gemfile and add the following lines.
gem ‘haml’
group :test do
gem ‘rspec’
gem ‘rspec-rails’
end
Then install and run bundler:
gem install bundler
bundle install
rspec
Generate the rspec files:
rails g rspec:install
vim-ruby-refactoring – Extract Method
This post is part of a series which documents the vim-ruby-refactoring plugin.
IMPORTANT: As well as installing the vim-ruby-refactoring plugin, you must also install the matchit.vim plugin for this refactoring to work.
Extract Method
Extracts a selection into a method and places it above the current method.
The refactoring: http://www.refactoring.com/catalog/extractMethod.html
Example
Before refactoring:
Visually select lines you wish to extract
Hit your <leader-key> then type rem
You will now see a prompt to enter the new method name:
Method name: print_details
After refactoring:
A new method print_details has been added above the print_owing method containing the contents of the selected lines.
rem is the default binding for this refactoring, think Refactor Extract Method.
vim-ruby-refactoring – Rename Instance Variable
This post is part of a series which documents the vim-ruby-refactoring plugin.
IMPORTANT: As well as installing the vim-ruby-refactoring plugin, you must also install the matchit.vim plugin for this refactoring to work.
Rename Instance Variable
Renames the selected instance variable.
Example
Before refactoring:
Visually select the instance variable you wish to rename
Hit your <leader-key> then type rriv
You will now see a prompt to enter the new variable name:
Rename to: new_name
After refactoring:
The instance variable @name has been renamed to @new_name in both locations within the class.
rriv is the default binding for this refactoring, think Refactor Rename Instance Variable.
vim-ruby-refactoring – Rename Local Variable
This post is part of a series which documents the vim-ruby-refactoring plugin.
IMPORTANT: As well as installing the vim-ruby-refactoring plugin, you must also install the matchit.vim plugin for this refactoring to work.
Rename Local Variable
Renames the selected local variable.
Example
Before refactoring:
Visually select the local variable you wish to rename
Hit your <leader-key> then type rrlv
You will now see a prompt to enter the new variable name:
Rename to: is_mac_os
After refactoring:
The local variable mac_os has been renamed to is_mac_os in both locations within the method.
rrlv is the default binding for this refactoring, think Refactor Rename Local Variable.
vim-ruby-refactoring – Extract Local Variable
This post is part of a series which documents the vim-ruby-refactoring plugin.
Extract Local Variable
Extracts a visual selection into a local variable.
The refactoring: http://www.refactoring.com/catalog/introduceExplainingVariable.html
Example
Before refactoring:
Visually select the value you wish to extract
Hit your <leader-key> then type relv
You will now see a prompt to enter the variable name:
Variable name: mac_os
After refactoring:
The value platform.upcase.include?(‘MAC’) has been extracted into a local variable mac_os.
relv is the default binding for this refactoring, think Refactor Extract Local Variable.
vim-ruby-refactoring – Extract to Let
This post is part of a series which documents the vim-ruby-refactoring plugin.
Extract to Let
This is an RSpec specific refactoring which will extract an initialisation line and create a let method for you.
Example
Before refactoring:
Move the cursor on to the line you wish to extract
Hit your <leader-key> then type rel
After refactoring:
The let method is created above the it block, using the initialisation line – account = Account.new.
rel is the default binding for this refactoring, think Refactor Extract Let.
vim-ruby-refactoring – Extract Constant
This post is part of a series which documents the vim-ruby-refactoring plugin.
Extract Constant
Extracts a selection into a constant which is placed at the top of the current module or class.
The refactoring: http://www.refactoring.com/catalog/replaceMagicNumberWithSymbolicConstant.html
Example
Before refactoring:
Visually select the value you wish to extract
Hit your <leader-key> then type rec
You will now see a prompt to enter the constant name:
Constant name: Gravitational_Constant
After refactoring:
The value 9.81 has been extracted into a constant GRAVITATIONAL_CONSTANT which is placed at the top of the module.
rec is the default binding for this refactoring, think Refactor Extract Constant.
vim-ruby-refactoring – Convert Post Conditional
This post is part of a series which documents the vim-ruby-refactoring plugin.
Convert Post Conditional
Converts a post conditional expression to a conditional expression.
Example
Before refactoring:
Move the cursor onto the line which contains the post conditional expression
Hit your <leader-key> then type rcpc
After refactoring:
The post conditional expression is split across 3 lines and converted into a standard if expression.
rcpc is the default binding for this refactoring, think Refactor Convert Post Conditional.
vim-ruby-refactoring – Inline Temp
This post is part of a series which documents the vim-ruby-refactoring plugin.
Inline Temp
Replaces a temporary variable with a direct call to the method or formula.
The refactoring: http://www.refactoring.com/catalog/inlineTemp.html
Example
Before refactoring:
Move the cursor onto the temp variable (in this case base_price)
Hit your <leader-key> then type rit
After refactoring:
The temp variable base_price has been replace with a direct call to the method and we’ve saved a line of code.
rit is the default binding for this refactoring, think Refactor Inline Temp.