Module: IncludeWithRespect

Defined in:
lib/include_with_respect.rb,
lib/include_with_respect/version.rb,
lib/include_with_respect/configuration.rb,
lib/include_with_respect/module_with_respect.rb,
lib/include_with_respect/concern_with_respect.rb

Overview

This is an ActiveSupport::Concern that will make other concerns “include with respect” Usage:

module MyConcern
  extend ActiveSupport::Concern

  included do
    include IncludeWithRespect::ConcernWithRespect
    include SomeOtherConcern
  end
end

Defined Under Namespace

Modules: ConcernWithRespect, ModuleWithRespect Classes: Configuration, Error

Constant Summary collapse

VERSION =
'1.0.1'.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.include_with_respect_configurationObject

Returns the value of attribute include_with_respect_configuration.



54
55
56
# File 'lib/include_with_respect.rb', line 54

def include_with_respect_configuration
  @include_with_respect_configuration
end

Class Method Details

.configurationObject

For single statement global config in an initializer e.g. IncludeWithRespect.configuration.level = :error



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

def self.configuration
  self.include_with_respect_configuration ||= Configuration.new
end

.configure {|configuration| ... } ⇒ Object

For global config in an initializer with a block

Yields:



48
49
50
# File 'lib/include_with_respect.rb', line 48

def self.configure
  yield(configuration)
end

.extend_with_respect(receiver, module1, *smth) ⇒ Object



33
34
35
36
37
38
# File 'lib/include_with_respect.rb', line 33

def extend_with_respect(receiver, module1, *smth)
  @skip_include = false
  have_respect('extended', receiver, module1, skip, *smth) if receiver.singleton_class.include?(module1)

  receiver.send(:extend_without_respect, module1, *smth) unless @skip_include
end

.have_respect(action, receiver, module1, *smth) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/include_with_respect.rb', line 59

def have_respect(action, receiver, module1, *smth)
  message = "#{module1}#{smth.any? ? " with #{smth}" : ''} already #{action} in #{receiver}"
  puts "message: #{message}" if configuration.level == [:error]
  configuration.level.each do |level|
    case level
    when :error
      raise ::IncludeWithRespect::Error, message
    when :warning
      puts message
    when :skip
      @skip_include = true
    else # :silent
      # NOOP
    end
  end
end

.include_with_respect(receiver, module1, *smth) ⇒ Object



25
26
27
28
29
30
# File 'lib/include_with_respect.rb', line 25

def include_with_respect(receiver, module1, *smth)
  @skip_include = false
  have_respect('included', receiver, module1, *smth) if receiver.include?(module1)

  receiver.send(:include_without_respect, module1, *smth) unless @skip_include
end