Class: TurboBoost::Commands::AttributeSet

Inherits:
Object
  • Object
show all
Includes:
AttributeHydration
Defined in:
lib/turbo_boost/commands/attribute_set.rb

Instance Method Summary collapse

Methods included from AttributeHydration

#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?

    # type casting
    value = value.to_i if cast_to_integer?(value)
    value = value == "true" if cast_to_boolean?(value)

    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 # standard:disable Lint/RescueException
      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



74
75
76
77
# File 'lib/turbo_boost/commands/attribute_set.rb', line 74

def method_missing(name, *args)
  return false if name.end_with?("?")
  nil
end

Instance Method Details

#include?(key) ⇒ Boolean Also known as: has_key?, key?, member?

Returns:

  • (Boolean)


39
40
41
# File 'lib/turbo_boost/commands/attribute_set.rb', line 39

def include?(key)
  instance_variable_defined?(:"@#{key}")
end

#orig_respond_to_missing?Object



68
# File 'lib/turbo_boost/commands/attribute_set.rb', line 68

alias_method :orig_respond_to_missing?, :respond_to_missing?

#render_optionsObject



55
56
57
58
59
60
61
62
# File 'lib/turbo_boost/commands/attribute_set.rb', line 55

def render_options
  options = {
    partial: renders,
    assigns: assigns,
    locals: locals
  }
  options.deep_symbolize_keys
end

#respond_to?(name, include_all = false) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/turbo_boost/commands/attribute_set.rb', line 64

def respond_to?(name, include_all = false)
  respond_to_missing? name, include_all
end

#respond_to_missing?(name, include_all) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/turbo_boost/commands/attribute_set.rb', line 70

def respond_to_missing?(name, include_all)
  true
end

#to_hObject



47
48
49
50
51
52
53
# File 'lib/turbo_boost/commands/attribute_set.rb', line 47

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