Class: Datum
Overview
An immutable, dot-notation object built from attributes in a single call
Think of it as the sealed counterpart to OpenStruct: the ergonomics of OpenStruct.new(hash) with the immutability of Data. Values are stored as-is (nested hashes and arrays are left untouched), so the object is immutable only at the top level. Use Hash#to_datum when you want the conversion to recurse.
Class Method Summary collapse
-
.new(attributes = {}) ⇒ Datum
Builds an immutable object from the given attributes.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
Compares against another Datum by attributes rather than by class.
-
#hash ⇒ Integer
Returns a hash code derived from the attributes.
Methods included from Everythingrb::InspectQuotable
Class Method Details
.new(attributes = {}) ⇒ Datum
Builds an immutable object from the given attributes
32 33 34 |
# File 'lib/everythingrb/datum.rb', line 32 def self.new(attributes = {}) define(*attributes.keys.map(&:to_sym)).new(*attributes.values) end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
Compares against another Datum by attributes rather than by class
Every Datum is backed by its own anonymous Data class, so the default comparison (which requires an identical class) would never match. This compares the underlying attributes instead.
47 48 49 |
# File 'lib/everythingrb/datum.rb', line 47 def ==(other) other.is_a?(Datum) && to_h == other.to_h end |
#hash ⇒ Integer
Returns a hash code derived from the attributes
Keeps #hash consistent with #eql? so equal Datums collapse to the same Hash key or Set member.
61 62 63 |
# File 'lib/everythingrb/datum.rb', line 61 def hash to_h.hash end |