Module: BasicAssumption

Defined in:
lib/basic_assumption.rb,
lib/basic_assumption/rails.rb,
lib/basic_assumption/version.rb,
lib/basic_assumption/configuration.rb,
lib/basic_assumption/default_assumption.rb,
lib/basic_assumption/default_assumption/base.rb,
lib/basic_assumption/default_assumption/rails.rb,
lib/basic_assumption/configuration/active_record.rb,
lib/basic_assumption/default_assumption/restful_rails.rb,
lib/basic_assumption/default_assumption/class_resolver.rb

Overview

BasicAssumption

A module that allows for a declaritive idiom that maps a name to behavior inside your application. It uses memoization, blocks, and the ability to set default behavior to clean up certain kinds of code, particularly Rails’ controllers and views.

Defined Under Namespace

Modules: DefaultAssumption, Version Classes: Configuration, Railtie

Instance Method Summary collapse

Instance Method Details

#after_assumption(name) ⇒ Object

Callback that is invoked once assume has created a new method. When BasicAssumption is used in the context of ActionController, for example, this callback is used to prevent the new method from being a visible action and to make it available in views. (See the documentation for Railtie.)

class ActionController::Base
  extend BasicAssumption
  def after_assumption(name)
    helper_method(name)
    hide_action(name)
  end
end


98
# File 'lib/basic_assumption.rb', line 98

def after_assumption(name); end

#assume(name, context = {}, &block) ⇒ Object

Declares a resource at the instance level by creating an instance method called name that returns the result of block if it is given or the default if it is not.

Example

class Foo
  extend BasicAssumption

  assume(:cute) { 'Fuzzy kittens.' }
  assume 'assumption'
end

foo = Foo.new
foo.cute         #=> 'Fuzzy kittens.'
foo.assumption   #=> nil

The first call to assume creates an instance method cute that returns the result of evaluating the block passed to it. The second call creates a method assumption that returns the default result, which will be nil unless the default has been overridden. See default_assumption for details on overriding the default behavior.

In both cases, the result is memoized and returned directly for subsequent calls.

assume will also create an attribute writer method that will allow the value returned by the instance method (the reader, from this point of view) to be overriden.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/basic_assumption.rb', line 64

def assume(name, context={}, &block)
  define_method(name) do
    @basic_assumptions       ||= {}
    unless @basic_assumptions.key?(name)
      @basic_assumptions[name] = if block_given?
        instance_eval(&block)
      else
        which = context.delete(:using) || self.class
        block = DefaultAssumption.resolve(which)
        instance_exec(name, context, &block)
      end
    end
    @basic_assumptions[name]
  end
  define_method("#{name}=") do |value|
    @basic_assumptions       ||= {}
    @basic_assumptions[name] = value
  end
  after_assumption(name)
end

#default_assumption(name = nil, &block) ⇒ Object

Changes the default behavior for methods created by assume in the current class and its descendants. If a block is given, that block will be used as the new default. Otherwise, if name is provided, BasicAssumption will assume it refers to a snake-cased class name that will provide the default behavior. For details on custom defaults, please see the documentation for DefaultAssumption.

Example

class WidgetController
  extend BasicAssumption

  default_assumption do
    Widget.find_by_id(params[:widget_id])
    log_request(current_user, params[:widget_id])
  end

  assume(:widget)
end


30
31
32
33
# File 'lib/basic_assumption.rb', line 30

def default_assumption(name=nil, &block)
  default = block_given? ? block : name
  DefaultAssumption.register(self, default)
end