Neethack

Rails Abstraction Showcase

| Comments

Abstraction showcase

Maintaining large application is always a pain for rails developer. Because the MVC scructure encourage developer to write business logics in controller and model. Therefore when application become bigger, usually it will result in Fat controller or All mighty model that have lots of business logics crumble all over the places. Extract those logic to another place is one of the solution, but how do we extract and where to extract is another problem.

Rails Circular Dependency

| Comments

Circular dependency

Recently, I encountered a circular dependency problem that happened in rails, When the parent model is dependent on child model, it returns Runtime Error for Circular dependency. However, there is 2 child model that have circular dependency on parent model, but only one will fail on loading:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# ./app/models/alpha_product
class AlphaProduct < BaseProduct
end

# ./app/models/base_product.rb
class BaseProduct
  PRODUCTS = [AlphaProduct, Product]
  # this works
  # PRODUCTS = [Product]
end

# ./app/models/product.rb
class Product < BaseProduct
end

# test file:
require 'spec_helper'

it 'does something' do
  AlphaProduct.do_things # RuntimeError: Circular dependency detected while autoloading constant AlphaProduct
end

But when we remove the dependency on AlphaProduct, the application works fine. Why is that?

Dig Into the Rails Errors

| Comments

Errors

Rails errors is handling by ActiveModel::Errors, which generate error messages with attribute name and error type. Recently I am working on some feature related to rails error messages, so it is a good time to go over how the rails errors works.

It’s just a hash

ActiveModel::Errors actually is a wrapper for error messages hash, which include the attribute names and error messages for attributes.

Source Code Odyssey: angular.js Injector

| Comments

Angular.js

Angular.js is a fasnating framework that including a lots of interesting features. One of the unique feature in Angular.js is dependency injection, instead of requiring and injecting the dependencies, Angular.js creates a special component to find the dependencies according to parameter names and pass it through the function:

1
2
3
4
5
6
var injector = angular.injector();

injector.invoke(function($http) {
  //get http service from service providers
  $http.ping('http://angularjs.org');
});

command-t.vim Crash on Yosemite

| Comments

The bug

Today I updraged my mac osx to 10.10 yosemite, However, the command-t.vim plugin does not work correctly with new os.

I saw the command-t.vim could not load the C extension message from vim.

Learn jQuery From jqLite

| Comments

JQuery

Jquery is a great library, it makes manipulating DOM element and browser really simple and easy. As a web developer, jQuery is our day to day tool. However, sometimes it just too convenience that we forget how to make web page without it. It is important to go back to see how jQuery handle and wrap the DOM api and provide the simple interface for us.

JQLite

JQLite

Angular.js comes with a simple compatible implementation of jQuery, called JQLite. JQLite is used internally for angular.element if user doesn’t include jQuery, as a replacement for jQuery. It only have 1000 lines of code with lots comments, So it’s a good starting point to understand how jQuery works.

Understand Node Stream (What I Learned When Fixing Aws Sdk Bug)

| Comments

Node.js stream

Node.js provides asynchronous I/O base on event loop. When reading and writing from filesystem or sending http request, Node.js can process other events when waiting for response, which we called it non-blocking I/O. Stream is an extend of this concept, it provides an event base I/O interface to save memory buffers and bandwidth.

What I Talk About When I Talk About Pairing

| Comments

Pair programming, I used to heard a lot about it.
Some said, it is fun.
Some said, it produce good and clean code.
Some said, you can learn from your pair everyday.
Therefore, When I have chance to join a pair programming shop. I took the offer without any hestitation.

So after 7 months of pair programming. I thinks it’s a good time to talk about it.

Understand Chef Lwrp (Heavy Version)

| Comments

Recently I am mainly working on devops things, including system admin and chef. We are refactoring our old chef recipes into a more modulize shape with tests, So I think it’s a good time to share some experience in this refactor!

Resource and Provider in Chef

In chef, we use resource to describe the state of our system. And cookbook is a series of resources that describe the server state.

Announcing Code-Warrior

| Comments

Recently I am preparing the interview with some companies in West Coast.
One of the problem I have during the preparation, which is because I am a vim user, It is uncomfortable to practice algorithm questions on TopCoder, so I think it would be great if I have a npm tool that can download algorithm problems and provide some skeleton test case for practice. Therefore I created Code-Warrior