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

Version Path
ledger_sync-3.0.0 lib/ledger_sync/util/read_only_object.rb
ledger_sync-2.6.0 lib/ledger_sync/util/read_only_object.rb
ledger_sync-2.5.0 lib/ledger_sync/util/read_only_object.rb
ledger_sync-2.4.4 lib/ledger_sync/util/read_only_object.rb
ledger_sync-2.3.1 lib/ledger_sync/util/read_only_object.rb
ledger_sync-2.2.3 lib/ledger_sync/util/read_only_object.rb
ledger_sync-2.2.1 lib/ledger_sync/util/read_only_object.rb
ledger_sync-2.2.0 lib/ledger_sync/util/read_only_object.rb
ledger_sync-2.0.2 lib/ledger_sync/util/read_only_object.rb
ledger_sync-2.0.1 lib/ledger_sync/util/read_only_object.rb
ledger_sync-2.0.0 lib/ledger_sync/util/read_only_object.rb
ledger_sync-2.0.0.pre.1 lib/ledger_sync/util/read_only_object.rb
ledger_sync-1.8.1 lib/ledger_sync/util/read_only_object.rb
ledger_sync-1.8.0 lib/ledger_sync/util/read_only_object.rb
ledger_sync-1.7.0 lib/ledger_sync/util/read_only_object.rb
ledger_sync-1.6.0 lib/ledger_sync/util/read_only_object.rb
ledger_sync-1.5.2 lib/ledger_sync/util/read_only_object.rb
ledger_sync-1.5.1 lib/ledger_sync/util/read_only_object.rb
ledger_sync-1.5.0 lib/ledger_sync/util/read_only_object.rb
ledger_sync-1.4.4 lib/ledger_sync/util/read_only_object.rb