Class: TurboBoost::Commands::AttributeSet
- Inherits:
-
Object
- Object
- TurboBoost::Commands::AttributeSet
show all
- Includes:
- AttributeHydration
- Defined in:
- lib/turbo_boost/commands/attribute_set.rb
Instance Method Summary
collapse
#dehydrate, #hydrate
Constructor Details
#initialize(attributes = {}, prefix: nil) ⇒ AttributeSet
Returns a new instance of AttributeSet.
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/turbo_boost/commands/attribute_set.rb', line 11
def initialize(attributes = {}, prefix: nil)
prefix = prefix.to_s
attrs = hydrate(attributes)
attrs.each do |key, value|
key = key.to_s.strip
next unless prefix.blank? || key.start_with?(prefix)
name = key.parameterize.underscore
name.delete_prefix!("#{prefix}_") unless prefix.blank?
value = value.to_i if value.is_a?(String) && value.match?(/\A-?\d+\z/)
value = value == "true" if value.is_a?(String) && value.match?(/\A(true|false)\z/i)
instance_variable_set :"@#{name}", value
next if orig_respond_to_missing?(name, false)
self.class.define_method(name) { instance_variable_get :"@#{name}" }
self.class.define_method(:"#{name}?") { public_send(name).present? }
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
63
64
65
66
|
# File 'lib/turbo_boost/commands/attribute_set.rb', line 63
def method_missing(name, *args)
return false if name.end_with?("?")
nil
end
|
Instance Method Details
#orig_respond_to_missing? ⇒ Object
57
|
# File 'lib/turbo_boost/commands/attribute_set.rb', line 57
alias_method :orig_respond_to_missing?, :respond_to_missing?
|
#render_options ⇒ Object
44
45
46
47
48
49
50
51
|
# File 'lib/turbo_boost/commands/attribute_set.rb', line 44
def render_options
options = {
partial: renders,
assigns: assigns,
locals: locals
}
options.deep_symbolize_keys
end
|
#respond_to?(name, include_all = false) ⇒ Boolean
53
54
55
|
# File 'lib/turbo_boost/commands/attribute_set.rb', line 53
def respond_to?(name, include_all = false)
respond_to_missing? name, include_all
end
|
#respond_to_missing?(name, include_all) ⇒ Boolean
59
60
61
|
# File 'lib/turbo_boost/commands/attribute_set.rb', line 59
def respond_to_missing?(name, include_all)
true
end
|
#to_h ⇒ Object
36
37
38
39
40
41
42
|
# File 'lib/turbo_boost/commands/attribute_set.rb', line 36
def to_h
instance_variables.each_with_object({}.with_indifferent_access) do |name, memo|
value = instance_variable_get(name)
value = value.to_h if value.is_a?(self.class)
memo[name.to_s.delete_prefix("@").to_sym] = value
end
end
|