Class: Bumblebee::ObjectInterface
- Inherits:
-
Object
- Object
- Bumblebee::ObjectInterface
- Defined in:
- lib/bumblebee/object_interface.rb
Overview
Provides methods for interacting with custom objects.
Class Method Summary collapse
- .build(object, through) ⇒ Object
- .get(object, key) ⇒ Object
- .set(object, key, val) ⇒ Object
- .traverse(object, through) ⇒ Object
Class Method Details
.build(object, through) ⇒ Object
26 27 28 29 30 31 32 33 34 |
# File 'lib/bumblebee/object_interface.rb', line 26 def build(object, through) pointer = object through.each do |t| pointer = get(pointer, t) || get(set(pointer, t, pointer.class.new), t) end pointer end |
.get(object, key) ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/bumblebee/object_interface.rb', line 47 def get(object, key) if object.is_a?(Hash) indifferent_hash_get(object, key) elsif object.respond_to?(key) object.send(key) end end |
.set(object, key, val) ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/bumblebee/object_interface.rb', line 36 def set(object, key, val) object.tap do |o| setter_method = "#{key}=" if o.respond_to?(setter_method) o.send(setter_method, val) elsif o.respond_to?(:[]) o[key] = val end end end |
.traverse(object, through) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/bumblebee/object_interface.rb', line 14 def traverse(object, through) pointer = object through.each do |t| next unless pointer pointer = get(pointer, t) end pointer end |