Module: Contrast::Components::ComponentReceiverClassInterface

Defined in:
lib/contrast/components/interface.rb

Overview

Interface to allow for iteration over each of the configuration components

Constant Summary collapse

COMPONENT_MAP =

Components are manually required at the end of this file, and this constant is then frozen. RUBY-535 to handle this better.

{}

Instance Method Summary collapse

Instance Method Details

#access_component(*component_set_syms) ⇒ Object

.access_component

to be used as:

class Abc

include Contrast::Components::Interface
access_component :logging, :agent

def function
  if AGENT.disabled?
    0 / 3
  end
rescue
  logger.error "this function did error"
end

end

‘:logger` creates a #logger and .logger method `:agent` provides an AGENT constant, analogous to a local singleton.



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/contrast/components/interface.rb', line 128

def access_component *component_set_syms
  @_access_component ||= {}

  component_set_syms.each do |sym|
    next if @_access_component[sym]

    if (mods = component_map[sym]) # rubocop:disable Style/GuardClause
      # We may support multiple components via one access request.
      mods.each do |m|
        name = Contrast::Components.component_const_name(m.name)
        cs__const_set(name, m::COMPONENT_INTERFACE) if m.cs__const_defined?(:COMPONENT_INTERFACE)
        include m::InstanceMethods               if m.cs__const_defined?(:InstanceMethods, false)
        extend  m::ClassMethods                  if m.cs__const_defined?(:ClassMethods, false)
      end

      @_access_component[sym] = true
    else
      raise NoMethodError, "#{ self } asked to access undefined component '#{ sym }'."
    end
  end
end

#component_mapObject

TODO: RUBY-535 This module is used via ‘extend`, so it can’t access constants we define here.



104
105
106
# File 'lib/contrast/components/interface.rb', line 104

def component_map
  COMPONENT_MAP
end