Hot Chocolate

A blog about the boringly interesting life of Chris Clarke.

Focus on a Text Field on Hover With jQuery

A while back I decided to see if it was possible to use a couple lines of Javascript (with the help of jQuery) to make you automatically focus on a text-field when you hover over it. Turns out, it is:

1
2
3
4
5
6
7
8
9
$('input').hover(
  function() {
    $(this).focus();
  },

  function() {
    $(this).blur();
  }
);

Neat, eh? By the way, you can check it out over at JSFiddle for a live demo!

Links

JSFiddle

How I Made a Plugin DSL

Recently I created a framework called Socks, a web framework written in Ruby.

One of the useful features I wanted to implement for Socks was plugins - plugins you could design easily with a handy DSL.

After a while of tinkering on RubyFiddle, I finally got what I was looking for:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Result

plugin = Socks::Plugin.new do |plugin|
  plugin.name = "myplugin"
  plugin.desc = "A plugin I made."

  plugin.action :hello do
    puts "Hello world!"
  end
end

plugin.run(:hello)

#=> Hello world!

How did I come to make this pretty nice looking plugin DSL? Let’s Take a look at Socks::Plugin, the container of the plugin source:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
module Socks
  class Plugin
    def initilialize(&block) # Makes the Socks::Plugin.new do ... end part
      @name = name # Variable storage for name and desc
      @desc = desc
      instance_eval &block
    end

    def name=(str) # The '=' can actually be used with spaces even in a function!
      @name << str # We just stuff `str` into @name
    end

    def desc=(str) # See above
      @desc << str
    end

    def action(name, &block) # action() takes a name (string or symbol), and a block of code
      yield if block_given? # We tell it to yield, but only if the user passes in a block.
    end

    def run(name) # This runs the action given
      action(name)
    end

    def info # Something not used in the example, it just shows the name and desc.
      puts "Name: #{@name}"
      puts "Description: #{@desc}"
    end

  end
end

Note: You can have multiple actions and call them one by one, since an action has a name you just name them different things each action.

Hope you learned something from this DSL source, because I certainly did!

Links

Socks, RubyFiddle

Testing-http_router

As you may have seen in my last post, the gem http_router provides a amazing way to interact with a nice router in a Rack app. This time, I’m going to show you an example of testing http_router located in a config/router.rb file from the spec directory. For these tests, I will be using RSpec, the simple Ruby testing DSL that is my personal favorite:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# spec/spec_helper.rb

require 'rspec'
require 'rack'

# spec/router_spec.rb

require 'spec_helper'
require File.expand_path('../../config/router.rb', __FILE__) # The router

describe App::Router do
  let(:request) { Rack::MockRequest.new(App::Router) }

  it "contains something at /" do
    request.get('/').should_include "This blog is too hardcore for a hello world!"
  end
end

Links

HTTP Router, RSpec, Rack

Using a Router in Rack-based Apps

Many web-Ruby programmers that love to get into the core of Ruby and Rack may have troubles designing a routing interface for your application/framework.

Luckily, there’s a simple, Sinatra-like way to do this using http_router.

Installation

Just like any gem, you just use:

1
$ gem install http_router

In the config.ru

To hook up the router to your application you’ll need to require an http_router file, or run a new http_router inside the config.ru. For now, I’ll jot down the simple way to do it:

1
2
3
4
5
6
7
# config.ru

require 'http_router'

run HttpRouter.new do
  get('/') { [200, {'Content-type': 'text/plain'}, ["This blog is too hardcore for a hello world!"]] }
end

As you can see, the config.ru is really no different than a regular config.ru, besides the use of running http_router rather than a regular class for your app.

This way to make an app is great, and it almost looks like Sinatra, except for one thing, what if you wan’t your routes to be in another file, like say, config/router.rb. Sounds like Rails, good enough:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# config.ru

require File.expand_path("../config/router.rb", __FILE__)

run App::Router

# config/router.rb

require 'http_router'

class App
  Router = HttpRouter.new do # The magic
    get('/') { [200, {'Content-type': 'text/plain'}, ["This blog is too hardcore for a hello world!"]] }
  end
end

And there we go! Define your routes in an external file, and run the router via a class with a constant. Now you’ve got a working, routed app, ready to be used for the web.

Links

HTTP Router, Rack

Welcome

Welcome to Hot Chocolate, a place where I show you pretty much anything interesting I find, learn, or is on my mind.