Module: Kantox::Split::Utils
- Included in:
- Graph::Attributed
- Defined in:
- lib/kantox/split/utils.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
- .included(base) ⇒ Object
- .lookup_variable(object, name) ⇒ Object
- .lookup_variable_value(object, getter) ⇒ Object
- .omnivorous_to_h(obj, levels = 1, collected = [], default = nil) ⇒ Object
- .store_variable(object, name, value) ⇒ Object
Instance Method Summary collapse
Class Method Details
.included(base) ⇒ Object
44 45 46 |
# File 'lib/kantox/split/utils.rb', line 44 def self.included base base.extend ClassMethods end |
.lookup_variable(object, name) ⇒ Object
10 11 12 13 14 15 16 17 |
# File 'lib/kantox/split/utils.rb', line 10 def lookup_variable object, name return object.instance_variable_get(:"@#{name}") if object.instance_variable_defined?(:"@#{name}") klazz = object.class while klazz do break klazz.instance_variable_get(:"@#{name}") if klazz.instance_variable_defined?(:"@#{name}") klazz = klazz.superclass end end |
.lookup_variable_value(object, getter) ⇒ Object
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/kantox/split/utils.rb', line 18 def lookup_variable_value object, getter case getter when Array then getter.map { |v| lookup_variable_value object, v } when Hash then getter.map { |k, v| [k, lookup_variable_value(object, v)] }.to_h when String then object.instance_eval(getter) rescue nil when Symbol then object.public_send(getter) rescue nil when ->(p) { p.respond_to? :to_proc } then getter.to_proc.call(object) rescue nil else raise ArgumentError.new "Expected Array, Hash, String, Symbol or Proc. Got: #{getter.class}" end end |
.omnivorous_to_h(obj, levels = 1, collected = [], default = nil) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/kantox/split/utils.rb', line 29 def omnivorous_to_h obj, levels = 1, collected = [], default = nil return default ? "∃#{instance_eval(default)}" : obj if collected.include? obj levels < 0 ? obj : case obj when Array obj.map { |e| omnivorous_to_h e, levels, collected, default } when ->(o) { o.methods.include?(:to_h) && o.method(:to_h).parameters.map(&:first) == [:opt, :opt] } obj.to_h levels - 1, collected << obj else obj end end |
.store_variable(object, name, value) ⇒ Object
5 6 7 8 9 |
# File 'lib/kantox/split/utils.rb', line 5 def store_variable object, name, value # FIXME object.instance_variable_set :"@#{name}", value object.class.instance_variable_set :"@#{name}", value end |
Instance Method Details
#lookup_variable(name) ⇒ Object
54 55 56 |
# File 'lib/kantox/split/utils.rb', line 54 def lookup_variable name Utils.lookup_variable self, name end |
#lookup_variable_value(name) ⇒ Object
57 58 59 |
# File 'lib/kantox/split/utils.rb', line 57 def lookup_variable_value name Utils.lookup_variable_value self, name end |