class EacRubyUtils::Struct
Attributes
data[RW]
Public Class Methods
new(initial_data = {})
click to toggle source
# File lib/eac_ruby_utils/struct.rb, line 9 def initialize(initial_data = {}) self.data = initial_data.symbolize_keys end
Public Instance Methods
[](key)
click to toggle source
# File lib/eac_ruby_utils/struct.rb, line 13 def [](key) key, bool = parse_key(key) bool ? self[key].present? : data[key] end
fetch(key)
click to toggle source
# File lib/eac_ruby_utils/struct.rb, line 18 def fetch(key) key, bool = parse_key(key) bool ? fetch(key).present? : data.fetch(key) end
merge(other)
click to toggle source
# File lib/eac_ruby_utils/struct.rb, line 23 def merge(other) other = self.class.new(other) unless other.is_a?(self.class) self.class.new(to_h.merge(other.to_h)) end
method_missing(method_name, *arguments, &block)
click to toggle source
Calls superclass method
# File lib/eac_ruby_utils/struct.rb, line 28 def method_missing(method_name, *arguments, &block) property_method?(method_name) ? fetch(method_name) : super end
respond_to_missing?(method_name, include_private = false)
click to toggle source
Calls superclass method
# File lib/eac_ruby_utils/struct.rb, line 32 def respond_to_missing?(method_name, include_private = false) property_method?(method_name) || super end
slice_fetch(*keys)
click to toggle source
# File lib/eac_ruby_utils/struct.rb, line 36 def slice_fetch(*keys) self.class.new(keys.map { |key| [key, fetch(key)] }.to_h) end
to_h()
click to toggle source
# File lib/eac_ruby_utils/struct.rb, line 40 def to_h data.dup end
Private Instance Methods
parse_key(key)
click to toggle source
# File lib/eac_ruby_utils/struct.rb, line 50 def parse_key(key) m = /\A(.+)\?\z/.match(key.to_s) [(m ? m[1] : key.to_s).to_sym, m ? true : false] end
property_method?(key)
click to toggle source
# File lib/eac_ruby_utils/struct.rb, line 55 def property_method?(key) property_methods.include?(key.to_sym) end
property_methods()
click to toggle source
# File lib/eac_ruby_utils/struct.rb, line 59 def property_methods data.keys.flat_map { |k| [k.to_sym, "#{k}?".to_sym] } end