Module: Waylon::BaseComponent::ClassUtilityMethods

Defined in:
lib/waylon/base_component.rb

Overview

Base Component utility methods

Instance Method Summary collapse

Instance Method Details

#cache(key, expires: 600) ⇒ Object

Allows caching from class methods

Parameters:

  • key (String)

    How to store/retrieved the cached value

  • expires (Integer) (defaults to: 600)

    How long to cache the value



29
30
31
32
33
34
35
36
37
38
# File 'lib/waylon/base_component.rb', line 29

def cache(key, expires: 600)
  cache_key = config_key_for(key)
  if !Waylon::Cache.key?(cache_key) && block_given?
    result = yield
    Waylon::Cache.store(cache_key, result, expires:)
  elsif !Waylon::Cache.key?(cache_key)
    return nil
  end
  Waylon::Cache.load(cache_key, expires:)
end

#component_namespace(value = nil) ⇒ String

The namespace used for this component’s storage

Parameters:

  • value (String, nil) (defaults to: nil)

    Sets this namespace unless set (without setting, returns a sane default)

Returns:

  • (String)

    The namespace for this component



43
44
45
46
47
# File 'lib/waylon/base_component.rb', line 43

def component_namespace(value = nil)
  @namespace ||= value
  # Either returns the namespace or the stripped down class name
  @namespace || name.to_s.split("::").last.downcase
end

#config(key, default: nil, required: false, type: String) ⇒ Object

Creates namespaced configuration keys for BaseComponent subclasses

Parameters:

  • key (String)

    The config key to define a schema for

  • required (Boolean) (defaults to: false)

    Is this key required on startup?

  • type (Class) (defaults to: String)

    The class type for the stored value

  • default (Object) (defaults to: nil)

    The optional default value



51
52
53
54
55
# File 'lib/waylon/base_component.rb', line 51

def config(key, default: nil, required: false, type: String)
  conf = Config.instance
  config_key = config_key_for(key)
  conf.add_schema(config_key, default:, required:, type:)
end

#config_key_for(key) ⇒ String

Provides the full Config key given a relative key

Parameters:

  • key (String)

    The relative key to fully-qualify

Returns:

  • (String)

    The fully-qualified Config key



60
61
62
# File 'lib/waylon/base_component.rb', line 60

def config_key_for(key)
  "#{config_namespace}.#{key}"
end

#configured?Boolean

Determines if the current component is fully configured

Returns:

  • (Boolean)


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/waylon/base_component.rb', line 66

def configured?
  conf = Config.instance
  req_confs = conf.schema.select do |k, v|
    k.match?(/^#{config_namespace}\./) && v[:required]
  end
  missing_configs = req_confs.reject { |k, _v| conf.set?(k) }
  if missing_configs.empty? && conf.valid?
    true
  elsif missing_configs.empty?
    log("Configuration for #{component_namespace} failed validation!", :error)
    false
  else
    missing_configs.each { |k, _v| log("Missing required configuration: #{k}", :error) }
    false
  end
end

#features(list) ⇒ Object

Describes features supported by a Sense

Parameters:

  • list (String, Array<String,Symbol>)

    List of features supported by this BaseComponent



85
86
87
# File 'lib/waylon/base_component.rb', line 85

def features(list)
  @features = [*list].map(&:to_sym)
end

#supports?(key) ⇒ Boolean

Determine if a BaseComponent subclass supports a feature

Parameters:

  • key (String, Symbol)

    The feature in question

Returns:

  • (Boolean)


91
92
93
94
# File 'lib/waylon/base_component.rb', line 91

def supports?(key)
  @features ||= []
  @features.include?(key.to_sym)
end