Skip to main content

Posts

Rails - to create a blog site Part 2

Creating a resource. The format used is as follow : $ rails generate scaffold <resource name> <list of fields> Example : $ rails generate scaffold test_j author:string title:string tag:string content:text create_datetime:timestamp Create associated table in the database. $ rake db:migrate Then you are done! This new resource can be accessed by http://0.0.0.0:3000/<resource name>/ For the example that I used, it would be http://http://0.0.0.0:3000/test_js/ Click on the New Test_j link will give you the form that we created with the one line command earlier. Note that, the date time is showing current GMT time. I haven't figured out why/how matters. :)

Rails - to create a blog site Part 1

Tutorial that I am following is from this site : http://guides.rubyonrails.org/getting_started.html I choose to sudo as root to avoid the permission issue. $ sudo su Install Ruby $ apt-get install rubygems Install Rails $ gem install rails Create a blog application $ rails new blog Note : You might encounter this error. Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension To solve this, install sqlite before create the blog application. $ apt-get install libsqlite3-dev $ gem install sqlite3 -v '1.3.6' Create a database $ rake db:create You might encounter this error. rake aborted! Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes. Go to the created blog directory. Edit this file : Gemfile. Uncomment this line : gem 'therubyracer', :platform => :ruby Run this (installing nodejs and also reinstall rails) : $ apt-get install nodejs $ bundle install To start t...

Function prototyping in Perl

Today, I studied some old codes from year 2003. It has very weird Perl syntax which I have never seen before. I did some testing using the following code. Of course, I did try and error until I got what the $;$ means. They are actually representing argument that is passing to the subroutine. One $ represent one argument. The ones before ; are required arguments, and the ones after the ; are optional arguments. My colleague is smarter, she found this link online : http://perldoc.perl.org/perlsub.html#Prototypes . There are a lot more to learn in Perl! use strict; use Data::Dumper; $Data::Dumper::Indent = 3; sub sub0 () {   print Dumper(@_); } sub subA ($;$) {    unshift @_, 'new from a';   print Dumper(@_);   goto &sub0; } sub subB () {   subA("a", "b");   subA("c"); } subB(); 1;

Blogger API - create a new post using JavaScript

I am a heavy Blogger user.I write blogs. I never thought of wanted to know Blogger API, but I do thought of get to know Google API. I am glad that my work gives me an opportunity to explore this, for work and for myself. :)  I'd like to thank Brett Morgan and also those responded to Issue 42 in gdata-java​script-cli​ent . Without them, I won't be able to complete this piece of code. And also, not forgetting the original source code that provide me the baseline to start to work at available here . This piece of code will do the Google account authentication and authorization process, it will then grab your blog list, and provide a simple form for you to select a blog, taking your new post entry's title and content, and then post it. I have tested on FF3.2.26, Safari 5, Chrome, and IE8. There'll be some prompts to proceed depends on the browser that you are using. Some special handlings were happened during my weeks of getting this piece of code in place. I can only...

git stash

To save a change without commit. This is to enable checkouts. git stash save "Your comments" To see the list of stashed changes. git stash list To apply the saved change. git stash apply To delete the stashed changes. git stash clear Reference : http://book.git-scm.com/4_stashing.html

Basic commands of git

1. To initialize and setup a git central repository > git --bare init central_repo 2. To push to central repo from clone > git push origin master 3. To check available branches > git branch 4. To create a branch > git checkout -b <branch name> origin/master > git checkout -b <branch name> origin/<existing branch name> 5. To reset changes on branch > git reset --hard <old branch name> http://help.github.com/git-cheat-sheets/

Design Pattern : behavioural

Chain of responsibility The purpose for this is for high cohesion. This means, to increase the reusability. This can be visualized with the specialization in manufacturing world. Each function performs specialized task, and chained together to perform a bigger task. These functions can be reused in other scope since they are doing specialized and simpler task. Command This is to encapsulate requests. Thus, for a program to call any command, it has the similar interface. Interpreter This will translate the context passed by client and perform necessary task on it. Iterator This to enable to parse through the whole data structure. Mediator As a middle man between 2 sub-program to enable low coupling, or for decoupling purpose. Momento This is to capture the state of the class and stored in another object Good for undo to restore the object to its previous state Observer It’s for event driven. As listener, or “don’t call me, I’ll call you”  (After reading the online...

Design Pattern : structural

Composite It looks like tree structure, the end node is called leaf. All the node has uniform interface, thus they can be used/called via uniform operation/data structure Adaptor This is using another class to encapsulate the class to be accessed. Thus the caller will see/use the same interface regardless on how the class to be assessed being implemented. proxy This is acting as a middle man to handle the client requests and sever response. Façade This is using an interface to reduce the complexity. It packages the objects and methods Decorator This is for dynamically to add new responsibility / variation on the methods It will hold the base class pointer, and the real implementation on the child class or actual object. Bridge This is acting like multi-purpose adapter. It serves for multi-platform. Flyweight It handles the pool of resources that are shared by the clients. Private Class Data This is to put the private and readonly data into a class, and being initial...

Design Pattern : creation

Singleton this is basically suitable for single instance through out the program. it can contains the static variables used by the program. the benefits for this versus global variable is for data encapsulation purpose. You can have accessor and modifier functions as a guarding to set or read the variable. Factory The factory is a class to hold and control the creation of the class objects. The object class held by factory is the base class, thus any of the child class object can be instantiated, and transparent in the main program. Abstract Factory Similar to Factory type, but the factory is an abstract class. This is good for multi-platform support, where the right factory will be instantiated based on the platform. Builder This is to hide the complexity of the object by the builder. The program can have multiple builder with same operation but different methods. There is a “directory” to call the right builder to call for operation to produce “product”. Protot...

OO concept and software development practices

I attended a 5-day Design Pattern class last year, and the summary that I published then, was the initial thought of this k-db site. I am revising them and put them here. :) Day 1 : OO concept and software development practices OO concept revised. Normally these are discussed : inheritance composition encapsulation polymorphism Discussed in class : abstraction encapsulation generalization specialization Type of errors compilation syntax contextual linking runtime logical Example to show the best way to code to avoid error : if (x == 0) if (0 == x) <– using this coding style, missing a "=" in the condition statement will alarm error during compilation. Why it is a good practice to use static for variable declaration? for information hiding. so people won’t use extern to access to it in other file, to ensure the value is not overwritten by unknown. error prone during linking. Software quality measurement : reusability : code, function, user exper...