Module: Receiver::ActsAsHash

Defined in:
lib/gorillib/receiver/acts_as_hash.rb,
lib/gorillib/receiver/tree_diff.rb

Overview

Makes a Receiver thingie behave mostly like a hash.

By default, the hashlike methods iterate over the receiver attributes: instance #keys delegates to self.class.keys which calls receiver_attr_names. If you want to filter our add to the keys list, you can just override the class-level keys method (and call super, or not):

def self.keys
  super + [:firstname, :lastname] - [:fullname]
end

All methods are defined naturally on [], []= and has_key? – if you enjoy

in addition to the below, by including Enumerable, this also adds

:each_cons, :each_entry, :each_slice, :each_with_index, :each_with_object,
:map, :collect, :collect_concat, :entries, :to_a, :flat_map, :inject, :reduce,
:group_by, :chunk, :cycle, :partition, :reverse_each, :slice_before, :drop,
:drop_while, :take, :take_while, :detect, :find, :find_all, :find_index, :grep,
:all?, :any?, :none?, :one?, :first, :count, :zip :max, :max_by, :min, :min_by,
:minmax, :minmax_by, :sort, :sort_by

As opposed to hash, does not define

default, default=, default_proc, default_proc=, shift, flatten, compare_by_identity
compare_by_identity? rehash

Defined Under Namespace

Modules: ClassMethods, InstanceMethods

Instance Method Summary collapse

Instance Method Details

#<=>(other) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/gorillib/receiver/tree_diff.rb', line 22

def <=>(other)
  return 1 if other.blank?
  each_key do |k|
    if has_key?(k) && other.has_key?(k)
      cmp = self[k] <=> other[k]
      return cmp unless cmp == 0
    end
  end
  0
end