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
35
36
37
|
# 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)
begin
next if instance_variable_defined?(:"@#{name}")
next if orig_respond_to_missing?(name, false)
instance_variable_set :"@#{name}", value
self.class.define_method(name) { instance_variable_get :"@#{name}" }
self.class.define_method(:"#{name}?") { public_send(name).present? }
rescue Exception => error Rails.logger.error "TurboBoost::Commands::AttributeSet failed to define method! #{name}; #{error}"
end
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
66
67
68
69
|
# File 'lib/turbo_boost/commands/attribute_set.rb', line 66
def method_missing(name, *args)
return false if name.end_with?("?")
nil
end
|
Instance Method Details
#orig_respond_to_missing? ⇒ Object
60
|
# File 'lib/turbo_boost/commands/attribute_set.rb', line 60
alias_method :orig_respond_to_missing?, :respond_to_missing?
|
#render_options ⇒ Object
47
48
49
50
51
52
53
54
|
# File 'lib/turbo_boost/commands/attribute_set.rb', line 47
def render_options
options = {
partial: renders,
assigns: assigns,
locals: locals
}
options.deep_symbolize_keys
end
|
#respond_to?(name, include_all = false) ⇒ Boolean
56
57
58
|
# File 'lib/turbo_boost/commands/attribute_set.rb', line 56
def respond_to?(name, include_all = false)
respond_to_missing? name, include_all
end
|
#respond_to_missing?(name, include_all) ⇒ Boolean
62
63
64
|
# File 'lib/turbo_boost/commands/attribute_set.rb', line 62
def respond_to_missing?(name, include_all)
true
end
|
#to_h ⇒ Object
39
40
41
42
43
44
45
|
# File 'lib/turbo_boost/commands/attribute_set.rb', line 39
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
|