Source code odyssey: GraphQL Ruby

Recently I am working with GraphQL on a day-to-day basis. The more I work with it, the more I like the GraphQL API compared to the traditional Restful API. And it is an interesting project, this article is going to do a

Read More...

Source code odyssey - Rake

Why?Recently I have a chance to work on mass among of rake tasks in the code base. During the work I found Rake is somewhat confusing but also an interesting framework. I would like to talk about some of the good and bad

Read More...

Ruby Concurrent Programming

This is a talk that I presented in Case Common learning lunch:

Concurrent programming in Ruby is hard.
Before we start implement things with ‘Thread.new’, we should know the difference between Thread and Process, how to write thread safe code and what framework to use.

Read More...

Rails abstraction showcase

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.

Read More...

Rails circular dependency

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
22
23

# ./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?

Read More...

Dig into the rails errors

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.

Read More...

Understand chef lwrp (Heavy version)

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.

Read More...

Test driven server configuration with chef

Recently I am working on Devops things, including server, network,deployment and chef scripts. In the Devops team, we are working hard on refactoringour old chef recipes into more clean and modularize cookbooks.So I thin

Read More...

Code Odyssey : Sinatra

In 2012, I am planning to start contribute and participate more on opensource projects.
The target of this series is to read through the source of open source projects that I am interested with,
and explain the structure and interesting pieces that I found in the source.

Sinatra

Sinatra is a rack-base , lightweight web framework implemented in ruby.
Written and desinged by Blake Mizerany. Famous for it’s dsl syntax and simpliness.

Read More...