Module: RestCore::RailsUtilUtil

Included in:
Facebook::RailsUtil, Github::RailsUtil, Linkedin::RailsUtil, Twitter::RailsUtil
Defined in:
lib/rest-core/util/rails_util_util.rb

Defined Under Namespace

Modules: Cache, InstanceMethod

Class Method Summary collapse

Class Method Details

.extend_rails_util(rails_util, name) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# 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) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# 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 = ) ⇒ Object



55
56
57
58
59
# 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) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# 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) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
# 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