Class: Rails3::PluginExtender::Matchers::BeExtendedWith

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/plugin_toolbox/rspec/matchers/be_extended_with.rb

Constant Summary

Constants included from Util

Util::ACTIVE_MODULES, Util::INIT

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

#act_type?, #get_base_class, #get_constant, #get_load_type, #get_module, #make_constant, #rails_const_base

Constructor Details

#initialize(module_const, submodules = nil) ⇒ BeExtendedWith

Returns a new instance of BeExtendedWith.

Raises:

  • (ArgumentError)


9
10
11
12
13
# File 'lib/plugin_toolbox/rspec/matchers/be_extended_with.rb', line 9

def initialize(module_const, submodules=nil)
  @module_const = module_const     
  raise ArgumentError, "List of submodules must be given as a collection of Symbols or Strings" if submodules && !submodules.respond_to?(:flatten)
  @submodules = submodules.flatten if submodules
end

Instance Attribute Details

#bad_constObject (readonly)

Returns the value of attribute bad_const.



7
8
9
# File 'lib/plugin_toolbox/rspec/matchers/be_extended_with.rb', line 7

def bad_const
  @bad_const
end

#causeObject (readonly)

Returns the value of attribute cause.



7
8
9
# File 'lib/plugin_toolbox/rspec/matchers/be_extended_with.rb', line 7

def cause
  @cause
end

#methodsObject (readonly)

Returns the value of attribute methods.



7
8
9
# File 'lib/plugin_toolbox/rspec/matchers/be_extended_with.rb', line 7

def methods
  @methods
end

#module_constObject (readonly)

Returns the value of attribute module_const.



7
8
9
# File 'lib/plugin_toolbox/rspec/matchers/be_extended_with.rb', line 7

def module_const
  @module_const
end

#rails_constObject (readonly)

Returns the value of attribute rails_const.



7
8
9
# File 'lib/plugin_toolbox/rspec/matchers/be_extended_with.rb', line 7

def rails_const
  @rails_const
end

#rails_const_nameObject (readonly)

Returns the value of attribute rails_const_name.



7
8
9
# File 'lib/plugin_toolbox/rspec/matchers/be_extended_with.rb', line 7

def rails_const_name
  @rails_const_name
end

#submodulesObject (readonly)

Returns the value of attribute submodules.



7
8
9
# File 'lib/plugin_toolbox/rspec/matchers/be_extended_with.rb', line 7

def submodules
  @submodules
end

Instance Method Details

#base_class_methodsObject



30
31
32
# File 'lib/plugin_toolbox/rspec/matchers/be_extended_with.rb', line 30

def base_class_methods 
  (rails_const == I18n) ? rails_const.methods : rails_const.instance_methods
end

#failure_messageObject



50
51
52
# File 'lib/plugin_toolbox/rspec/matchers/be_extended_with.rb', line 50

def failure_message   
  "Expected the rails class #{rails_const_name} to be extended with the methods in #{bad_const}#{cause}"
end

#get_methods(name) ⇒ Object



46
47
48
# File 'lib/plugin_toolbox/rspec/matchers/be_extended_with.rb', line 46

def get_methods name
  get_constant(module_const, name).instance_methods
end

#match_submodules?Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
# File 'lib/plugin_toolbox/rspec/matchers/be_extended_with.rb', line 34

def match_submodules? 
  submodules.each do |name|
    @bad_const = make_constant(module_const, name)
    return false if !methods_included? get_methods(name)                          
  end
  true
end

#matches?(type) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/plugin_toolbox/rspec/matchers/be_extended_with.rb', line 15

def matches? type
  begin  
    @rails_const_name = get_base_class(type)
    @bad_const = module_const
    @rails_const = rails_const_name.constantize            
    
    return match_submodules? if submodules            

    methods_included? module_const.instance_methods 
  rescue
    @cause = ", but the extension module wasn't found"
    false
  end
end

#methods_included?(methods) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/plugin_toolbox/rspec/matchers/be_extended_with.rb', line 42

def methods_included? methods
  (base_class_methods & methods) == methods
end

#negative_failure_messageObject



54
55
56
# File 'lib/plugin_toolbox/rspec/matchers/be_extended_with.rb', line 54

def negative_failure_message  
  "Did not expect the rails class #{rails_const_name} to be extended with the methods in #{bad_const}"
end