Class: BaseFinder

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

Direct Known Subclasses

RailsFinder, ScriptFinder

Constant Summary collapse

DEFAULT_BIN_DIR =
'script'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command, bin_dir = nil) ⇒ BaseFinder

Returns a new instance of BaseFinder.



38
39
40
41
# File 'lib/script_finder/base_finder.rb', line 38

def initialize(command, bin_dir = nil)
  @command = command
  self.bin_dir = bin_dir || DEFAULT_BIN_DIR
end

Instance Attribute Details

#bin_dirObject

Returns the value of attribute bin_dir.



10
11
12
# File 'lib/script_finder/base_finder.rb', line 10

def bin_dir
  @bin_dir
end

#commandObject (readonly)

Returns the value of attribute command.



11
12
13
# File 'lib/script_finder/base_finder.rb', line 11

def command
  @command
end

Class Method Details

.find_and_execute(command, opts = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/script_finder/base_finder.rb', line 15

def self.find_and_execute(command, opts = {})
  bin_dir = opts[:bin_dir] ||= nil
  r_or_s = opts[:prefix]
  command = command.split(' ') if command.is_a?(String)
  unless running_command_for_current_rails_version?(r_or_s)
    puts "You are attempting to run a command for a version of rails you are not currently running. Please verify rails version" 
    puts "Current rails version is #{`rails -v`}"
    exit
  end
  finder = new(command, bin_dir).execute_command
end

.running_command_for_current_rails_version?(command) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
35
36
# File 'lib/script_finder/base_finder.rb', line 27

def self.running_command_for_current_rails_version?(command)
  if command =~ /r/i or command =~ /s/i
    right_version = command =~ /r/i ? Regexp.new(/rails\s(3\.)/i) : Regexp.new(/rails\s(2\.)/i)

    return !right_version.match(`rails -v`).nil?
  end

  #unknown command. Assuming that this is the right call and they know what they are doing
  return true
end

Instance Method Details

#execute_commandObject

Raises:

  • (NotImplementedError)


43
44
45
# File 'lib/script_finder/base_finder.rb', line 43

def execute_command
  raise NotImplementedError.new("You must implement execute_command for subclasses of BaseFinder!")
end