Class: RSpec::RailsApp::Content::Matchers::HaveGem

Inherits:
Object
  • Object
show all
Extended by:
RailsAssist::UseMacro
Includes:
RailsAssist::File::Special
Defined in:
lib/rails_app_spec/matchers/special/have_gem.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ HaveGem

Returns a new instance of HaveGem.

Raises:

  • (ArgumentException)


16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rails_app_spec/matchers/special/have_gem.rb', line 16

def initialize *args
  raise ArgumentException, "First argument must name the gem" if !args.first.kind_of_label? 
  @name = args.delete_at(0)
  return if args.size == 0 || !args[1].kind_of_label? 

  @version = args.delete_at(1) 
  @options = {}

  return if args.empty?
  raise ArgumentException, "Last argument of gem statement must be a n options Hash" if !args.last.kind_of? Hash
  @options = args.last
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



14
15
16
# File 'lib/rails_app_spec/matchers/special/have_gem.rb', line 14

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



14
15
16
# File 'lib/rails_app_spec/matchers/special/have_gem.rb', line 14

def options
  @options
end

#versionObject (readonly)

Returns the value of attribute version.



14
15
16
# File 'lib/rails_app_spec/matchers/special/have_gem.rb', line 14

def version
  @version
end

Instance Method Details

#failure_messageObject



52
53
54
# File 'lib/rails_app_spec/matchers/special/have_gem.rb', line 52

def failure_message
  "Expected #{msg}" 
end

#matches?(root_path = nil) ⇒ Boolean

TODO: relative to root_path ?

Returns:

  • (Boolean)


30
31
32
33
34
# File 'lib/rails_app_spec/matchers/special/have_gem.rb', line 30

def matches?(root_path=nil)      
  content = read_gem_file
  return nil if content.empty?    
  (content =~ /gem\s+#{name_expr}#{version_expr}/)
end

#msgObject



44
45
46
# File 'lib/rails_app_spec/matchers/special/have_gem.rb', line 44

def msg
  "the Gemfile to have a gem statement: gem '#{name}'#{version_txt}'"
end

#name_exprObject



36
37
38
# File 'lib/rails_app_spec/matchers/special/have_gem.rb', line 36

def name_expr
  "('|\")" + name + '\1'
end

#negative_failure_messageObject



56
57
58
# File 'lib/rails_app_spec/matchers/special/have_gem.rb', line 56

def negative_failure_message
  "Did not expect #{msg}" 
end

#version_exprObject



40
41
42
# File 'lib/rails_app_spec/matchers/special/have_gem.rb', line 40

def version_expr
  '\s*,\s*' + "('|\")" + name + '\2' if version
end

#version_txtObject



48
49
50
# File 'lib/rails_app_spec/matchers/special/have_gem.rb', line 48

def version_txt
  version ? ", '#{version}'" : ""
end