1
0
Fork 0
mirror of synced 2024-11-17 14:35:34 -05:00
ultimate-vim/sources_non_forked/snipmate-snippets/snippets/ruby.snippets

926 lines
20 KiB
Text
Raw Normal View History

2012-08-16 23:41:25 -04:00
########################################
# Ruby snippets - for Rails, see below #
########################################
# #!/usr/bin/env ruby
snippet #!
#!/usr/bin/env ruby
# encoding: utf-8
# New Block
snippet =b
=begin rdoc
${1}
=end
snippet y
:yields: ${1:arguments}
snippet rb
#!/usr/bin/env ruby -wKU
snippet beg
begin
${3}
rescue ${1:Exception} => ${2:e}
end
snippet req
require "${1}"${2}
snippet #
# =>
snippet end
__END__
snippet case
case ${1:object}
when ${2:condition}
${3}
end
snippet when
when ${1:condition}
${2}
snippet def
def ${1:method_name}
${2}
end
snippet deft
def test_${1:case_name}
${2}
end
snippet if
if ${1:condition}
${2}
end
snippet ife
if ${1:condition}
${2}
else
${3}
end
snippet elsif
elsif ${1:condition}
${2}
snippet unless
unless ${1:condition}
${2}
end
snippet while
while ${1:condition}
${2}
end
snippet for
for ${1:e} in ${2:c}
${3}
end
snippet until
until ${1:condition}
${2}
end
snippet cla class .. end
class ${1:`substitute(Filename(), '\(_\|^\)\(.\)', '\u\2', 'g')`}
${2}
end
snippet cla class .. initialize .. end
class ${1:`substitute(Filename(), '\(_\|^\)\(.\)', '\u\2', 'g')`}
def initialize(${2:args})
${3}
end
end
snippet cla class .. < ParentClass .. initialize .. end
class ${1:`substitute(Filename(), '\(_\|^\)\(.\)', '\u\2', 'g')`} < ${2:ParentClass}
def initialize(${3:args})
${4}
end
end
snippet cla ClassName = Struct .. do .. end
${1:`substitute(Filename(), '\(_\|^\)\(.\)', '\u\2', 'g')`} = Struct.new(:${2:attr_names}) do
def ${3:method_name}
${4}
end
end
snippet cla class BlankSlate .. initialize .. end
class ${1:BlankSlate}
instance_methods.each { |meth| undef_method(meth) unless meth =~ /\A__/ }
end
snippet cla class << self .. end
class << ${1:self}
${2}
end
# class .. < DelegateClass .. initialize .. end
snippet cla-
class ${1:`substitute(Filename(), '\(_\|^\)\(.\)', '\u\2', 'g')`} < DelegateClass(${2:ParentClass})
def initialize(${3:args})
super(${4:del_obj})
${5}
end
end
snippet mod module .. end
module ${1:`substitute(Filename(), '\(_\|^\)\(.\)', '\u\2', 'g')`}
${2}
end
snippet mod module .. module_function .. end
module ${1:`substitute(Filename(), '\(_\|^\)\(.\)', '\u\2', 'g')`}
module_function
${2}
end
snippet mod module .. ClassMethods .. end
module ${1:`substitute(Filename(), '\(_\|^\)\(.\)', '\u\2', 'g')`}
module ClassMethods
${2}
end
module InstanceMethods
end
def self.included(receiver)
receiver.extend ClassMethods
receiver.send :include, InstanceMethods
end
end
# attr_reader
snippet r
attr_reader :${1:attr_names}
# attr_writer
snippet w
attr_writer :${1:attr_names}
# attr_accessor
snippet rw
attr_accessor :${1:attr_names}
snippet atp
attr_protected :${1:attr_names}
snippet ata
attr_accessible :${1:attr_names}
# include Enumerable
snippet Enum
include Enumerable
def each(&block)
${1}
end
# include Comparable
snippet Comp
include Comparable
def <=>(other)
${1}
end
# extend Forwardable
snippet Forw-
extend Forwardable
# def self
snippet defs
def self.${1:class_method_name}
${2}
end
# def method_missing
snippet defmm
def method_missing(meth, *args, &blk)
${1}
end
snippet defd
def_delegator :${1:@del_obj}, :${2:del_meth}, :${3:new_name}
snippet defds
def_delegators :${1:@del_obj}, :${2:del_methods}
snippet am
alias_method :${1:new_name}, :${2:old_name}
snippet app
if __FILE__ == $PROGRAM_NAME
${1}
end
# usage_if()
snippet usai
if ARGV.${1}
abort "Usage: #{$PROGRAM_NAME} ${2:ARGS_GO_HERE}"${3}
end
# usage_unless()
snippet usau
unless ARGV.${1}
abort "Usage: #{$PROGRAM_NAME} ${2:ARGS_GO_HERE}"${3}
end
snippet array
Array.new(${1:10}) { |${2:i}| ${3} }
snippet hash
Hash.new { |${1:hash}, ${2:key}| $1[$2] = ${3} }
snippet file File.foreach() { |line| .. }
File.foreach(${1:"path/to/file"}) { |${2:line}| ${3} }
snippet file File.read()
File.read(${1:"path/to/file"})${2}
snippet Dir Dir.global() { |file| .. }
Dir.glob(${1:"dir/glob/*"}) { |${2:file}| ${3} }
snippet Dir Dir[".."]
Dir[${1:"glob/**/*.rb"}]${2}
snippet dir
Filename.dirname(__FILE__)
snippet deli
delete_if { |${1:e}| ${2} }
snippet fil
fill(${1:range}) { |${2:i}| ${3} }
# flatten_once()
snippet flao
inject(Array.new) { |${1:arr}, ${2:a}| $1.push(*$2)}${3}
snippet zip
zip(${1:enums}) { |${2:row}| ${3} }
# downto(0) { |n| .. }
snippet dow
downto(${1:0}) { |${2:n}| ${3} }
snippet ste
step(${1:2}) { |${2:n}| ${3} }
snippet tim
times { |${1:n}| ${2} }
snippet upt
upto(${1:1.0/0.0}) { |${2:n}| ${3} }
snippet loo
loop { ${1} }
snippet ea
each { |${1:e}| ${2} }
snippet ead
each do |${1:e}|
${2}
end
snippet eab
each_byte { |${1:byte}| ${2} }
snippet eac- each_char { |chr| .. }
each_char { |${1:chr}| ${2} }
snippet eac- each_cons(..) { |group| .. }
each_cons(${1:2}) { |${2:group}| ${3} }
snippet eai
each_index { |${1:i}| ${2} }
snippet eaid
each_index do |${1:i}|
${2}
end
snippet eak
each_key { |${1:key}| ${2} }
snippet eakd
each_key do |${1:key}|
${2}
end
snippet eal
each_line { |${1:line}| ${2} }
snippet eald
each_line do |${1:line}|
${2}
end
snippet eap
each_pair { |${1:name}, ${2:val}| ${3} }
snippet eapd
each_pair do |${1:name}, ${2:val}|
${3}
end
snippet eas-
each_slice(${1:2}) { |${2:group}| ${3} }
snippet easd-
each_slice(${1:2}) do |${2:group}|
${3}
end
snippet eav
each_value { |${1:val}| ${2} }
snippet eavd
each_value do |${1:val}|
${2}
end
snippet eawi
each_with_index { |${1:e}, ${2:i}| ${3} }
snippet eawid
each_with_index do |${1:e},${2:i}|
${3}
end
snippet reve
reverse_each { |${1:e}| ${2} }
snippet reved
reverse_each do |${1:e}|
${2}
end
snippet inj
inject(${1:init}) { |${2:mem}, ${3:var}| ${4} }
snippet injd
inject(${1:init}) do |${2:mem}, ${3:var}|
${4}
end
snippet map
map { |${1:e}| ${2} }
snippet mapd
map do |${1:e}|
${2}
end
snippet mapwi-
enum_with_index.map { |${1:e}, ${2:i}| ${3} }
snippet sor
sort { |a, b| ${1} }
snippet sorb
sort_by { |${1:e}| ${2} }
snippet ran
sort_by { rand }
snippet all
all? { |${1:e}| ${2} }
snippet any
any? { |${1:e}| ${2} }
snippet cl
classify { |${1:e}| ${2} }
snippet col
collect { |${1:e}| ${2} }
snippet cold
collect do |${1:e}|
${2}
end
snippet det
detect { |${1:e}| ${2} }
snippet detd
detect do |${1:e}|
${2}
end
snippet fet
fetch(${1:name}) { |${2:key}| ${3} }
snippet fin
find { |${1:e}| ${2} }
snippet find
find do |${1:e}|
${2}
end
snippet fina
find_all { |${1:e}| ${2} }
snippet finad
find_all do |${1:e}|
${2}
end
snippet gre
grep(${1:/pattern/}) { |${2:match}| ${3} }
snippet sub
${1:g}sub(${2:/pattern/}) { |${3:match}| ${4} }
snippet sca
scan(${1:/pattern/}) { |${2:match}| ${3} }
snippet scad
scan(${1:/pattern/}) do |${2:match}|
${3}
end
snippet max
max { |a, b| ${1} }
snippet min
min { |a, b| ${1} }
snippet par
partition { |${1:e}| ${2} }
snippet pard
partition do |${1:e}|
${2}
end
snippet rej
reject { |${1:e}| ${2} }
snippet rejd
reject do |${1:e}|
${2}
end
snippet sel
select { |${1:e}| ${2} }
snippet seld
select do |${1:e}|
${2}
end
snippet lam
lambda { |${1:args}| ${2} }
snippet doo
do
${1}
end
snippet dov
do |${1:variable}|
${2}
end
snippet :
:${1:key} => ${2:"value"}${3}
snippet ope
open(${1:"path/or/url/or/pipe"}, "${2:w}") { |${3:io}| ${4} }
# path_from_here()
snippet patfh
File.join(File.dirname(__FILE__), *%2[${1:rel path here}])${2}
# unix_filter {}
snippet unif
ARGF.each_line${1} do |${2:line}|
${3}
end
# option_parse {}
snippet optp
require "optparse"
options = {${1:default => "args"}}
ARGV.options do |opts|
opts.banner = "Usage: #{File.basename($PROGRAM_NAME)}
snippet opt
opts.on( "-${1:o}", "--${2:long-option-name}", ${3:String},
"${4:Option description.}") do |${5:opt}|
${6}
end
snippet tc
require "test/unit"
require "${1:library_file_name}"
class Test${2:$1} < Test::Unit::TestCase
def test_${3:case_name}
${4}
end
end
snippet ts
require "test/unit"
require "tc_${1:test_case_file}"
require "tc_${2:test_case_file}"${3}
snippet as
assert ${1:test}, "${2:Failure message.}"${3}
snippet ase
assert_equal ${1:expected}, ${2:actual}${3}
snippet asne
assert_not_equal ${1:unexpected}, ${2:actual}${3}
snippet asid
assert_in_delta ${1:expected_float}, ${2:actual_float}, ${3:2 ** -20}${4}
snippet asio
assert_instance_of ${1:ExpectedClass}, ${2:actual_instance}${3}
snippet asko
assert_kind_of ${1:ExpectedKind}, ${2:actual_instance}${3}
snippet asn
assert_nil ${1:instance}${2}
snippet asnn
assert_not_nil ${1:instance}${2}
snippet asm
assert_match /${1:expected_pattern}/, ${2:actual_string}${3}
snippet asnm
assert_no_match /${1:unexpected_pattern}/, ${2:actual_string}${3}
snippet aso
assert_operator ${1:left}, :${2:operator}, ${3:right}${4}
snippet asr
assert_raise ${1:Exception} { ${2} }
snippet asrd
assert_raise ${1:Exception} do
${2}
end
snippet asnr
assert_nothing_raised ${1:Exception} { ${2} }
snippet asnrd
assert_nothing_raised ${1:Exception} do
${2}
end
snippet asrt
assert_respond_to ${1:object}, :${2:method}${3}
snippet ass assert_same(..)
assert_same ${1:expected}, ${2:actual}${3}
snippet ass assert_send(..)
assert_send [${1:object}, :${2:message}, ${3:args}]${4}
snippet asns
assert_not_same ${1:unexpected}, ${2:actual}${3}
snippet ast
assert_throws :${1:expected} { ${2} }
snippet astd
assert_throws :${1:expected} do
${2}
end
snippet asnt
assert_nothing_thrown { ${1} }
snippet asntd
assert_nothing_thrown do
${1}
end
snippet fl
flunk "${1:Failure message.}"${2}
# Benchmark.bmbm do .. end
snippet bm-
TESTS = ${1:10_000}
Benchmark.bmbm do |results|
${2}
end
snippet rep
results.report("${1:name}:") { TESTS.times { ${2} }}
# Marshal.dump(.., file)
snippet Md
File.open(${1:"path/to/file.dump"}, "wb") { |${2:file}| Marshal.dump(${3:obj}, $2) }${4}
# Mashal.load(obj)
snippet Ml
File.open(${1:"path/to/file.dump"}, "rb") { |${2:file}| Marshal.load($2) }${3}
# deep_copy(..)
snippet deec
Marshal.load(Marshal.dump(${1:obj_to_copy}))${2}
snippet Pn-
PStore.new(${1:"file_name.pstore"})${2}
snippet tra
transaction(${1:true}) { ${2} }
# xmlread(..)
snippet xml-
REXML::Document.new(File.read(${1:"path/to/file"}))${2}
# xpath(..) { .. }
snippet xpa
elements.each(${1:"//Xpath"}) do |${2:node}|
${3}
end
# class_from_name()
snippet clafn
split("::").inject(Object) { |par, const| par.const_get(const) }
# singleton_class()
snippet sinc
class << self; self end
snippet nam
namespace :${1:`Filename()`} do
${2}
end
snippet tas
desc "${1:Task description}"
task :${2:task_name => [:dependent, :tasks]} do
${3}
end
# block
snippet b
{ |${1:var}| ${2} }
snippet begin
begin
raise 'A test exception.'
rescue Exception => e
puts e.message
puts e.backtrace.inspect
else
# other exception
ensure
# always executed
end
#debugging
snippet debug
require 'ruby-debug'; debugger; true;
snippet pry
require 'pry'; binding.pry
#############################################
# Rails snippets - for pure Ruby, see above #
#############################################
snippet art
assert_redirected_to ${1::action => "${2:index}"}
snippet artnp
assert_redirected_to ${1:parent}_${2:child}_path(${3:@$1}, ${4:@$2})
snippet artnpp
assert_redirected_to ${1:parent}_${2:child}_path(${3:@$1})
snippet artp
assert_redirected_to ${1:model}_path(${2:@$1})
snippet artpp
assert_redirected_to ${1:model}s_path
snippet asd
assert_difference "${1:Model}.${2:count}", $1 do
${3}
end
snippet asnd
assert_no_difference "${1:Model}.${2:count}" do
${3}
end
snippet asre
assert_response :${1:success}, @response.body${2}
snippet asrj
assert_rjs :${1:replace}, "${2:dom id}"
snippet ass
assert_select '${1:path}', :${2:text} => '${3:inner_html' ${4:do}
snippet bf
before_filter :${1:method}
snippet bt
belongs_to :${1:association}
snippet crw
cattr_accessor :${1:attr_names}
snippet defcreate
def create
@${1:model_class_name} = ${2:ModelClassName}.new(params[:$1])
respond_to do |wants|
if @$1.save
flash[:notice] = '$2 was successfully created.'
wants.html { redirect_to(@$1) }
wants.xml { render :xml => @$1, :status => :created, :location => @$1 }
else
wants.html { render :action => "new" }
wants.xml { render :xml => @$1.errors, :status => :unprocessable_entity }
end
end
end${3}
snippet defdestroy
def destroy
@${1:model_class_name} = ${2:ModelClassName}.find(params[:id])
@$1.destroy
respond_to do |wants|
wants.html { redirect_to($1s_url) }
wants.xml { head :ok }
end
end${3}
snippet defedit
def edit
@${1:model_class_name} = ${2:ModelClassName}.find(params[:id])
end
snippet defindex
def index
@${1:model_class_name} = ${2:ModelClassName}.all
respond_to do |wants|
wants.html # index.html.erb
wants.xml { render :xml => @$1s }
end
end${3}
snippet defnew
def new
@${1:model_class_name} = ${2:ModelClassName}.new
respond_to do |wants|
wants.html # new.html.erb
wants.xml { render :xml => @$1 }
end
end${3}
snippet defshow
def show
@${1:model_class_name} = ${2:ModelClassName}.find(params[:id])
respond_to do |wants|
wants.html # show.html.erb
wants.xml { render :xml => @$1 }
end
end${3}
snippet defupdate
def update
@${1:model_class_name} = ${2:ModelClassName}.find(params[:id])
respond_to do |wants|
if @$1.update_attributes(params[:$1])
flash[:notice] = '$2 was successfully updated.'
wants.html { redirect_to(@$1) }
wants.xml { head :ok }
else
wants.html { render :action => "edit" }
wants.xml { render :xml => @$1.errors, :status => :unprocessable_entity }
end
end
end${3}
snippet flash
flash[:${1:notice}] = "${2}"
snippet habtm
has_and_belongs_to_many :${1:object}, :join_table => "${2:table_name}", :foreign_key => "${3}_id"${4}
snippet hm
has_many :${1:object}
snippet hmd
has_many :${1:other}s, :class_name => "${2:$1}", :foreign_key => "${3:$1}_id", :dependent => :destroy${4}
snippet hmt
has_many :${1:object}, :through => :${2:object}
snippet ho
has_one :${1:object}
snippet i18
I18n.t('${1:type.key}')${2}
snippet ist
<%= image_submit_tag("${1:agree.png}", :id => "${2:id}"${3} %>
snippet log
Rails.logger.${1:debug} ${2}
snippet log2
RAILS_DEFAULT_LOGGER.${1:debug} ${2}
snippet logd
logger.debug { "${1:message}" }${2}
snippet loge
logger.error { "${1:message}" }${2}
snippet logf
logger.fatal { "${1:message}" }${2}
snippet logi
logger.info { "${1:message}" }${2}
snippet logw
logger.warn { "${1:message}" }${2}
snippet mapc
${1:map}.${2:connect} '${3:controller/:action/:id}'
snippet mapca
${1:map}.catch_all "*${2:anything}", :controller => "${3:default}", :action => "${4:error}"${5}
snippet mapr
${1:map}.resource :${2:resource}
snippet maprs
${1:map}.resources :${2:resource}
snippet mapwo
${1:map}.with_options :${2:controller} => '${3:thing}' do |$3|
${4}
end
snippet mbs
before_save :${1:method}
snippet mcht
change_table :${1:table_name} do |t|
${2}
end
snippet mp
map(&:${1:id})
snippet mrw
mattr_accessor :${1:attr_names}
snippet oa
order("${1:field}")
snippet od
order("${1:field} DESC")
snippet pa
params[:${1:id}]${2}
snippet ra
render :action => "${1:action}"
snippet ral
render :action => "${1:action}", :layout => "${2:layoutname}"
snippet rest
respond_to do |wants|
wants.${1:html} { ${2} }
end
snippet rf
render :file => "${1:filepath}"
snippet rfu
render :file => "${1:filepath}", :use_full_path => ${2:false}
snippet ri
render :inline => "${1:<%= 'hello' %>}"
snippet ril
render :inline => "${1:<%= 'hello' %>}", :locals => { ${2::name} => "${3:value}"${4} }
snippet rit
render :inline => "${1:<%= 'hello' %>}", :type => ${2::rxml}
snippet rjson
render :json => ${1:text to render}
snippet rl
render :layout => "${1:layoutname}"
snippet rn
render :nothing => ${1:true}
snippet rns
render :nothing => ${1:true}, :status => ${2:401}
snippet rp
render :partial => "${1:item}"
snippet rpc
render :partial => "${1:item}", :collection => ${2:@$1s}
snippet rpl
render :partial => "${1:item}", :locals => { :${2:$1} => ${3:@$1}
snippet rpo
render :partial => "${1:item}", :object => ${2:@$1}
snippet rps
render :partial => "${1:item}", :status => ${2:500}
snippet rt
render :text => "${1:text to render}"
snippet rtl
render :text => "${1:text to render}", :layout => "${2:layoutname}"
snippet rtlt
render :text => "${1:text to render}", :layout => ${2:true}
snippet rts
render :text => "${1:text to render}", :status => ${2:401}
snippet ru
render :update do |${1:page}|
$1.${2}
end
snippet rxml
render :xml => ${1:text to render}
snippet sc
scope :${1:name}, :where(:@${2:field} => ${3:value})
snippet sl
scope :${1:name}, lambda do |${2:value}|
where("${3:field = ?}", ${4:bind var})
end
snippet sha1
Digest::SHA1.hexdigest(${1:string})
snippet sweeper
class ${1:ModelClassName}Sweeper < ActionController::Caching::Sweeper
observe $1
def after_save(${2:model_class_name})
expire_cache($2)
end
def after_destroy($2)
expire_cache($2)
end
def expire_cache($2)
expire_page
end
end
snippet tcb
t.boolean :${1:title}
${2}
snippet tcbi
t.binary :${1:title}, :limit => ${2:2}.megabytes
${3}
snippet tcd
t.decimal :${1:title}, :precision => ${2:10}, :scale => ${3:2}
${4}
snippet tcda
t.date :${1:title}
${2}
snippet tcdt
t.datetime :${1:title}
${2}
snippet tcf
t.float :${1:title}
${2}
snippet tch
t.change :${1:name}, :${2:string}, :${3:limit} => ${4:80}
${5}
snippet tci
t.integer :${1:title}
${2}
snippet tcl
t.integer :lock_version, :null => false, :default => 0
${1}
snippet tcr
t.references :${1:taggable}, :polymorphic => { :default => '${2:Photo}' }
${3}
snippet tcs
t.string :${1:title}
${2}
snippet tct
t.text :${1:title}
${2}
snippet tcti
t.time :${1:title}
${2}
snippet tcts
t.timestamp :${1:title}
${2}
snippet tctss
t.timestamps
${1}
snippet va
validates_associated :${1:attribute}
snippet vao
validates_acceptance_of :${1:terms}
snippet vc
validates_confirmation_of :${1:attribute}
snippet ve
validates_exclusion_of :${1:attribute}, :in => ${2:%w( mov avi )}
snippet vf
validates_format_of :${1:attribute}, :with => /${2:regex}/
snippet vi
validates_inclusion_of :${1:attribute}, :in => %w(${2: mov avi })
snippet vl
validates_length_of :${1:attribute}, :within => ${2:3}..${3:20}
snippet vn
validates_numericality_of :${1:attribute}
snippet vpo
validates_presence_of :${1:attribute}
snippet vu
validates_uniqueness_of :${1:attribute}
snippet wants
wants.${1:js|xml|html} { ${2} }
snippet wc
where(${1:"conditions"}${2:, bind_var})
snippet wh
where(${1:field} => ${2:value})
snippet xdelete
xhr :delete, :${1:destroy}, :id => ${2:1}${3}
snippet xget
xhr :get, :${1:show}, :id => ${2:1}${3}
snippet xpost
xhr :post, :${1:create}, :${2:object} => { ${3} }
snippet xput
xhr :put, :${1:update}, :id => ${2:1}, :${3:object} => { ${4} }${5}
snippet test
test "should ${1:do something}" do
${2}
end
#migrations
snippet mac
add_column :${1:table_name}, :${2:column_name}, :${3:data_type}
snippet mrc
remove_column :${1:table_name}, :${2:column_name}
snippet mrnc
rename_column :${1:table_name}, :${2:old_column_name}, :${3:new_column_name}
snippet mcc
change_column :${1:table}, :${2:column}, :${3:type}
snippet mccc
t.column :${1:title}, :${2:string}
snippet mct
create_table :${1:table_name} do |t|
t.column :${2:name}, :${3:type}
end
snippet migration
class ${1:class_name} < ActiveRecord::Migration
def self.up
${2}
end
def self.down
end
end
snippet trc
t.remove :${1:column}
snippet tre
t.rename :${1:old_column_name}, :${2:new_column_name}
${3}
snippet tref
t.references :${1:model}
#rspec
snippet it
it "${1:spec_name}" do
${2}
end
snippet itp
it "${1:spec_name}"
${2}
snippet desc
describe ${1:class_name} do
${2}
end
snippet cont
context "${1:message}" do
${2}
end
snippet bef
before :${1:each} do
${2}
end
snippet aft
after :${1:each} do
${2}
end