module TrustyCms::ResourceResponses::InstanceMethods

def response_for(action)

def response_for(action)
  responses = self.class.responses.send(action)
  respond_to do |wants|
    responses.each_format do |f, format_block|
      if format_block
        wants.send(f, &wrap(format_block))
      else
        wants.send(f)
      end
    end
    responses.each_published do |pub, pub_block|
      wants.send(pub, &wrap(pub_block))
    end
    if responses.default
      wants.any(&wrap(responses.default))
    else
      wants.any
    end
  end
end

def wrap(proc)

def wrap(proc)
  # Makes sure our response blocks get evaluated in the right context
  lambda do
    # Ruby 1.9.2 yields self in instance_eval... see https://gist.github.com/479572
    # lambdas are as strict as methods in 1.9.x, making sure that the args match, Procs are not.
    if RUBY_VERSION =~ /^1\.9/ && proc.lambda? && (proc.arity != 1)
      raise "You can only pass a proc ('Proc.new') or a lambda that takes exactly one arg (for self) to the wrap method."
    end
    instance_eval(&proc)
  end
end