module RestCore::RailsUtilUtil

Public Class Methods

extend_rails_util(rails_util, name) click to toggle source
# File lib/rest-core/util/rails_util_util.rb, line 61
  def self.extend_rails_util rails_util, name
    meth = name.downcase
    mod  = if rails_util.const_defined?(:ClassMethod)
             rails_util.const_get(:ClassMethod)
           else
             Module.new
           end
    mod.module_eval(<<-RUBY, __FILE__, __LINE__)
    def init app=Rails
      RestCore::RailsUtilUtil.load_config(RestCore::#{name}, '#{meth}', app)
    end

    def included controller
      # skip if included already, any better way to detect this?
      return if controller.private_instance_methods.include?(:rc_#{meth})

      controller.send(:include, RestCore::RailsUtilUtil::InstanceMethod)

      controller.helper(RestCore::#{name}::RailsUtil::Helper)
      controller.instance_methods.select{ |method|
        method.to_s =~ /^rc_#{meth}/
      }.each{ |method| controller.send(:private, method) }
    end
    RUBY
    rails_util.send(:extend, mod)
    rails_util.const_set(:ClassMethod, mod)
  end
include_rails_util(rails_util, name) click to toggle source
# File lib/rest-core/util/rails_util_util.rb, line 89
  def self.include_rails_util rails_util, name
    meth = name.downcase
    mod  = if rails_util.const_defined?(:InstanceMethod)
             rails_util.const_get(:InstanceMethod)
           else
             Module.new
           end
    mod.module_eval(<<-RUBY, __FILE__, __LINE__)
    def rc_#{meth}
      client = RestCore::#{name}
      @rc_#{meth} ||= client.new(rc_options_new(client))
    end

    def rc_#{meth}_setup options={}
      client = RestCore::#{name}
      rc_setup(client, options)

      # we'll need to reinitialize rc_#{meth} with the new options,
      # otherwise if you're calling rc_#{meth} before rc_#{meth}_setup,
      # you'll end up with default options without the ones you've passed
      # into rc_#{meth}_setup.
      rc_#{meth}.send(:initialize, rc_options_new(client))

      true # keep going
    end
    RUBY
    rails_util.send(:include, mod)
    rails_util.const_set(:InstanceMethod, mod)
  end
included(rails_util, name=rails_util.name[/(\w+)::\w+$/, 1]) click to toggle source
# File lib/rest-core/util/rails_util_util.rb, line 55
def self.included rails_util, name=rails_util.name[/(\w+)::\w+$/, 1]
   extend_rails_util(rails_util, name)
  include_rails_util(rails_util, name)
        setup_helper(rails_util, name)
end
load_config(klass, namespace=nil, app=Rails) click to toggle source
# File lib/rest-core/util/rails_util_util.rb, line 4
def self.load_config klass, namespace=nil, app=Rails
  # make sure the default is there even if there's no config file
  RestCore::Config.default_attributes_module(klass)

  root = File.expand_path(app.root)
  path = ["#{root}/config/rest-core.yaml", # YAML should use .yaml
          "#{root}/config/rest-core.yml" ].find{|p| File.exist?(p)}
  return if path.nil?
  RestCore::Config.load(klass, path, app.env, namespace)
end
setup_helper(rails_util, name) click to toggle source
# File lib/rest-core/util/rails_util_util.rb, line 119
  def self.setup_helper rails_util, name
    meth = name.downcase
    mod  = if rails_util.const_defined?(:Helper)
             rails_util.const_get(:Helper)
           else
             Module.new
           end
    mod.module_eval(<<-RUBY, __FILE__, __LINE__)
    def rc_#{meth}
      controller.send(:rc_#{meth})
    end
    RUBY
    rails_util.const_set(:Helper, mod)
  end