Class: Rails3::Plugin::Extender::Matchers::BeExtendedWith

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

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)


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

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.



8
9
10
# File 'lib/r3_plugin_toolbox/rspec/matchers/be_extended_with.rb', line 8

def bad_const
  @bad_const
end

#causeObject (readonly)

Returns the value of attribute cause.



8
9
10
# File 'lib/r3_plugin_toolbox/rspec/matchers/be_extended_with.rb', line 8

def cause
  @cause
end

#methodsObject (readonly)

Returns the value of attribute methods.



8
9
10
# File 'lib/r3_plugin_toolbox/rspec/matchers/be_extended_with.rb', line 8

def methods
  @methods
end

#module_constObject (readonly)

Returns the value of attribute module_const.



8
9
10
# File 'lib/r3_plugin_toolbox/rspec/matchers/be_extended_with.rb', line 8

def module_const
  @module_const
end

#rails_constObject (readonly)

Returns the value of attribute rails_const.



8
9
10
# File 'lib/r3_plugin_toolbox/rspec/matchers/be_extended_with.rb', line 8

def rails_const
  @rails_const
end

#rails_const_nameObject (readonly)

Returns the value of attribute rails_const_name.



8
9
10
# File 'lib/r3_plugin_toolbox/rspec/matchers/be_extended_with.rb', line 8

def rails_const_name
  @rails_const_name
end

#submodulesObject (readonly)

Returns the value of attribute submodules.



8
9
10
# File 'lib/r3_plugin_toolbox/rspec/matchers/be_extended_with.rb', line 8

def submodules
  @submodules
end

Instance Method Details

#base_class_methodsObject



31
32
33
# File 'lib/r3_plugin_toolbox/rspec/matchers/be_extended_with.rb', line 31

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

#failure_messageObject



53
54
55
# File 'lib/r3_plugin_toolbox/rspec/matchers/be_extended_with.rb', line 53

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



49
50
51
# File 'lib/r3_plugin_toolbox/rspec/matchers/be_extended_with.rb', line 49

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

#match_submodules?Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
43
# File 'lib/r3_plugin_toolbox/rspec/matchers/be_extended_with.rb', line 35

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

#matches?(type) ⇒ Boolean

Returns:

  • (Boolean)


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

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 module #{rails_const_name} has not been registered"
    false
  end
end

#methods_included?(methods) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/r3_plugin_toolbox/rspec/matchers/be_extended_with.rb', line 45

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

#negative_failure_messageObject



57
58
59
# File 'lib/r3_plugin_toolbox/rspec/matchers/be_extended_with.rb', line 57

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