Class: ProviderKit::Capability

Inherits:
Object
  • Object
show all
Defined in:
lib/provider_kit/capability.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, **options)
  @key          = key
  @target_klass = target_klass.to_s
  @options      = options || {}
  @provider     = provider
  @context      = options.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

#keyObject (readonly)

Returns the value of attribute key.



6
7
8
# File 'lib/provider_kit/capability.rb', line 6

def key
  @key
end

#providerObject (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

Returns:

  • (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

Returns:

  • (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),
    **options.merge(context:)
  )
end