Module: Motr::Modding

Included in:
Motr::Mods::Sluggable
Defined in:
lib/motr/modding.rb

Instance Method Summary collapse

Instance Method Details

#append_features(base) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/motr/modding.rb', line 9

def append_features(base)
  return false if base < self
  super
  # Apply any orm specific configurations
  #
  if instance_variable_defined?("@_mod_config_for_orm")
    @_mod_config_for_orm.each do |orm, configs|            
      # Each orm should implement a motr_orm_type value which identifies
      # itself to mods (ie :active_record, :mongoid)
      next unless base.respond_to?(:motr_orm_type) && base.__send__(:motr_orm_type).to_s === orm.to_s
      [configs].flatten.each{ |conf| base.class_eval(&conf) }
    end          
  end
  
  base.extend const_get("ClassMethods") if const_defined?("ClassMethods")
  base.send :include, const_get("InstanceMethods") if const_defined?("InstanceMethods")
  base.class_eval(&@_mod_config_callback) if instance_variable_defined?("@_mod_config_callback")
  
end

#apply(&block) ⇒ Object

Apply is run each time a mod is applied to a model. As opposed to the configure method, apply blocks are run each time modded_with :your_mod is called.

Examples:

Configure a before_save callback to be applied to each instance


module MyMod
  apply do
    before_save :something_each_time
  end
end

Parameters:

  • &block (Block)

    The callback to be run when the mod is applied



43
44
45
# File 'lib/motr/modding.rb', line 43

def apply(&block)
  @_mod_apply_callback = block
end

#configure(orm = nil, &block) ⇒ Object

Each mod can pass an optional configuration block (or one global config and one per orm) which is run when a particular class or model implements that mod. Configure methods are only run on first inclusion. To run functionality each time the mod is applied, see Motr::Mod.apply

Orm specific functionality can be configured by passing an :orm as the first option

Examples:

Configure a before_filter callback when a model implements your mod


module MyMod
  configure do
    before_filter :something_awesome
  end
end

Configure mongoid only functionality


module MyMod
  configure(:mongoid) do
    embeds_many :somethings
  end
end

Parameters:

  • orm (Symbol) (defaults to: nil)

    The name of the orm in which this configuration applies



73
74
75
76
77
78
79
# File 'lib/motr/modding.rb', line 73

def configure(orm = nil, &block)
  if orm.nil?
    @_mod_config_callback = block
  else
    (@_mod_config_for_orm ||= {})[orm] = block        
  end
end

#options(*options) ⇒ Object

Each mod can specify any number of options that may be passed to it when applied to a class/model.

Examples:

Create configuration options on a module

module Motr::Mods::Sample
  options(:sample, :option1, :option2)
end

Parameters:

  • *options (Array)

    Argument list of options to create.



93
94
95
# File 'lib/motr/modding.rb', line 93

def options(*options)
  @_mod_options = options
end