This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'hpricot' | |
doc = Hpricot( File.open("file.html") ) | |
(doc / "span.foo").each do |span| | |
span.swap("***#{span.inner_html}***") | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def add(num1) | |
lambda { |num2| num1 + num2 } | |
end | |
add(2).call(2) # returns 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
OlderNewer