Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

#controllers/application.rb
...
class Hash
def diff(other)
keys = self.keys
keys.each.select{|k| self[k] != other[k]}
end
end
#config/init.rb
;; example rails like console for compojure showing params, etc. for each request
(ns bus.core
(:use compojure.core
ring.adapter.jetty
...
...
(:use ring.middleware.params))
@svs
svs / ruby-mode.el.diff
Created January 30, 2012 17:44
ruby-mode.el differentiate symbols from ordinary variables
git diff
diff --git a/ruby-mode.el b/ruby-mode.el
index b556e95..838d811 100644
--- a/ruby-mode.el
+++ b/ruby-mode.el
@@ -1169,7 +1169,7 @@ balanced expression is found."
2 font-lock-type-face)
;; symbols
'("\\(^\\|[^:]\\)\\(:\\([-+~]@?\\|[/%&|^`]\\|\\*\\*?\\|<\\(<\\|=>?\\)?\\|>[>=]?\\|===?\\|=~\\|\\[\\]=?\\|\\(\\w\\|_\\)+\\([!?=]\\|\\b_*\\)\\|#{[^}\n\\\\]*\\(\
- 2 font-lock-reference-face)
class LoanHistory
include DataMapper::Resource
property :loan_id, Integer, :key => true
property :date, Date, :key => true # the day that this record applies to
property :created_at, DateTime # automatic, nice for benchmarking runs
# some properties for similarly named methods of a loan:
property :scheduled_outstanding_total, Decimal, :nullable => false
@svs
svs / gist:2478214
Created April 24, 2012 09:23
Question about Rich Hickey's 'use hashes not classes' statement

So I don't have a model called Person, but I use hashes like {:name => "Siddharth", :sex => "Y"}. Great. Now I want to validate the presence of certain fields before persisting to the database. What do I do?

Module PersonValidation
  function validate_presence_of_name(person_hash)
    person_hash[:errors][:name] = "EEENKKK!" unless person_hash[:name]
  end
  
  ...
 ...
@svs
svs / workflow_spec.rb
Created July 13, 2012 12:12
Unit Testing a Workflow
describe Document do
context "with a scan" do
subject {FactoryGirl.build(:scannable)}
it {should have_events([:scan])}
end
context "taggable" do
subject { FactoryGirl.build(:taggable) }
it {should have_events([:to_edit])}
@svs
svs / gist:3105262
Created July 13, 2012 14:44
omg spec!
it "should move properly between states" do
@doc = Factory.build(:taggable, :user => @customer)
@doc.scan!(@customer)
@doc.scanned?.should == true
@doc.digi_document_state_transitions.count.should == 1
expect { @doc.tag_complete!}.should raise_exception # should not 'complete'
@doc.completed?.should == false
@doc.to_edit!(@customer) # should 'edit'
@doc.editing?.should == true
@doc.digi_document_state_transitions.count.should == 2
@svs
svs / gist:3496414
Created August 28, 2012 09:10
B.S. i.e. Before StateMachine
class Document
property :name, Text
.... # all the other properties
....
property :scanned_on, Date
property :tagged_on, Date
property :approved_on, Date
@svs
svs / gist:3496546
Created August 28, 2012 09:30
AWSM - After Workflow State Machine
class Document
# ....
# ..snip...
# ....
workflow do
state :created do
event :scan, :transitions_to => :scanned, :if => Proc.new{|t| !(t.scans.empty?)}
event :to_edit, :transitions_to => :editing
end
state :scanned do
@svs
svs / gist:3496733
Created August 28, 2012 09:54
update action
# API is POST {:action => "update", :event => "approve", :id => 3, :document => {:comment => "some comment"}}
def update
@document = Document.get(params[:id])
if @document.send(params[:event], current_user)
@document.update_attributes(params[:document])
end
respond_to do |format|
# ...
# ...
end