This commit is contained in:
Rico Sta. Cruz 2012-03-16 14:14:31 +08:00
commit 85cc0fe827
41 changed files with 3816 additions and 0 deletions

78
Etc/PHP_Kohana.ctxt Normal file
View File

@ -0,0 +1,78 @@
# Installing
wget "http://kohanaphp.com/download?modules%5Bauth%5D=Auth&languages%5Ben_US%5D=en_US&format=zip" -O k.zip &&\
unzip -q k.zip && rm k.zip &&\
mv Kohana_*/* . && rm -rf Kohana_* &&\
rm -f "Kohana License.html" &&\
# htaccess
cat example.htaccess | sed 's/RewriteBase .*/RewriteBase \//g' > .htaccess && rm example.htaccess &&\
echo Done! Go and edit application/config/config.php and change the site stuff.
# Public HTML
mkdir -p public_html &&\
mv index.html public_html &&\
mv .htaccess public_html &&\
echo Done. Now edit index.html's paths
Git ignore
(echo \*.swo; echo \*.swp; echo .DS_Store; echo Thumbs.db; echo \*~; echo application/logs; echo application/cache ) > .gitignore &&\
# Database
$config['default'] = array
(
'benchmark' => TRUE,
'persistent' => FALSE,
'connection' => array
(
'type' => 'mysql',
'user' => 'leet', // set to db user name
'pass' => 'l33t', // set to db user password
'host' => 'localhost',
'port' => FALSE,
'socket' => FALSE,
'database' => 'leetdb' // set to db name
),
'character_set' => 'utf8',
'table_prefix' => '',
'object' => TRUE,
'cache' => FALSE,
'escape' => TRUE
);
// ORM model
class Post_Model extends ORM {
protected $has_one = array('user'); // has_many, belong_to, has_one, has_and_belongs_to_many
}
// ORM
$post = ORM::factory('post', 1);
$post->name = "Post name";
$post->save();
foreach ($post->categories as $category)
{
echo $category->name;
}
// Find (returns even if no row is found)
$o = ORM::factory('article')->find(1);
$o = ORM::factory('article')->where('title', $title)->find();
if (!$o->loaded) { die('Not found'); }
echo $o->title;
// Find_all
$o = ORM::factory('article')->find_all();
foreach ($o as $article) { echo $article->title; }
// ->$saved
// ->$changed[]
// ->$object_name (Blog_Post_Model => "blog_post")
// ->$primary_key ('id')
// ->$primary_val ('username') - more userfriendly identifier
// ->$table_name
// ->$ignored_columns = array('whatever')
// ->$table_columns = array('id', 'username')
// ->$sorting = array('last_login' => 'desc') -- default sorting
//

104
Etc/Rails_2.ctxt Normal file
View File

@ -0,0 +1,104 @@
# Debug
logger.debug "xx"
# Controller stuff
class MyController < ApplicationController::Base
controller.response.body
# Filters
before_filter :require_login # Looks for require_login method
before_filter MyFilter # Looks for MyFilter class
before_filter { |ct| head(400) if ct.params["stop_action"] }
around_filter :catch_exceptions
after_filter :xx
layout "admin_area" # Looks for the view file
layout "admin_area", :except => [ :rss, :whatever ]
layout :foo # Looks for private function foo
private
def whatever ...
class MyFilter
def self.filter(controller, &block)
# Model
belongs_to :user
validates_presence_of :user
default_scope :order => 'id DESC'
named_scope :today, :conditions = "created_at x"
named_scope :today, lambda {{ :conditions = [ "created_at between ? and ?", 1.hour.ago.utc, 300.seconds.ago.utc ] }}
# Then you can call feed.today
# Controller methods
render :action => 'help', :layout => 'help'
render :text => 'so and so'
render :status => :created, :location => post_url(post) # With HTTP headers
redirect_to :action => 'index'
render :partial => 'product', :collection => @products, :as => :item, :spacer_template => "product_ruler"
return head(:method_not_allowed)
head :created, :location => '...'
url_for :controller => 'posts', :action => 'recent'
location = request.env["SERVER_ADDR"]
# For views
auto_discovery_link_tag(:rss, {:action => "feed"}, {:title => "RSS Feed"})
javascript_include_tag "foo"
stylesheet_link_tag
image_tag
# Ruby stuff!
# Defining a class method (not a typo)
Fixnum.instance_eval { def ten; 10; end }
Fixnum.ten # => 10
# Defining an instance method
Fixnum.class_eval { def number; self; end }
7.number #=> 7
# Multiple arguments, send()
class Klass
def hello(*args); "Hello " + args.join(' '); end
end
Klass.new.send :hello, "gentle", "readers"
def can(*args)
yield if can?(*args)
end
# can(x) {...} => if can?(x) {...}
# Struct
class Foo < Struct.new(:name, :email)
end
j = Foo.new("Jason", "jason@bateman.com")
j.name = "Hi"
print j.name
# Struct
class Foo < Struct.new(:name, :email)
end
j = Foo.new("Jason", "jason@bateman.com")
j.name = "Hi"
print j.name
# Method missing
def method_missing(method_name, *arguments)
if method_name.to_s[-1,1] == "?"
self == method_name.to_s[0..-2]
# Rails logger
Rails.logger.info("...")
# To string
:hello_there.to_s

4
Gemfile Normal file
View File

@ -0,0 +1,4 @@
source :rubygems
gem "proton", "~> 0.3.4"
gem "rack-cache", "~> 1.0.0"

41
Gemfile.lock Normal file
View File

@ -0,0 +1,41 @@
GEM
remote: http://rubygems.org/
specs:
RedCloth (4.2.8)
chunky_png (1.2.1)
compass (0.11.5)
chunky_png (~> 1.2)
fssm (>= 0.2.7)
sass (~> 3.1)
cuba (2.0.1)
rack
tilt
fssm (0.2.7)
haml (3.1.3)
hashie (1.0.0)
maruku (0.6.0)
syntax (>= 1.0.0)
proton (0.3.4)
RedCloth (~> 4.2.3)
compass (~> 0.11.1)
cuba (~> 2.0.0)
haml (~> 3.1.1)
hashie (~> 1.0.0)
maruku (~> 0.6.0)
sass (~> 3.1.1)
shake (~> 0.1)
tilt (~> 1.3.2)
rack (1.3.2)
rack-cache (1.0.2)
rack (>= 0.4)
sass (3.1.7)
shake (0.1.3)
syntax (1.0.0)
tilt (1.3.3)
PLATFORMS
ruby
DEPENDENCIES
proton (~> 0.3.4)
rack-cache (~> 1.0.0)

12
Protonfile Normal file
View File

@ -0,0 +1,12 @@
# This is a Proton site.
# Install Proton (`gem install proton`) and type `proton` for help.
requirement: 0.2
# The folder where the site's source files are kept.
site_path: .
output_path: _output
# Other paths:
layouts_path: _layouts
extensions_path: _extensions
partials_path: .

17
_layouts/default.html Normal file
View File

@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title></title>
<link href="/style.css" rel="stylesheet" />
</head>
<body>
<h1><%= page.meta.title || File.basename(page.file, '.*').capitalize %></h1>
<%= yield %>
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/1.6.4/jquery.min.js'></script>
<script src="http://cachedcommons.org/cache/prettify/1.0.0/javascripts/prettify-min.js"></script>
<script>$("pre").addClass("prettyprint");</script>
<script>prettyPrint();</script>
</body>
</html>

44
_prettify.scss Normal file
View File

@ -0,0 +1,44 @@
/* Pretty printing styles. Used with prettify.js. */
.str { color: #080; }
.kwd { color: #008; }
// .com { color: #607077; background: rgba(black, 0.05); padding: 1px 3px; @include border-radius(2px); }
.com { color: #7bd; text-shadow: 1px 1px 0 rgba(white, 0.3); }
.typ { color: #606; }
.lit { color: #066; }
.pun { color: #660; }
.pln { color: #000; }
.tag { color: #008; }
.atn { color: #606; }
.atv { color: #080; }
.dec { color: #606; }
/* Specify class=linenums on a pre to get line numbering */
ol.linenums { margin-top: 0; margin-bottom: 0 } /* IE indents via margin-left */
li.L0,
li.L1,
li.L2,
li.L3,
li.L5,
li.L6,
li.L7,
li.L8 { list-style-type: none }
/* Alternate shading for lines */
li.L1,
li.L3,
li.L5,
li.L7,
li.L9 { background: #eee }
@media print {
.str { color: #060; }
.kwd { color: #006; font-weight: bold; }
.com { color: #600; font-style: italic; }
.typ { color: #404; font-weight: bold; }
.lit { color: #044; }
.pun { color: #440; }
.pln { color: #000; }
.tag { color: #006; font-weight: bold; }
.atn { color: #404; }
.atv { color: #060; }
}

181
bash.md Normal file
View File

@ -0,0 +1,181 @@
title: Bash
---
### String substitutions by patterns
STR=/path/to/foo.c
echo ${STR%.c} #=> "/path/to/foo"
echo ${STR%.c}.o #=> "/path/to/foo.o"
echo ${STR##*.} #=> "c" (extension)
BASE=${SRC##*/} #=> "foo.c" (basepath)
DIR=${SRC%$BASE} #=> "/path/to"
### Substitutions by regex
echo ${STR/hi/hello} # Replace first match
echo ${STR//hi/hello} # Replace all matches
echo ${STR/#hi/hello} # ^hi
echo ${STR/%hi/hello} # hi$
echo "${STR:0:3}" # .substr(0, 3) -- position, length
echo "${STR:-3:3}" # Negative position = from the right
echo ${#line} # Length of $line
[ -z "$CC" ] && CC=gcc # CC ||= "gcc" assignment
${CC:=gcc} # $CC || "gcc"
### Loops
for i in /etc/rc.*; do
echo $i
end
Functions
---------
### Defining functions
myfunc() { ... }
fuction myfunc { ... }
fuction myfunc() { ... }
### Returning strings
myfunc() {
local myresult='some value'
echo $myresult
}
result=$(myfunc)
### Errors
myfunc() { return 1; }
### Arguments
$# # Number of arguments
$* # All args
$1 # First argument
Ifs - files
-----------
# File conditions
if [ -a FILE ]; then # -e exists -d directory -f file
fi # -r readable -w writeable -x executable
# -h symlink -s size > 0
# File comparisons
if [ FILE1 -nt FILE2 ] # -nt 1 more recent than 2
# -ot 2 more recent than 1
# -ef same files
Ifs
---
# String
if [ -z STRING ] # empty?
if [ -n STRING ] # not empty?
# Numeric
if [ $? -eq 0 ] # -eq -ne -lt -le -gt -ge
# $? is exit status by the way
# Etc
if [ -o noclobber ] # if OPTIONNAME is enabled
if [ ! EXPR ] # not
if [ ONE -a TWO ] # and
if [ ONE -o TWO ] # or
# Regex
if [[ "A" =~ "." ]]
### Numeric comparisons
if $(( $a < $b ))
### Unset variables
Assume `$FOO` is not set. Doing *this* will result in *that*:
${FOO:-word} # Returns word
${FOO:+word} # Returns empty, or word if set
${FOO:=word} # Sets parameter to word, returns word
${FOO:?message} # Echoes message and exits
${FOO=word} # : is optional in all of the above
Numeric calculations
--------------------
$((RANDOM%=200)) # Random number 0..200
$((a + 200)) # $ is optional
Arrays
------
Fruits[0]="Apple"
Fruits[1]="Banana"
Fruits[2]="Orange"
# Declaring using declare -a
declare -a Fruits=('Apple' 'Banana' 'Orange')
echo ${Fruits[0]} # Element #0
echo ${Fruits[@]} # All elements, space-separated
echo ${#Fruits[@]} # Number of elements
echo ${#Fruits} # String length of the 1st element
echo ${#Fruits[3]} # String length of the Nth element
echo ${Fruits[@]:3:2} # Range (from position 3, length 2)
Fruits=("${Fruits[@]}" "Watermelon") # Push
Fruits=( ${Fruits[@]/Ap*/} ) # Remove by regex match
unset Fruits[2] # Remove one item
Fruits=("${Fruits[@]}") # Duplicate
Fruits=("${Fruits[@]}" "${Veggies[@]}") # Concatenate
lines=(`cat "logfile"`) # Read from file
Misc crap
---------
command -V cd #=> "cd is a function/alias/whatever"
### Options
set -o noclobber # Avoid overlay files (echo "hi" > foo)
set -o errexit # Used to exit upon error, avoiding cascading errors
set -o pipefail # Unveils hidden failures
set -o nounset # Exposes unset variables
### Glob options
set -o nullglob # Non-matching globs are removed ('*.foo' => '')
set -o failglob # Non-matching globs throw errors
set -o nocaseglob # Case insensitive globs
set -o dotglob # Wildcards match dotfiles ("*.sh" => ".foo.sh")
set -o globstar # Allow ** for recursive matches ('lib/**/*.rb' => 'lib/a/b/c.rb')
set GLOBIGNORE as a colon-separated list of patterns to be removed from glob
matches.
### Trap errors
trap 'echo Error at about $LINENO' ERR
or
traperr() {
echo "ERROR: ${BASH_SOURCE[1]} at about ${BASH_LINENO[0]}"
}
set -o errtrace
trap traperr ERR
References
----------
* http://wiki.bash-hackers.org/

30
brew.md Normal file
View File

@ -0,0 +1,30 @@
title: Brew
---
Nice Homebrew packages:
* `tig` - Git "GUI" for the console
* `mysql`
* `postgresql`
* `fmdiff` - Adaptor to use Apple's FileMerge as `diff` (`git config --global merge-tool fmdiff`)
* `cmus` - Curses-based music player
* `cclive` - Video downloader
Not from brew:
* `DiffMerge` - nice free merge tool for OSX
Tmux
----
Install a more-recent version that supports tmux -C
brew install https://github.com/adamv/homebrew-alt/raw/master/other/tmux-iterm2.rb
Install the wrapper for stuff to enable OSX clipboard to work
brew install reattach-to-user-namespace --wrap-pbcopy-and-pbpaste
Make sure that your VIM alias uses it
alias vim="reattach-to-user-namespace /Application/MacVim/Contents/MacOS/Vim"

95
capybara.md Normal file
View File

@ -0,0 +1,95 @@
Navigating
----------
visit articles_path
Clicking links and buttons
--------------------------
click 'Link Text'
click_button
click_link
Interacting with forms
----------------------
attach_file
fill_in 'First Name', :with => 'John'
check
uncheck
choose
select
unselect
Querying
--------
Takes a CSS selector (or XPath if you're into that).
Translates nicely into RSpec matchers:
page.should have_no_button("Save")
Use should have_no_* versions with RSpec matchers b/c
should_not doesn't wait for a timeout from the driver
page.has_content?
page.has_css?
page.has_no_content?
page.has_no_css?
page.has_no_xpath?
page.has_xpath?
page.has_link?
page.has_no_link?
page.has_button?("Update")
page.has_no_button?
page.has_field?
page.has_no_field?
page.has_checked_field?
page.has_unchecked_field?
page.has_no_table?
page.has_table?
page.has_select?
page.has_no_select?
Finding
-------
find
find_button
find_by_id
find_field
find_link
locate
Scoping
-------
within
within_fieldset
within_table
within_frame
scope_to
Scripting
---------
execute_script
evaluate_script
Debugging
---------
save_and_open_page
Miscellaneous
-------------
all
body
current_url
drag
field_labeled
source
wait_until
current_path

2
cinema4d.md Normal file
View File

@ -0,0 +1,2 @@
E R T : Move/rotate/scale
P : snapping

26
config.ru Normal file
View File

@ -0,0 +1,26 @@
# This file exists to make this project Rack-compatible.
# You may delete it if you're not concerned about this.
require 'rubygems' unless defined?(::Gem)
# Use Bundler if possible.
begin
require 'bundler'
Bundler.setup
rescue LoadError
gem 'proton', '0.3.2'
end
# Optional: use the 'rack-cache' gem for cacheing.
if ENV['RACK_ENV'] == 'production'
begin
require 'rack/cache'
use Rack::Cache
rescue LoadError
end
end
# Load Proton.
require 'proton/server'
Proton::Project.new File.dirname(__FILE__)
run Proton::Server

126
devise.md Normal file
View File

@ -0,0 +1,126 @@
title: Devise
---
[Devise](https://github.com/plataformatec/devise) is a flexible authentication
gem.
Installation
------------
Rails 3: Add the following to your Gemfile
gem "devise"
gem "hpricot"
gem "ruby_parser"
Install devise in your project
$ rails generate devise:install
Generate devise for your model
$ rails generate devise MODEL
$ rake db:migrate
(Optional) Generate devise views
$ rails generate devise:views
Helpers
-------
user_signed_in?
current_user
user_session
destroy_user_session_path (Logout)
new_user_session_path (Login)
edit_user_registration_path (Edit registration)
new_user_registration_path (Register new user)
Controller stuff
----------------
before_filter :authenticate_user!
Model
-----
### Model options
class User < ActiveRecord::Base
devise :database_authenticatable,
:registerable,
:confirmable,
:recoverable,
:rememberable,
:trackable,
:validatable
end
### Migration helpers
create_table :users do |t|
t.database_authenticatable
t.confirmable
t.recoverable
t.rememberable
t.trackable
t.timestamps
end
Routing
-------
### Authenticated and unauthenticated routes
unauthenticated do
root :to => 'home#index'
end
authenticated do
root :to => 'dashboard#index'
end
### As
as :user do
get 'sign_in', :to => 'devise/sessions#new'
end
### Devise_for magic
devise_for :users
# Session routes for Authenticatable (default)
new_user_session GET /users/sign_in {:controller=>"devise/sessions", :action=>"new"}
user_session POST /users/sign_in {:controller=>"devise/sessions", :action=>"create"}
destroy_user_session GET /users/sign_out {:controller=>"devise/sessions", :action=>"destroy"}
# Password routes for Recoverable, if User model has :recoverable configured
new_user_password GET /users/password/new(.:format) {:controller=>"devise/passwords", :action=>"new"}
edit_user_password GET /users/password/edit(.:format) {:controller=>"devise/passwords", :action=>"edit"}
user_password PUT /users/password(.:format) {:controller=>"devise/passwords", :action=>"update"}
POST /users/password(.:format) {:controller=>"devise/passwords", :action=>"create"}
# Confirmation routes for Confirmable, if User model has :confirmable configured
new_user_confirmation GET /users/confirmation/new(.:format) {:controller=>"devise/confirmations", :action=>"new"}
user_confirmation GET /users/confirmation(.:format) {:controller=>"devise/confirmations", :action=>"show"}
POST /users/confirmation(.:format) {:controller=>"devise/confirmations", :action=>"create"}
### Customizing devise_for
devise_for :users,
:path => "usuarios",
:path_names => {
:sign_in => 'login',
:sign_out => 'logout',
:password => 'secret',
:confirmation => 'verification',
:unlock => 'unblock',
:registration => 'register',
:sign_up => 'cmon_let_me_in' }
Test helpers
------------
sign_in @user
sign_out @user

1293
figlets.txt Normal file

File diff suppressed because it is too large Load Diff

22
fitness.md Normal file
View File

@ -0,0 +1,22 @@
title: Fitness
---
### Target heart rate
max heart rate = (220 - age)
"The target heart rate method is a simple formula: take 220 and minus your age.
Then take that number and multiply it by .75 - .85, which will give you your
percentages of 75% -- 85% of your Max. HR."
http://www.bodybuilding.com/fun/mike1.htm
### Warmup sets
* 5 x Bar
* 5 x 60%
* 3 x 70%
* 2 x 80%
* 5 x 100% (work set)
http://corw.in/warmup/

29
heroku.md Normal file
View File

@ -0,0 +1,29 @@
## Create an app
heroku create sushi
## Custom domains
heroku addon:add custom_domains
heroku domains:add example.com
heroku domains:add www.example.com
## DNS records
# Root domains
mydomain.com. (A)
=> 75.101.163.44
=> 75.101.145.87
=> 174.129.212.2
# Subdomains
.mydomain.com. (CNAME)
=> proxy.heroku.com
## Wildcard domains
heroku addons:add wildcard_domains
*.yourdomain.com => heroku.com

27
html-css.md Normal file
View File

@ -0,0 +1,27 @@
### CSS - Selectors
.class {
}
### CSS - Font styling
font-family: Arial;
font-size: 12pt;
line-height: 150%;
color: #aa3322;
### CSS - Font styling
// Bold
font-weight: bold;
font-weight: normal;
// Italic
font-style: italic;
font-style: normal;
// Underline
text-decoration: underline;
text-decoration: none;

18
html.md Normal file
View File

@ -0,0 +1,18 @@
title: HTML
---
### H5BP HTML tag
<!--[if lt IE 7 ]> <html class="ie6"> <![endif]-->
<!--[if IE 7 ]> <html class="ie7"> <![endif]-->
<!--[if IE 8 ]> <html class="ie8"> <![endif]-->
<!--[if IE 9 ]> <html class="ie9"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <html class=""> <!--<![endif]-->
### iPhone viewport
<meta name="viewport" content="width=320; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"/>
### Google jQuery
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>

10
index.haml Normal file
View File

@ -0,0 +1,10 @@
title: Cheat sheets
---
%ul.pages
- Dir['./*.*'].each do |p|
- name = File.basename(p, '.*')
- ext = File.extname(p)
- next if %w[.sass .scss].include?(ext) || %w[config Gemfile index].include?(name) || name[0] == '_'
- url = name + ".html"
%li
%a{href: url}= name

11
ios.md Normal file
View File

@ -0,0 +1,11 @@
Path to Installous downloads:
/private/var/mobile/Documents/Installous/Downloads
Multiple Exchange accounts:
scp root@iphone.local:/private/var/mobile/Library/Preferences/com.apple.accountsettings.plist .
Ringtone conversion using ffmpeg:
ffmpeg -i foo.mp3 -ac 1 -ab 128000 -f mp4 -acodec libfaac -y target.m4r

51
jquery.md Normal file
View File

@ -0,0 +1,51 @@
title: jQuery
---
### Extending selectors
// $(":inline")
$.expr[':'].inline = function(a) {
return $(a).css('display') === 'inline';
};
### Extend CSS properties
$.cssHooks.someCSSProp = {
get: function(elem, computed, extra) {
},
set: function(elem, value) {
}
};
// Disable "px"
$.cssNumber["someCSSProp"] = true;
### fn.animate() hooks
$.fn.step.someWhatever = function(fx) {
// ...
}
### Mobile events
For support for `tap`, `swipe`, `swipeLeft`, et al, use
[jquery.mobile.event.js][m]. Be sure to set `$.support.touch` first.
To get `$.support.touch` (and family), use this from
[jquery.mobile.support.js][s]:
$.extend($.support, {
orientation: "orientation" in window && "onorientationchange" in window,
touch: "ontouchend" in document,
cssTransitions: "WebKitTransitionEvent" in window,
pushState: "pushState" in history && "replaceState" in history,
mediaquery: $.mobile.media( "only all" ),
cssPseudoElement: !!propExists( "content" ),
touchOverflow: !!propExists( "overflowScrolling" ),
boxShadow: !!propExists( "boxShadow" ) && !bb,
scrollTop: ( "pageXOffset" in window || "scrollTop" in document.documentElement || "scrollTop" in fakeBody[ 0 ] ) && !webos && !operamini,
dynamicBaseTag: baseTagTest()
});
[m]:https://github.com/jquery/jquery-mobile/blob/master/js/jquery.mobile.event.js
[s]:https://github.com/jquery/jquery-mobile/blob/master/js/jquery.mobile.support.js

4
linux.md Normal file
View File

@ -0,0 +1,4 @@
### Mounting a RAM drive
$ mount -t tmpfs -o size=5G,nr_inodes=5k,mode=700 tmpfs /tmp

26
makefile.md Normal file
View File

@ -0,0 +1,26 @@
### Safe assignment
prefix ?= /usr/local
### Cool stuff
gitdir ?= $(shell git --exec-path)
gitver ?= $(word 3,$(shell git --version))
### Substitutions
$(SOURCE:.cpp=.o)
$(patsubst %.cpp, %.c, $(SOURCES))
### Building files
%.o: %.c
ffmpeg -i $< > $@ # Input and output
foo $^
### Default task
default:
@echo "hello."
@false

14
markdown.md Normal file
View File

@ -0,0 +1,14 @@
title: Markdown
----
## Markdown
# h1
### h3
[link](http://google.com)
[link][google]
[google]: http://google.com
![Image alt text](/path/to/img.jpg)

23
osx.md Normal file
View File

@ -0,0 +1,23 @@
Locations of startup items
--------------------------
/System/Library/LaunchAgents/
/System/Library/LaunchDaemons/
/Library/LaunchAgents/
/Library/LaunchDaemons/
Hide desktop icons
------------------
defaults write com.apple.finder CreateDesktop -bool false
killall Finder
Auto-hide other windows on dock switch
--------------------------------------
defaults write com.apple.dock single-app -bool TRUE
killall Dock
defaults delete com.apple.dock single-app
killall Dock

11
passenger.md Normal file
View File

@ -0,0 +1,11 @@
title: Phusion Passenger
---
server {
listen 80;
server_name www.yourhost.com;
root /somewhere/public; # <--- be sure to point to 'public'!
passenger_enabled on;
autoindex on; # Show directory listings
}

293
rails-models.md Normal file
View File

@ -0,0 +1,293 @@
title: Rails Models
----
### Generating models
$ rails g model User
### Associations
belongs_to
has_one
has_many
has_many :through
has_one :through
has_and_belongs_to_many
belongs_to :author,
class_name: 'User',
dependent: :destroy // delete this
### Has many
belongs_to :parent, :foreign_key => 'parent_id' class_name: 'Folder'
has_many :folders, :foreign_key => 'parent_id', class_name: 'Folder'
has_many :comments, :order => "posted_on"
has_many :comments, :include => :author
has_many :people, :class_name => "Person"
has_many :people, :conditions => "deleted = 0"
has_many :tracks, :order => "position"
has_many :comments, :dependent => :nullify
has_many :comments, :dependent => :destroy
has_many :tags, :as => :taggable
has_many :reports, :readonly => true
has_many :subscribers, :through => :subscriptions, class_name: "User", :source => :user
has_many :subscribers, :finder_sql =>
'SELECT DISTINCT people.* ' +
'FROM people p, post_subscriptions ps ' +
'WHERE ps.post_id = #{id} AND ps.person_id = p.id ' +
'ORDER BY p.first_name'
### Many-to-many
If you have a join model:
class Programmer < ActiveRecord::Base
has_many :assignments
has_many :projects, :through => :assignments
end
class Project < ActiveRecord::Base
has_many :assignments
has_many :programmers, :through => :assignments
end
class Assignment
belongs_to :project
belongs_to :programmer
end
Or HABTM:
has_and_belongs_to_many :projects
has_and_belongs_to_many :projects, :include => [ :milestones, :manager ]
has_and_belongs_to_many :nations, :class_name => "Country"
has_and_belongs_to_many :categories, :join_table => "prods_cats"
has_and_belongs_to_many :categories, :readonly => true
has_and_belongs_to_many :active_projects, :join_table => 'developers_projects', :delete_sql =>
"DELETE FROM developers_projects WHERE active=1 AND developer_id = #{id} AND project_id = #{record.id}"
### Polymorphic associations
class Post
has_many :attachments, :as => :parent
end
class Image
belongs_to :parent, :polymorphic => true
end
And in migrations:
create_table :images do
t.references :post, :polymorphic => true
end
Migrations
----------
### Run migrations
$ rake db:migrate
### Migrations
create_table :users do |t|
t.string :name
t.text :description
t.primary_key :id
t.string
t.text
t.integer
t.float
t.decimal
t.datetime
t.timestamp
t.time
t.date
t.binary
t.boolean
end
options:
:null (boolean)
:limit (integer)
:default
:precision (integer)
:scale (integer)
### Tasks
create_table
change_table
drop_table
add_column
change_column
rename_column
remove_column
add_index
remove_index
### Associations
t.references :category # kinda same as t.integer :category_id
# Can have different types
t.references :category, polymorphic: true
### Add/remove columns
$ rails generate migration RemovePartNumberFromProducts part_number:string
class RemovePartNumberFromProducts < ActiveRecord::Migration
def up
remove_column :products, :part_number
end
def down
add_column :products, :part_number, :string
end
end
Validation
----------
class Person < ActiveRecord::Base
# Checkboxes
  validates :terms_of_service, :acceptance => true
# Validate associated records
  has_many :books
  validates_associated :books
# Confirmation (like passwords)
validates :email, :confirmation => true
# Format
validates :legacy_code, :format => {
:with => /\A[a-zA-Z]+\z/,
:message => "Only letters allowed"
}
# Length
validates :name, :length => { :minimum => 2 }
validates :bio, :length => { :maximum => 500 }
validates :password, :length => { :in => 6..20 }
validates :number, :length => { :is => 6 }
# Length (full enchalada)
validates :content, :length => {
:minimum => 300,
:maximum => 400,
:tokenizer => lambda { |str| str.scan(/\w+/) },
:too_short => "must have at least %{count} words",
:too_long => "must have at most %{count} words"
}
end
# Numeric
validates :points, :numericality => true
validates :games_played, :numericality => { :only_integer => true }
# Non empty
  validates :name, :presence => true
# Multiple
validate :login, :email, :presence => true
end
### Custom validations
class Person < ActiveRecord::Base
validate :foo_cant_be_nil
def foo_cant_be_nil
errors.add(:foo, 'cant be nil') if foo.nil?
end
end
API
---
items = Model.find_by_email(email)
items = Model.where(first_name: "Harvey")
item = Model.find(id)
item.serialize_hash
item.new_record?
item.create # Same an #new then #save
item.create! # Same as above, but raises an Exception
item.save
item.save! # Same as above, but raises an Exception
item.update
item.update_attributes
item.update_attributes!
item.valid?
item.invalid?
http://guides.rubyonrails.org/active_record_validations_callbacks.html
### Mass updates
# Updates person id 15
Person.update 15, name: "John", age: 24
Person.update [1,2], [{name: "John"}, {name: "foo"}]
### Joining
Student.joins(:schools).where(:schools => { :type => 'public' })
Student.joins(:schools).where('schools.type' => 'public' )
### Serialize
class User < ActiveRecord::Base
serialize :preferences
end
user = User.create(:preferences => { "background" => "black", "display" => large })
You can also specify a class option as the second parameter thatll raise an
exception if a serialized object is retrieved as a descendant of a class not in
the hierarchy.
class User < ActiveRecord::Base
serialize :preferences, Hash
end
user = User.create(:preferences => %w( one two three ))
User.find(user.id).preferences # raises SerializationTypeMismatch
Overriding accessors
--------------------
class Song < ActiveRecord::Base
# Uses an integer of seconds to hold the length of the song
def length=(minutes)
write_attribute(:length, minutes.to_i * 60)
end
def length
read_attribute(:length) / 60
end
end
* http://api.rubyonrails.org/classes/ActiveRecord/Base.html
Callbacks
---------
after_create
after_initialize
after_validation
after_save
after_commit

133
rails-plugins.md Normal file
View File

@ -0,0 +1,133 @@
Generate a plugin
-----------------
Generate a Rails Engine plugin:
rails plugin new myplugin --skip-bundle --full
Initializers
------------
* [Rails::Railtie](http://edgeapi.rubyonrails.org/classes/Rails/Railtie.html)
* [EngineYard blog
post](http://www.engineyard.com/blog/2010/extending-rails-3-with-railties/)
Subclass Railtie and provide an `initializer` method.
module NewPlugin
class Railtie < Rails::Railtie
initializer "newplugin.initialize" do |app|
# subscribe to all rails notifications: controllers, AR, etc.
ActiveSupport::Notifications.subscribe do |*args|
event = ActiveSupport::Notifications::Event.new(*args)
puts "Got notification: #{event.inspect}"
end
end
end
end
Custom routes
-------------
* [ActionDispatch::Routing::Mapper](http://api.rubyonrails.org/classes/ActionDispatch/Routing/Mapper.html)
To create custom `routes.rb` keywords:
# # routes.rb:
# myplugin_for x
#
class ActionDispatch::Routing
class Mapper
def myplugin_for(*x)
end
end
end
Example with a block:
# authenticated do
# resources :users
# end
#
def authenticated
constraint = lambda { |request| request... }
constraints(constraint) { yield }
end
Custom generators
-----------------
* [Guide: generators](http://guides.rubyonrails.org/generators.html)
* [ActiveRecord::Generators::Base](http://api.rubyonrails.org/classes/ActiveRecord/Generators/Base.html)
### Basic
# rails g initializer
# lib/generators/initializer_generator.rb
class InitializerGenerator < Rails::Generators::Base
def create_initializer_file
create_file "config/initializers/initializer.rb", "# Add initialization content here"
end
end
* Extend `Rails::Generators::Base`.
* Each public method in the generator is executed when a generator is invoked.
### Generating a generator
$ rails generate generator initializer
### NamedBase
Use `NamedBase` instead if you want to take an argument. It will be available as
`file_name`.
class InitializerGenerator < Rails::Generators::Base
def lol
puts file_name
end
end
### More
class InitializerGenerator < Rails::Generators::NamedBase
#
  source_root File.expand_path("../templates", __FILE__)
desc "Description goes here."
end
### Generators lookup
When invoking `rails g XXX`:
* [rails/]generators/XXX/XXX_generator.rb
* [rails/]generators/XXX_generator.rb
When invoking `rails g XXX:YYY`:
* [rails/]generators/XXX/YYY_generator.rb
ActiveModel 'acts as'
---------------------
# yaffle/lib/yaffle/acts_as_yaffle.rb
module Yaffle
module ActsAsYaffle
extend ActiveSupport::Concern
included do
end
module ClassMethods
def acts_as_yaffle(options = {})
# your code will go here
end
end
end
end
ActiveRecord::Base.send :include, Yaffle::ActsAsYaffle

172
rails-routes.md Normal file
View File

@ -0,0 +1,172 @@
mapping
-------------
[Guides/Routing](http://guides.rubyonrails.org/routing.html)
[ActionDispatch::Routing::Mapper](Rhttp://api.rubyonrails.org/classes/ActionDispatch/Routing/Mapper.html)
(See included modules)
### Multiple resources
resources :books
# PhotosController:
# index => GET /photos
# new => GET /photos/new
# create => POST /photos/new
# show => GET /photos/:id
# edit => GET /photos/:id/edit
# update => PUT /photos/:id
# delete => DELETE /photos/:id
#
# Helpers:
# new_book_path
# book_path(id)
# edit_book_path(id)
#
resources :photos do
member { get 'preview' } # /photo/1/preview
get 'preview', on: :member # (..same as the first)
collection { get 'search' } # /photos/search
end
### Single resource
resource :coder
# CodersController:
# new => GET /coder/new
# create => POST /coder/new
# show => GET /coder
# edit => GET /coder/edit
# update => PUT /coder
# delete => DELETE /coder
### Matching
match 'photo/:id' => 'photos#show' # /photo/what-is-it
match 'photo/:id', id: /[0-9]+/ # /photo/0192
match 'photo/:id' => 'photos#show', constraints: { id: /[0-9]+/ }
match 'photo/:id', via: :get
match 'photo/:id', via: [:get, :post]
match 'photo/*path' => 'photos#unknown' # /photo/what/ever
# params[:format] == 'jpg'
match 'photos/:id' => 'photos#show', :defaults => { :format => 'jpg' }
### Redirection
match '/stories' => redirect('/posts')
match '/stories/:name' => redirect('/posts/%{name}')
### Named
# logout_path
match 'exit' => 'sessions#destroy', as: :logout
### Constraints
match '/', constraints: { subdomain: 'admin' }
# admin.site.com/admin/photos
namespace 'admin' do
constraints subdomain: 'admin' do
resources :photos
end
end
### Custom constraints
class BlacklistConstraint
def initialize
@ips = Blacklist.retrieve_ips
end
def matches?(request)
@ips.include?(request.remote_ip)
end
end
TwitterClone::Application.routes.draw do
match "*path" => "blacklist#index",
:constraints => BlacklistConstraint.new
end
### Scopes
scope 'admin', constraints: { subdomain: 'admin' } do
resources ...
end
### Rack middleware
# Yes, Sprockets is middleware
match '/application.js' => Sprockets
### Route helpers
projects_path # /projects
projects_url # http://site.com/projects
### Default help text
# The priority is based upon order of creation:
# first created -> highest priority.
# Sample of regular route:
match 'products/:id' => 'catalog#view'
# Keep in mind you can assign values other than :controller and :action
# Sample of named route:
match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
# This route can be invoked with purchase_url(:id => product.id)
# Sample resource route (maps HTTP verbs to controller actions automatically):
resources :products
# Sample resource route with options:
resources :products do
member do
get 'short'
post 'toggle'
end
collection do
get 'sold'
end
end
# Sample resource route with sub-resources:
resources :products do
resources :comments, :sales
resource :seller
end
# Sample resource route with more complex sub-resources
resources :products do
resources :comments
resources :sales do
get 'recent', :on => :collection
end
end
# Sample resource route within a namespace:
namespace :admin do
# Directs /admin/products/* to Admin::ProductsController
# (app/controllers/admin/products_controller.rb)
resources :products
end
# You can have the root of your site routed with "root"
# just remember to delete public/index.html.
root :to => 'welcome#index'
# See how all your routes lay out with "rake routes"
# This is a legacy wild controller route that's not recommended for RESTful applications.
# Note: This route will make all actions in every controller accessible via GET requests.
match ':controller(/:action(/:id(.:format)))'

129
rails.md Normal file
View File

@ -0,0 +1,129 @@
Helpers
-------
class ApplicationController
helper_method :logged_in?
def logged_in?
"Something"
end
end
### CSS/JS packages
stylesheet_link_tag :monkey
javascript_link_tag :monkey
### Forms
# http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html
- form_for @person do |f|
= f.label :first_name
= f.label :first_name, "First name"
= f.text_field :first_name
= f.label :last_name>
= f.text_field :last_name>
- fields_for @person.permission do |fields|
= fields.checkbox :admin
-# name="person[admin]"
- fields_for :person, @client do |fields|
= fields.checkbox :admin
= f.submit
# Also: check_box, email_field, fields_for
# file_field, hidden_field, label, number_field, password_field
# radio_button, range_field, search_field, telephonen_field,
# text_area, text_field, url_field
Controllers
-----------
http://apidock.com/rails/ActionController/Base
class ProjectsController
layout 'project' # Actually defaults to `projects` based
# on the controller name
def save
end
def edit
end
end
### Before filter
class ApplicationController < ActionController::Base
before_filter :validate, only: [:save, :edit]
before_filter :ensure_auth, except: [:logout]
before_filter :require_login
private
def require_login
unless logged_in?
flash[:error] = "You must be logged in to access this section"
redirect_to new_login_url # halts request cycle
end
end
end
### Default URL optinos
class ApplicationController < ActionController::Base
# The options parameter is the hash passed in to 'url_for'
def default_url_options(options)
{:locale => I18n.locale}
end
end
### Hashes
session[:what]
flash[:notice] = "Your session expired"
params[:id]
### XML and JSON
class UsersController < ApplicationController
def index
@users = User.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @users}
format.json { render :json => @users}
end
end
end
### Redirection
redirect_to action: 'show', id: @entry.id
redirect_to root_url # a path
### Render
render nothing: true
render template: 'products/show'
render action: 'something' # same as `file: 'my/something'`
# Renders the template only, does not execute
# the action
Layouts
-------
# app/views/layouts/application.html.erb
<%= content_for?(:content) ? yield :content : yield %>
# app/views/layouts/news.html.erb
<% content_for :content do %>
...
<% end %>
<% render template: :'layouts/application' %>

28
rdoc.md Normal file
View File

@ -0,0 +1,28 @@
title: Rdoc
----
### Basic RDoc format
# Foo.
#
# @example
#
# y
# g
#
# @param [String] param_name The xx and xx.
#
# @see http://url.com
#
# @return [true] if so
#
# == Definition lists
#
# list:: hi.
# +foo+:: parameterized
#
# == Definition lists
# [foo] also
# [bar] like this
http://rdoc.rubyforge.org/RDoc/Markup.html

51
rst.md Normal file
View File

@ -0,0 +1,51 @@
title: ReStructuredText
---
### Comments
.. @theme 2010
.. include:: ../themes/2010/common.rst
.. contents::
.. |substitute| replace:: replacement name
### Headings
Heading
=======
.. class:: brief
Hello there. |substitute| **This is bold**
- Bullet list with a link_ (or `link with words`_)
- Yes
.. _link: http://link.org
### PDF page break
.. raw:: pdf
PageBreak oneColumn
### Link targets
Internal link target_.
.. _target:
This is where _target will end up in.
### Tables (?)
.. class:: hash-table
.. list-table::
* - :key:`Weekly work hours:`
- :val:`50 hours`
* - :key:`Cost per hour:`
- :val:`PhP 1,500/hour`
* - :key:`Weekly rate:`
- :val:`PhP 75,000/week`

386
sequel.rdoc Normal file
View File

@ -0,0 +1,386 @@
title: Sequel
----
= Cheat Sheet
== Open a database
require 'rubygems'
require 'sequel'
DB = Sequel.sqlite('my_blog.db')
DB = Sequel.connect('postgres://user:password@localhost/my_db')
DB = Sequel.postgres('my_db', :user => 'user', :password => 'password', :host => 'localhost')
DB = Sequel.ado('mydb')
== Open an SQLite memory database
Without a filename argument, the sqlite adapter will setup a new sqlite database in memory.
DB = Sequel.sqlite
== Logging SQL statements
require 'logger'
DB = Sequel.sqlite '', :loggers => [Logger.new($stdout)]
# or
DB.loggers << Logger.new(...)
== Using raw SQL
DB.run "CREATE TABLE users (name VARCHAR(255) NOT NULL, age INT(3) NOT NULL)"
dataset = DB["SELECT age FROM users WHERE name = ?", name]
dataset.map(:age)
DB.fetch("SELECT name FROM users") do |row|
p row[:name]
end
== Create a dataset
dataset = DB[:items]
dataset = DB.from(:items)
== Most dataset methods are chainable
dataset = DB[:managers].where(:salary => 5000..10000).order(:name, :department)
== Insert rows
dataset.insert(:name => 'Sharon', :grade => 50)
== Retrieve rows
dataset.each{|r| p r}
dataset.all # => [{...}, {...}, ...]
dataset.first # => {...}
== Update/Delete rows
dataset.filter(~:active).delete
dataset.filter('price < ?', 100).update(:active => true)
== Datasets are Enumerable
dataset.map{|r| r[:name]}
dataset.map(:name) # same as above
dataset.inject(0){|sum, r| sum + r[:value]}
dataset.sum(:value) # same as above
== Filtering (see also doc/dataset_filtering.rdoc)
=== Equality
dataset.filter(:name => 'abc')
dataset.filter('name = ?', 'abc')
=== Inequality
dataset.filter{value > 100}
dataset.exclude{value <= 100}
=== Inclusion
dataset.filter(:value => 50..100)
dataset.where{(value >= 50) & (value <= 100)}
dataset.where('value IN ?', [50,75,100])
dataset.where(:value=>[50,75,100])
dataset.where(:id=>other_dataset.select(:other_id))
=== Subselects as scalar values
dataset.where('price > (SELECT avg(price) + 100 FROM table)')
dataset.filter{price > dataset.select(avg(price) + 100)}
=== LIKE/Regexp
DB[:items].filter(:name.like('AL%'))
DB[:items].filter(:name => /^AL/)
=== AND/OR/NOT
DB[:items].filter{(x > 5) & (y > 10)}.sql
# SELECT * FROM items WHERE ((x > 5) AND (y > 10))
DB[:items].filter({:x => 1, :y => 2}.sql_or & ~{:z => 3}).sql
# SELECT * FROM items WHERE (((x = 1) OR (y = 2)) AND (z != 3))
=== Mathematical operators
DB[:items].filter((:x + :y) > :z).sql
# SELECT * FROM items WHERE ((x + y) > z)
DB[:items].filter{price - 100 < avg(price)}.sql
# SELECT * FROM items WHERE ((price - 100) < avg(price))
== Ordering
dataset.order(:kind)
dataset.reverse_order(:kind)
dataset.order(:kind.desc, :name)
== Limit/Offset
dataset.limit(30) # LIMIT 30
dataset.limit(30, 10) # LIMIT 30 OFFSET 10
== Joins
DB[:items].left_outer_join(:categories, :id => :category_id).sql
# SELECT * FROM items LEFT OUTER JOIN categories ON categories.id = items.category_id
DB[:items].join(:categories, :id => :category_id).join(:groups, :id => :items__group_id)
# SELECT * FROM items INNER JOIN categories ON categories.id = items.category_id INNER JOIN groups ON groups.id = items.group_id
== Aggregate functions methods
dataset.count #=> record count
dataset.max(:price)
dataset.min(:price)
dataset.avg(:price)
dataset.sum(:stock)
dataset.group_and_count(:category)
dataset.group(:category).select(:category, :AVG.sql_function(:price))
== SQL Functions / Literals
dataset.update(:updated_at => :NOW.sql_function)
dataset.update(:updated_at => 'NOW()'.lit)
dataset.update(:updated_at => "DateValue('1/1/2001')".lit)
dataset.update(:updated_at => :DateValue.sql_function('1/1/2001'))
== Schema Manipulation
DB.create_table :items do
primary_key :id
String :name, :unique => true, :null => false
TrueClass :active, :default => true
foreign_key :category_id, :categories
DateTime :created_at
index :created_at
end
DB.drop_table :items
DB.create_table :test do
String :zipcode
enum :system, :elements => ['mac', 'linux', 'windows']
end
== Aliasing
DB[:items].select(:name.as(:item_name))
DB[:items].select(:name___item_name)
DB[:items___items_table].select(:items_table__name___item_name)
# SELECT items_table.name AS item_name FROM items AS items_table
== Transactions
DB.transaction do
dataset.insert(:first_name => 'Inigo', :last_name => 'Montoya')
dataset.insert(:first_name => 'Farm', :last_name => 'Boy')
end # Either both are inserted or neither are inserted
Database#transaction is re-entrant:
DB.transaction do # BEGIN issued only here
DB.transaction
dataset << {:first_name => 'Inigo', :last_name => 'Montoya'}
end
end # COMMIT issued only here
Transactions are aborted if an error is raised:
DB.transaction do
raise "some error occurred"
end # ROLLBACK issued and the error is re-raised
Transactions can also be aborted by raising Sequel::Rollback:
DB.transaction do
raise(Sequel::Rollback) if something_bad_happened
end # ROLLBACK issued and no error raised
Savepoints can be used if the database supports it:
DB.transaction do
dataset << {:first_name => 'Farm', :last_name => 'Boy'} # Inserted
DB.transaction(:savepoint=>true) # This savepoint is rolled back
dataset << {:first_name => 'Inigo', :last_name => 'Montoya'} # Not inserted
raise(Sequel::Rollback) if something_bad_happened
end
dataset << {:first_name => 'Prince', :last_name => 'Humperdink'} # Inserted
end
== Miscellaneous:
dataset.sql # "SELECT * FROM items"
dataset.delete_sql # "DELETE FROM items"
dataset.where(:name => 'sequel').exists # "EXISTS ( SELECT * FROM items WHERE name = 'sequel' )"
dataset.columns #=> array of columns in the result set, does a SELECT
DB.schema(:items) => [[:id, {:type=>:integer, ...}], [:name, {:type=>:string, ...}], ...]
----------------------------------------------------------------------------------------------------------------------------------------------------------------
== Documents
http://sequel.rubyforge.org/rdoc/files/doc/association_basics_rdoc.html
http://sequel.rubyforge.org/rdoc/classes/Sequel/Schema/Generator.html
http://sequel.rubyforge.org/rdoc/files/doc/validations_rdoc.html
http://sequel.rubyforge.org/rdoc/classes/Sequel/Model.html
== Alter table
database.alter_table :deals do
add_column :name, String
drop_column :column_name
rename_column :from, :to
add_constraint :valid_name, :name.like('A%')
drop_constraint :constraint
add_full_text_index :body
add_spacial_index [columns]
add_index :price
drop_index :index
add_foreign_key :artist_id, :table
add_primary_key :id
add_unique_constraint [columns]
set_column_allow_null :foo, false
set_column_default :title, ''
set_column_type :price, 'char(10)'
end
== Model associations
class Deal < Sequel::Model
# Us (left) <=> Them (right)
many_to_many :images,
left_id: :deal_id,
right_id: :image_id,
join_table: :image_links
one_to_many :files,
key: :deal_id,
class: :DataFile,
many_to_one :parent, class: self
one_to_many :children, key: :parent_id, class: self
one_to_many :gold_albums, class: :Album do |ds|
ds.filter { copies_sold > 50000 }
end
Provided by many_to_many
Deal[1].images
Deal[1].add_image
Deal[1].remove_image
Deal[1].remove_all_images
== Validations
def validate
super
errors.add(:name, 'cannot be empty') if !name || name.empty?
validates_presence [:title, :site]
validates_unique :name
validates_format /\Ahttps?:\/\//, :website, :message=>'is not a valid URL'
validates_includes %w(a b c), :type
validates_integer :rating
validates_numeric :number
validates_type String, [:title, :description]
validates_integer :rating if new?
# options: :message =>, :allow_nil =>, :allow_blank =>,
# :allow_missing =>,
validates_exact_length 17, :isbn
validates_min_length 3, :name
validates_max_length 100, :name
validates_length_range 3..100, :name
# Setter override
def filename=(name)
@values[:filename] = name
end
end
end
deal.errors
== Model stuff
deal = Deal[1]
deal.changed_columns
deal.destory # Calls hooks
deal.delete # No hooks
deal.exists?
deal.new?
deal.hash # Only uniques
deal.keys #=> [:id, :name]
deal.modified!
deal.modified?
deal.lock!
== Callbacks
before_create
after_create
before_validation
after_validation
before_save
before_update
UPDATE QUERY
after_update
after_save
before_destroy
DELETE QUERY
after_destroy
== Schema
class Deal < Sequel::Model
set_schema do
primary_key :id
primary_key [:id, :title]
String :name, primary_key: true
String :title
Numeric :price
DateTime :expires
unique :whatever
check(:price) { num > 0 }
foreign_key :artist_id
String :artist_name, key: :id
index :title
index [:artist_id, :name]
full_text_index :title
# String, Integer, Fixnum, Bignum, Float, Numeric, BigDecimal,
# Date, DateTime, Time, File, TrueClass, FalseClass
end
end
== Unrestrict primary key
Category.create id: 'travel' # error
Category.unrestrict_primary_key
Category.create id: 'travel' # ok

93
style.sass Normal file
View File

@ -0,0 +1,93 @@
@import 'compass/css3'
body
font-family: 'pt sans', sans-serif
font-size: 14px
line-height: 1.5
html
background: #505060
body
width: 800px
margin: 20px auto
padding: 20px
+border-radius(2px)
+box-shadow(0 2px 3px rgba(black, 0.2))
background: #fafafa
color: #333
p, ul, ol, pre
margin: 15px 0
h1, h2, h3, h4, h5, h6
margin: 40px 0 15px 0
h1
font-family: georgia, serif
text-align: center
font-size: 24pt
color: #aaa
font-style: italic
font-weight: normal
margin: 10px 0
padding: 10px 0
border-bottom: dotted 1px #ddd
border-top: dotted 1px #ddd
h2, h3
color: #88a
font-size: 1.5em
border-bottom: solid 1px #ddd
padding-bottom: 3px
h3
font-size: 1.3em
pre, code
font-family: monaco, monospace
font-size: 12px
color: #444
pre
line-height: 1.6
background: #f7f7f2
padding: 10px 35px
+box-shadow(inset 0 0 5px rgba(black, 0.1))
overflow-x: auto
margin-left: -20px
margin-right: -20px
h2+pre, h3+pre
border-top: solid 2px #c0c0dd
margin-top: -16px
ul.pages
&, li
margin: 0
padding: 0
list-style-type: none
&
overflow: hidden
margin: 40px 0
li
width: 20%
float: left
a
display: block
text-decoration: none
padding: 2px 5px
+border-radius(3px)
a:hover
background: #eee
li:first-letter
text-transform: uppercase
@import '_prettify'

61
textile.md Normal file
View File

@ -0,0 +1,61 @@
title: Textile
---
### Pre blocks
<pre>
I am <b>very serious.</b> -- this will get escaped.
</pre>
### Line breaks
Line breaks.
Just break the lines.
### Entities
one(TM), two(R), three(C).
### Inlines
_em_ *strong* __bold-italic__. ??citation??.
@code@. -strikehtrough-. +insertion+.
%span%. %{color:red}formatting%.
"Hypertext":index.html
"Text link":link
[link]http://link.com
!image.jpg!
!image.jpg(title text)!
!image.jpg!:link.html
!>right.jpg!
### Horizontal line
--
### Blocks
h1. Header 1
h2. Header 2
bq. Blockquote
p(classname). Class.
p(#id). ID.
### Lists
## ordered list
* unordered list
### Footnotes
Footnotes[1].
fn1. Something.

32
tig.md Normal file
View File

@ -0,0 +1,32 @@
Tig shortcuts
-------------
Invocation
tig blame FILE
tig master # Show a branch
tig test..master # Show difference between two bracnhes
tig FILE # Show history of file
tig v0.0.3:README # Show contents of file in a specific revision
All views
---------
* `^N` - Next on parent view
* `^P` - Previous on parent view
`m` - Main view
* `D` - Toggle between date display modes
* `A` - Toggle between author display modes
* `C` - Cherry pick a commit
`S` - Stage view
* `u` - Stage/unstage file or chunk
* `!` - Revert file or chunk
* `C` - Commit
* `M` - Merge
`H` - Branch view
* `i` - Change sort header

51
tmux.md Normal file
View File

@ -0,0 +1,51 @@
title: tmux Terminal Multiplexer
---
### Commands
$ tmux
-u # UTF8 mode
-S ~/.tmux.socket
$ tmux attach
### Help
C-b ?
### Scrolling
C-b [ # Enter scroll mode then press up and down
### Copy/paste
C-b [ # 1. Enter scroll mode first.
Space # 2. Start selecting and move around.
Enter # 3. Press enter to copy.
C-b ] # Paste
### Panes
C-b v # vert
C-b n # horiz
C-b hkjl # navigation
C-b HJKL # resize
C-b o # next window
C-b x # close pane
C-b { or } # move windows around
### Windows
C-b c # New window
C-b 1 # Go to window 1
### Detach/attach
C-b d # detatch
C-b ( ) # Switch through sessions
$ tmux attach
### Niceties
C-b t # Time

13
ubuntu.md Normal file
View File

@ -0,0 +1,13 @@
title: Ubuntu/Debian
----
### Aptitude stuff
aptitude search mysql # Look for something
dpkg -S `which tsclient` # What package does it belong to?
dpkg -L aria2c # What does this package provide?
dpkg -i *.deb # Install a deb file
### Apt archives path
/var/cache/apt/archives

19
unicode.txt Normal file
View File

@ -0,0 +1,19 @@
▲▼▶
⬅⬆⬇
◢ ◣ ◤ ◥
: «» XOR , (), [], •, ⌘, ⌥, ▲▼, ▸▹, ◇ XOR ◆, ◐◑◒◓ ◢ ◣ ◤ ◥, ★ ☆ , ♠♥♣♦, ⚐⚑, ✂
ࣾ home &#002302;
information &#008505;
♡ heart &#009825;
⚙ cog or gear &#009881;
⚿ key &#009919;
✉ envelope &#009993;
✎ pencil &#009998;
✓ check or tick mark &#010003;
❌ cross mark &#010060;
💬 speech balloon &#128172;

36
vim.md Normal file
View File

@ -0,0 +1,36 @@
title: vim
----
. - repeat last command
]p - paste under the current indentation level
Motions
-------
vip - Select paragraph
vipipipip - Select more
ap - a paragraph
ip - inner paragraph
{a,i}p - Paragraph
{a,i}w - Word
{a,i}s - Sentence
ab - A block [(
aB - A block in [{
at - A XML tag block
a[ ( { < - A [], (), or {} block
a' " ` - A quoted string
Example:
yip - Yank inner paragraph
yap - Yank paragraph (including newline)
SCSS!
-----
va{= - reindent block

20
zsh.md Normal file
View File

@ -0,0 +1,20 @@
title: ZSH
---
### Stuff
!! Last command (sudo !!)
!* Last command's parameters (vim !*)
!^ Last command's first parameter
!$ Last command's last parameter
!?ls<tab> Command and params of last `ls` command (sudo !?mv<tab>)
!?ls?:*<tab> Params of last `ls` command
*(m0) Last modified today
*(m-4) Last modified <4 days ago
### Change default shell
chsh -s `which zsh`