Module: Waylon::BaseComponent::ClassUtilityMethods
- Defined in:
- lib/waylon/base_component.rb
Overview
Base Component utility methods
Instance Method Summary collapse
-
#cache(key, expires: 600) ⇒ Object
Allows caching from class methods.
-
#component_namespace(value = nil) ⇒ String
The namespace used for this component’s storage.
-
#config(key, default: nil, required: false, type: String) ⇒ Object
Creates namespaced configuration keys for BaseComponent subclasses.
-
#config_key_for(key) ⇒ String
Provides the full Config key given a relative key.
-
#configured? ⇒ Boolean
Determines if the current component is fully configured.
-
#features(list) ⇒ Object
Describes features supported by a Sense.
-
#supports?(key) ⇒ Boolean
Determine if a BaseComponent subclass supports a feature.
Instance Method Details
#cache(key, expires: 600) ⇒ Object
Allows caching from class methods
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
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
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
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
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
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
91 92 93 94 |
# File 'lib/waylon/base_component.rb', line 91 def supports?(key) @features ||= [] @features.include?(key.to_sym) end |