Sha256: a2d31b546607906ca32c1223f7e2c87d146754ba068b19d58cdda84625bd1453
Contents?: true
Size: 1.23 KB
Versions: 20
Compression:
Stored size: 1.23 KB
Contents
# frozen_string_literal: true module LedgerSync module Util class ReadOnlyObject attr_reader :raw def initialize(args = {}) @raw = args.with_indifferent_access self.class.attributes.except { |_k, _| e.to_sym == :raw }.each do |name, attr_settings| if attr_settings.key?(:default) instance_variable_set( "@#{name}", raw.fetch(name, attr_settings[:default]) ) else instance_variable_set( "@#{name}", raw.fetch(attr_settings.fetch(:source, name)) ) end end end def ==(other) self.class.attributes.keys.all? { |name| send(name) == other.send(name) } end def self.attributes @attributes ||= {} end def self.attribute(name, **keywords) attributes[name.to_sym] = keywords attr_reader name end def self.new_from_array(data) data.map { |e| new_from_hash(e) } end def self.new_from_hash(data) new( data.symbolize_keys ) end def self.source_keys @source_keys ||= attributes.map { |k, v| v.fetch(:source, k).to_sym } end end end end
Version data entries
20 entries across 20 versions & 1 rubygems