Class: RailsGemChooser

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_gem_chooser.rb

Overview

Rails gem chooser.

Selects RAILS_GEM_VERSION from ENV, or from RAILS_ROOT/config/environment.rb. Returns nil otherwise.

Class Method Summary collapse

Class Method Details

.__load(rails_gem_version = nil, config_file = nil) ⇒ Object

Either define rails_gem_version or config_file



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
88
89
90
91
# File 'lib/rails_gem_chooser.rb', line 61

def __load(rails_gem_version=nil,config_file=nil)
  if (config_file and rails_gem_version) and (version != rails_gem_version)
    raise 'oops: Rails version mismatch'
  end

  rails_gem_version ||= version(config_file) # also detects ENV['RAILS_GEM_VERSION']

  #STDOUT.puts 'Loading Rails version %s' % rails_gem_version
  # the gem without underline will be removed in Rails3..
  #rails_gems = %w{ active_support action_pack active_record }
  # except that with the underline divider the gem is not found ..
  #rails_gems = %w{ activesupport actionpack activerecord }
  
  rails_gems = {
    # require name      gem name
    "active_support" => "activesupport",
    "action_pack"    => "actionpack",
    "active_record"  => "activerecord"
  }

  rails_gems.keys.each do |rg_key|
    __load_gem(rg_key, rails_gems[rg_key], rails_gem_version)
  end
  require 'action_controller' # action_controller lives in action_pack

  #STDOUT.puts "Loaded gems:"
  #STDOUT.puts "ActiveRecord: %s" % ActiveRecord::VERSION::STRING
  #STDOUT.puts "ActionPack: %s" %  ActionPack::VERSION::STRING
  #require 'active_support/version'
  #STDOUT.puts "ActiveSupport: %s" %  ActiveSupport::VERSION::STRING
end

.__load_gem(require_name, gem_name, version) ⇒ Object

Load a specific GEM



50
51
52
53
54
55
56
57
58
# File 'lib/rails_gem_chooser.rb', line 50

def __load_gem(require_name, gem_name, version)
  #STDOUT.puts 'Loading gem: %s v %s' % [require_name, version]
  version ? gem(gem_name, '= '+version) : gem(gem_name)
  begin
    require require_name
  rescue LoadError
    require gem_name
  end
end

.version(config_file = nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rails_gem_chooser.rb', line 18

def version(config_file=nil)
  # detect from ENV
  if ENV['RAILS_GEM_VERSION']
    return ENV['RAILS_GEM_VERSION']
  elsif not config_file
    # load Rails config file
    if RAILS_ROOT
        config_file = File.join(
        File.expand_path(RAILS_ROOT),
          'config',
          'environment.rb'
          )
    else
      STDERR.puts 'Could not detect Rails version'
      return nil
    end
  end
  # don't attempt to load Rails if building a Rubygem..!
  if $0[/gem$/] or !File.exist?(config_file)
    return nil
  else
    # read from Rails config file
    f=File.open(config_file)
    config = f.read
    f.close
    rails_gem_version = config[/^RAILS_GEM_VERSION.*(\d\.\d\.\d+)/,1]
    #STDOUT.puts 'Detected Rails version %s from the config file %s' % [rails_gem_version,config_file]
    return rails_gem_version
  end
end