module Characterize::ControllerMacros

Private Instance Methods

characterize(*options) click to toggle source
# File lib/characterize/controller.rb, line 27
def characterize(*options)
  object_name = options.shift
  actions_hash = options.last

  object_constant_name = object_name.to_s.gsub(/(?:^|_)([a-z])/){ $1.upcase }.gsub('/','::')
  default_features = actions_hash.delete(:default) || ["::#{object_constant_name}#{Characterize.module_suffix}"]

  mod = Module.new
  mod.module_eval %{
    def #{object_name}
      return @#{object_name} if defined?(@#{object_name})
      @#{object_name} = characterize(load_#{object_name}, *characters_for_action(:#{object_name}, action_name))
    end

    def load_#{object_name}
      #{object_constant_name}.find(params[:id])
    end

    def default_#{object_name}_features
      [#{default_features.map(&:to_s).join(', ')}]
    end
  }
  actions_hash.each_pair do |action_name, characters|
    mod.module_eval %{
      def #{object_name}_#{action_name}_features
        [#{characters.map(&:to_s).join(', ')}]
      end
    }
  end
  self.const_set(object_constant_name + 'ControllerMethods', mod)
  include mod
  self.send(:helper_method, object_name)
end