Class: ProjectScout::Dir

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

Overview

This is a helper class with a bit of syntactical magic. Want to know if /home/user/bubbles is a Rails project?

ProjectScout::Dir("/home/user/bubbles").ruby_rails_project?

Want to know if it’s any sort of Ruby project?

ProjectScout::Dir("/home/user/bubbles").ruby_project?

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Dir

Returns a new instance of Dir.



15
16
17
# File 'lib/project_scout/dir.rb', line 15

def initialize(path)
  self.path = path
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

Explanation of magic:

1) if a method is invoked with a “_project?” suffix, strip “_project”

and call with the same arguments. Thus calling foo_bar_project?
invokes foo_bar?

2) if a method invoked has no underscores in it, and local methods

exist that start with the same string, invoke all of them and
return true if and return true. Thus calling foo_project? when
foo_bar_project? and foo_baz_project? exist will return true
only if any of foo_bar_project? and foo_baz_project? return true.


51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/project_scout/dir.rb', line 51

def method_missing(method, *args)
  method = method.to_s
  if method.end_with? "_project?"
    method.sub! "_project", ""
    self.send method.to_sym, *args
  elsif !method.include?("_") && local_methods.find { |m| m.to_s.start_with? "#{method.chop}_" }
    project_methods = local_methods.find_all { |m| m.to_s.start_with? "#{method.chop}_" }
    project_methods.collect { |m| self.send m.to_s }.any?
  else
    raise NameError.new("undefined local variable or method '#{method}' for ProjectScout::Dir")
  end
end

Instance Attribute Details

#pathObject

Returns the value of attribute path.



13
14
15
# File 'lib/project_scout/dir.rb', line 13

def path
  @path
end

Instance Method Details

#contains?(path_suffix) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/project_scout/dir.rb', line 64

def contains?(path_suffix)
  File.exists?(File.join(path, path_suffix))
end

#git_repository?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/project_scout/dir.rb', line 19

def git_repository?
  contains? ".git"
end

#local_methodsObject



35
36
37
# File 'lib/project_scout/dir.rb', line 35

def local_methods
  self.methods - self.class.methods
end

#ruby_cucumber?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/project_scout/dir.rb', line 23

def ruby_cucumber?
  contains? "features/env.rb"
end

#ruby_rails?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/project_scout/dir.rb', line 27

def ruby_rails?
  contains? "config/environment.rb"
end

#ruby_rspec?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/project_scout/dir.rb', line 31

def ruby_rspec?
  contains? "spec/spec_helper.rb"
end