Class: ProviderKit::ProviderAttribute
- Inherits:
-
Object
- Object
- ProviderKit::ProviderAttribute
show all
- Includes:
- Buildable
- Defined in:
- lib/provider_kit/provider_attribute.rb
Overview
serializer for a provider attribute turned into a Provider instance
Constant Summary
collapse
- ATTRIBUTE_QUERY =
/^(?<attribute>[a-z0-9_]+)\?$/.freeze
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Buildable
#with
Constructor Details
#initialize(key, record: nil, **context) ⇒ ProviderAttribute
Returns a new instance of ProviderAttribute.
15
16
17
18
19
|
# File 'lib/provider_kit/provider_attribute.rb', line 15
def initialize(key, record: nil, **context)
@key = (key.to_s.presence || "default").to_sym
@record = record
@context = context.reverse_merge(default_context)
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/provider_kit/provider_attribute.rb', line 29
def method_missing(method_name, *args)
return super unless provider.present?
if provider.respond_to?(method_name)
return provider.public_send(method_name, *args)
end
if match = method_name.to_s.match(ATTRIBUTE_QUERY)
return match[:attribute].to_s.strip.downcase.to_sym == key
end
provider.with_context(**context).public_send(method_name, *args)
end
|
Instance Attribute Details
#context ⇒ Object
Returns the value of attribute context.
13
14
15
|
# File 'lib/provider_kit/provider_attribute.rb', line 13
def context
@context
end
|
#key ⇒ Object
Returns the value of attribute key.
11
12
13
|
# File 'lib/provider_kit/provider_attribute.rb', line 11
def key
@key
end
|
#record ⇒ Object
Returns the value of attribute record.
12
13
14
|
# File 'lib/provider_kit/provider_attribute.rb', line 12
def record
@record
end
|
Class Method Details
.dump(instance) ⇒ Object
90
91
92
93
94
95
96
97
|
# File 'lib/provider_kit/provider_attribute.rb', line 90
def self.dump(instance)
case instance
when self
instance.to_s
else
new(instance).to_s
end
end
|
.load(str) ⇒ Object
99
100
101
|
# File 'lib/provider_kit/provider_attribute.rb', line 99
def self.load(str)
new(str)
end
|
Instance Method Details
#==(other_key) ⇒ Object
21
22
23
|
# File 'lib/provider_kit/provider_attribute.rb', line 21
def ==(other_key)
to_s == other_key.to_s
end
|
#inspect ⇒ Object
25
26
27
|
# File 'lib/provider_kit/provider_attribute.rb', line 25
def inspect
@key.inspect
end
|
#label ⇒ Object
59
60
61
|
# File 'lib/provider_kit/provider_attribute.rb', line 59
def label
name
end
|
#name ⇒ Object
63
64
65
|
# File 'lib/provider_kit/provider_attribute.rb', line 63
def name
registration&.name.presence || key.to_s.titleize
end
|
#options ⇒ Object
67
68
69
|
# File 'lib/provider_kit/provider_attribute.rb', line 67
def options
registration&.options.presence || {}
end
|
#present? ⇒ Boolean
71
72
73
|
# File 'lib/provider_kit/provider_attribute.rb', line 71
def present?
provider.present?
end
|
#registration ⇒ Object
75
76
77
78
79
|
# File 'lib/provider_kit/provider_attribute.rb', line 75
def registration
if present?
ProviderKit.registrations[key.to_sym]
end
end
|
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/provider_kit/provider_attribute.rb', line 44
def respond_to_missing?(method_name, include_private = false)
return super unless provider.present?
return true if provider.respond_to?(method_name, include_private)
return true if method_name.to_s.match?(ATTRIBUTE_QUERY)
return true if provider.with_context(**context).respond_to?(method_name, include_private)
super
end
|
#to_s ⇒ Object
81
82
83
|
# File 'lib/provider_kit/provider_attribute.rb', line 81
def to_s
key.to_s
end
|
#with_context(**context) ⇒ Object
85
86
87
88
|
# File 'lib/provider_kit/provider_attribute.rb', line 85
def with_context(**context)
@context = context
self
end
|