Skip to content

Instantly share code, notes, and snippets.

@snuxoll
snuxoll / safe_session.rb
Created January 30, 2009 11:36
Safe session extension for merb
# Safe session extension for merb applications. Generates a
# unique session salt for an application install automatically
# instead of requiring the user to do so themselves.
class SafeSession
require "digest/sha1"
attr_reader :salt
# Attempts to load the session salt from the saltfile, if it does
# not exist a new salt is generated
@snuxoll
snuxoll / init.rb
Created January 30, 2009 11:49
Configuration example for SafeSession
Merb::Config.use do |c|
c[:use_mutex] = false
# Setup the session store, require the safe_session extension to
# generate a unique salt for the application if one does not
# already exist
c[:session_store] = 'cookie'
require File.join(Merb.root, "merb/extensions/safe_session.rb")
safe_session = SafeSession.new
c[:session_secret_key] = safe_session.salt
c[:session_id_key] = '_irsea_session_id'
# Module containing a collect_kv method to allow
# a new hash to be generated my collect instead
# of returning an array of modified keys or values
module CollectKeyValue
# Takes a block that returns a [key, value] pair
# and builds a new hash based on those pairs
def collect_kv
result = {}
each do |k,v|
# Create a new hash and extend it with our CollectKeyValue
# module
hash ={
"key_1"=>"one",
"key_2"=>"two",
"key_3"=>"three",
"key_4"=>"four"
}.extend(CollectKeyValue)
# Now lets call our new hash#collect_kv method and rename
require 'rubygems'
require 'hpricot'
doc = Hpricot( File.open("file.html") )
(doc / "span.foo").each do |span|
span.swap("***#{span.inner_html}***")
end
def add(num1)
lambda { |num2| num1 + num2 }
end
add(2).call(2) # returns 4
# Mixin to scan an array for objects that respond to a given method
module WithMethod
# Iterates through the array and returns a list of items that
# respond to method
def with_method(method)
select { |item| item.respond_to? method }
end
end
#
# Return UTM Zone for a geom
#
# Return Integer
def utm_zone
geomgeog = srid == 4326 ? self[get_column_name] : transform(4326)
puts geomgeog.class
geom = case geomgeog
when Point then geomgeog
when LineString then geomgeog.first
# Adds the two arguments passed in to the method
def add(num1, num2)
num1 + num2
end
# We have an array that we want to pass in to add,
# with each item as a seperate argument
my_args = [1, 2]
# Prefixing a variable name with a * will attempt
# adds two named arguments passed in, named :first and
# :second respectively
def add(args)
args[:first] + args[:second]
end
# Call it using 1.8 and below style
puts add(:first => 2, :second => 4) # 6
# Call it using the 1.9 style hash syntax