module RocketGate::Hashable

Public Class Methods

included(base) click to toggle source
# File lib/rocketgate/hashable.rb, line 32
def self.included(base)
  base.extend(ClassMethods)
end

Public Instance Methods

to_hash() click to toggle source
# File lib/rocketgate/hashable.rb, line 36
def to_hash
  hash = Hash.new.tap do |hsh|
    self.class.mappings.each do |display_name, obj|
      hsh[display_name] = attribute_or_proc_value(obj)
    end
  end

  self.class.mergables.each do |obj|
    hash.merge!(attribute_or_proc_value(obj))
  end if self.class.mergables.any?

  hash
end

Private Instance Methods

attribute_or_proc_value(obj) click to toggle source
# File lib/rocketgate/hashable.rb, line 52
def attribute_or_proc_value(obj)
  if obj.is_a?(Proc)
    obj.call
  elsif self.respond_to?(obj)
    self.send(obj)
  end
end
is_callable?(obj) click to toggle source
# File lib/rocketgate/hashable.rb, line 60
def is_callable?(obj)
  obj.is_a?(Proc) || self.respond_to?(obj)
end