Class: ProviderKit::Capability
- Inherits:
-
Object
- Object
- ProviderKit::Capability
- Defined in:
- lib/provider_kit/capability.rb
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#provider ⇒ Object
readonly
Returns the value of attribute provider.
Instance Method Summary collapse
- #callable?(method_name) ⇒ Boolean
-
#initialize(key, target_klass, provider: nil, **options) ⇒ Capability
constructor
A new instance of Capability.
- #method_missing(method_name, *args, **kwargs) ⇒ Object
- #respond_to_missing?(method_name, include_private = false) ⇒ Boolean
- #with_context(provider: @provider, **context) ⇒ Object
Constructor Details
#initialize(key, target_klass, provider: nil, **options) ⇒ Capability
Returns a new instance of Capability.
8 9 10 11 12 13 14 |
# File 'lib/provider_kit/capability.rb', line 8 def initialize(key, target_klass, provider: nil, **) @key = key @target_klass = target_klass.to_s @options = || {} @provider = provider @context = .delete(:context) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, **kwargs) ⇒ Object
20 21 22 23 24 25 26 27 |
# File 'lib/provider_kit/capability.rb', line 20 def method_missing(method_name, *args, **kwargs) case method_name.to_s when /(.+)\?$/ call($1, *args, **kwargs) == true else call(method_name, *args, **kwargs) end end |
Instance Attribute Details
#key ⇒ Object (readonly)
Returns the value of attribute key.
6 7 8 |
# File 'lib/provider_kit/capability.rb', line 6 def key @key end |
#provider ⇒ Object (readonly)
Returns the value of attribute provider.
6 7 8 |
# File 'lib/provider_kit/capability.rb', line 6 def provider @provider end |
Instance Method Details
#callable?(method_name) ⇒ Boolean
16 17 18 |
# File 'lib/provider_kit/capability.rb', line 16 def callable?(method_name) callable_methods.include?(method_name.to_s) end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
29 30 31 32 33 34 35 36 |
# File 'lib/provider_kit/capability.rb', line 29 def respond_to_missing?(method_name, include_private = false) if method_name.to_s.match?(/(.+)\?$/) attribute_name = method_name.to_s.chomp("?") return callable?(attribute_name) end callable?(method_name) || super end |
#with_context(provider: @provider, **context) ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/provider_kit/capability.rb', line 38 def with_context(provider: @provider, **context) Capability.new( key, @target_klass, provider: provider.with_context(**context), **.merge(context:) ) end |