Skip to content

Instantly share code, notes, and snippets.

@myronmarston
Created October 13, 2012 02:55
Show Gist options
  • Save myronmarston/3883036 to your computer and use it in GitHub Desktop.
Save myronmarston/3883036 to your computer and use it in GitHub Desktop.
Examples of code with comments for https://twitter.com/avdi/status/256820958676013056
# From: https://github.com/seomoz/interpol/blob/5108f29eadf52c4401785d86f4bc75f590813792/Rakefile#L42-51
desc "Import the twitter bootstrap assets from the compass_twitter_bootstrap gem"
task :import_bootstrap_assets do
# when the gem installed as a :git gem, it has "-" as a separator;
# when it's installed as a released rubygem, it has "_" as a separator.
gem_lib_path = $LOAD_PATH.grep(/compass[-_]twitter[-_]bootstrap/).first
assets = Dir[File.join(gem_lib_path, '..', 'vendor', 'assets', '**')]
destination_path = File.expand_path("../lib/interpol/documentation_app/public", __FILE__)
FileUtils.cp_r(assets, destination_path)
end
# From: https://github.com/rspec/rspec-mocks/blob/68f1d1c1bacad919a5528014da03f76b7165eabc/lib/rspec/mocks/stub_const.rb#L7-57
# We only want to consider constants that are defined directly on a
# particular module, and not include top-level/inherited constants.
# Unfortunately, the constant API changed between 1.8 and 1.9, so
# we need to conditionally define methods to ignore the top-level/inherited
# constants.
#
# Given:
# class A; B = 1; end
# class C < A; end
#
# On 1.8:
# - C.const_get("Hash") # => ::Hash
# - C.const_defined?("Hash") # => false
# - C.constants # => ["B"]
# - None of these methods accept the extra `inherit` argument
# On 1.9:
# - C.const_get("Hash") # => ::Hash
# - C.const_defined?("Hash") # => true
# - C.const_get("Hash", false) # => raises NameError
# - C.const_defined?("Hash", false) # => false
# - C.constants # => [:B]
# - C.constants(false) #=> []
if Module.method(:const_defined?).arity == 1
def const_defined_on?(mod, const_name)
mod.const_defined?(const_name)
end
def get_const_defined_on(mod, const_name)
if const_defined_on?(mod, const_name)
return mod.const_get(const_name)
end
raise NameError, "uninitialized constant #{mod.name}::#{const_name}"
end
def constants_defined_on(mod)
mod.constants.select { |c| const_defined_on?(mod, c) }
end
else
def const_defined_on?(mod, const_name)
mod.const_defined?(const_name, false)
end
def get_const_defined_on(mod, const_name)
mod.const_get(const_name, false)
end
def constants_defined_on(mod)
mod.constants(false)
end
end
# From: https://github.com/myronmarston/vcr/blob/v2.2.5/lib/vcr/util/hooks.rb#L48-60
module ClassMethods
def define_hook(hook_type, prepend = false)
placement_method = prepend ? :unshift : :<<
# Put the hook methods in a module so we can override and super to these methods.
self::DefinedHooks.module_eval do
define_method hook_type do |*filters, &hook|
hooks[hook_type].send(placement_method, FilteredHook.new(hook, filters))
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment