Skip to content

Instantly share code, notes, and snippets.

@mort
Created June 4, 2011 20:25
Show Gist options
  • Select an option

  • Save mort/1008318 to your computer and use it in GitHub Desktop.

Select an option

Save mort/1008318 to your computer and use it in GitHub Desktop.
Scissors
class Scissors
# power :cut, :objects => :cuttable
# These are the methods available for a thing with the power of cutting
def cut(thing, options = {}, restrictions = nil, &block)
return false unless may_cut?(thing, restrictions)
self.send(:before_cut) if self.respond_to?(:before_cut)
self.send(:do_cut, &block)
self.send(:after_cut) if self.respond_to?(:after_cut)
thing
end
def can_cut?
true
end
def may_cut?(thing, restrictions = nil)
if restrictions.is_a?(Hash)
collection = restrictions[:collection]
things = *restrictions[:things]
end
return false if things && !things.include?(thing.class.to_sym)
return false if collection && !collection.include?(thing)
return false unless thing.may_be_cut_by?(self)
return true if thing.can_be_cut?
end
def before_cut
'gonna cut'
end
def after_cut
'done cutting'
end
def do_cut(thing, options, &block)
[:before_being_cut, :when_cut, :after_being_cut].each do |callback|
thing.send(callback, self, options) if thing.respond_to?(callback)
end
thing.send(:before_being_cut) if thing.respond_to?(:before_being_cut)
yield(block)
puts 'cutting'
thing.send(:after_being_cut) if thing.respond_to?(:after_being_cut)
end
# quality :crushable
# These are the methods available for a thing with the quality 'crushable'
def can_be_crushed?
true
end
def crush_with(thing, options, restrictions)
return false unless self.may_be_crushed_by?(thing, restrictions)
thing.send(:crush, self, options)
end
def may_be_crushed_by?(thing, restrictions = nil)
if restrictions.is_a?(Hash)
collection = restrictions[:collection]
things = restrictions[:things]
end
return false if things && things.is_a?(Array) && !things.include?(thing.class.to_sym)
return false if collection && collection.is_a?(Array) && !collection.include?(thing)
return true if thing.can_crush?
end
def before_being_crushed
'oh noes'
end
def when_crushed
'crushed'
end
def after_being_crushed
'i feel flat'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment