Class: EacRubyUtils::Struct

Inherits:
Object show all
Defined in:
lib/eac_ruby_utils/struct.rb

Instance Method Summary collapse

Constructor Details

#initialize(initial_data = {}) ⇒ Struct

Returns a new instance of Struct.


8
9
10
# File 'lib/eac_ruby_utils/struct.rb', line 8

def initialize(initial_data = {})
  self.data = ::ActiveSupport::HashWithIndifferentAccess.new(initial_data)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments, &block) ⇒ Object


22
23
24
# File 'lib/eac_ruby_utils/struct.rb', line 22

def method_missing(method_name, *arguments, &block)
  property_method?(method_name) ? fetch(method_name) : super
end

Instance Method Details

#[](key) ⇒ Object


12
13
14
15
# File 'lib/eac_ruby_utils/struct.rb', line 12

def [](key)
  key, bool = parse_key(key)
  bool ? self[key].present? : data[key]
end

#fetch(key) ⇒ Object


17
18
19
20
# File 'lib/eac_ruby_utils/struct.rb', line 17

def fetch(key)
  key, bool = parse_key(key)
  bool ? fetch(key).present? : data.fetch(key)
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:


26
27
28
# File 'lib/eac_ruby_utils/struct.rb', line 26

def respond_to_missing?(method_name, include_private = false)
  property_method?(method_name) || super
end

#slice_fetch(*keys) ⇒ Object


30
31
32
# File 'lib/eac_ruby_utils/struct.rb', line 30

def slice_fetch(*keys)
  self.class.new(keys.map { |key| [key, fetch(key)] }.to_h)
end

#to_hObject


34
35
36
# File 'lib/eac_ruby_utils/struct.rb', line 34

def to_h
  data.dup
end