Class: TurboReflex::AttributeSet

Inherits:
Object
  • Object
show all
Defined in:
lib/turbo_reflex/attribute_set.rb

Instance Method Summary collapse

Constructor Details

#initialize(prefix, attributes: {}) ⇒ AttributeSet

Returns a new instance of AttributeSet.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/turbo_reflex/attribute_set.rb', line 4

def initialize(prefix, attributes: {})
  prefix = prefix.to_s
  attrs = attributes.to_h.transform_values(&:to_s)

  attrs.each do |key, value|
    key = key.to_s.strip

    next unless key.start_with?(prefix)

    name = key.parameterize.underscore.delete_prefix("#{prefix}_")
    value = value.to_i if value.to_s.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



35
36
37
38
# File 'lib/turbo_reflex/attribute_set.rb', line 35

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

Instance Method Details

#orig_respond_to_missing?Object



29
# File 'lib/turbo_reflex/attribute_set.rb', line 29

alias_method :orig_respond_to_missing?, :respond_to_missing?

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

Returns:

  • (Boolean)


25
26
27
# File 'lib/turbo_reflex/attribute_set.rb', line 25

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

#respond_to_missing?(name, include_all) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/turbo_reflex/attribute_set.rb', line 31

def respond_to_missing?(name, include_all)
  true
end