class OopRailsServer::Gemfile
This is not intended to be anything even close to a real Gemfile
parser – it is just enough to create/parse the simple Gemfiles we create or that are generated by 'rails new'.
Constants
- GEM_LINE_REGEXP
Attributes
file_path[R]
Public Class Methods
new(file_path)
click to toggle source
# File lib/oop_rails_server/gemfile.rb, line 5 def initialize(file_path) @file_path = File.expand_path(file_path) end
Public Instance Methods
add_version_constraints!(gem_name, *version_specs)
click to toggle source
# File lib/oop_rails_server/gemfile.rb, line 9 def add_version_constraints!(gem_name, *version_specs) additional_version_constraints = version_specs.map { |vs| ", '#{vs}'" }.join update_gemfile_line!(gem_name) do |gemdecl, specs| "#{gemdecl}#{additional_version_constraints}#{specs}" end end
ensure_gem!(gem_name)
click to toggle source
# File lib/oop_rails_server/gemfile.rb, line 30 def ensure_gem!(gem_name) update_gemfile_line!(gem_name) do |gemdecl, specs| "#{gemdecl}#{specs}" end end
set_specs!(gem_name, new_specs)
click to toggle source
# File lib/oop_rails_server/gemfile.rb, line 17 def set_specs!(gem_name, new_specs) new_specs_line = new_specs.inspect if new_specs_line =~ /^\s*\{\s*(.*?)\s*\}\s*$/ new_specs_line = $1 else raise "Object doesn't seem to #inspect into a Hash string: #{new_specs.inspect}" end update_gemfile_line!(gem_name) do |gemdecl, specs| "#{gemdecl}, #{new_specs_line}" end end
write!()
click to toggle source
# File lib/oop_rails_server/gemfile.rb, line 36 def write! File.open(file_path, "w") do |f| contents.each { |l| f.puts l } end end
Private Instance Methods
contents()
click to toggle source
# File lib/oop_rails_server/gemfile.rb, line 73 def contents @contents ||= begin c = if File.exist?(file_path) File.read(file_path) else empty_gemfile_contents end c.split(/\n/) end end
empty_gemfile_contents()
click to toggle source
# File lib/oop_rails_server/gemfile.rb, line 85 def empty_gemfile_contents "source 'https://rubygems.org'\n\n" end
update_gemfile_line!(gem_name) { |gemdecl, matchdata| ... }
click to toggle source
# File lib/oop_rails_server/gemfile.rb, line 47 def update_gemfile_line!(gem_name) found = nil contents.each_with_index do |line, index| if line =~ GEM_LINE_REGEXP && $2.strip.downcase == gem_name.strip.downcase if found raise "Multiple lines in '#{file_path}' seem to specify gem '#{gem_name}':\n #{found[0]}: #{found[1]}\nand #{index}: #{line}" end found = [ index, line ] end end unless found new_line = "gem '#{gem_name}'" contents << new_line found = [ (contents.length - 1), new_line ] end matchdata = GEM_LINE_REGEXP.match(found[1]) gemdecl = [ matchdata[1], matchdata[2], matchdata[3] ].join modified_line = yield gemdecl, matchdata[4] contents[found[0]] = modified_line end