Sha256: 90a92841409ff2395d282c0c0ca46e45de235677c664f3c06b88ab8d751a1e09

Contents?: true

Size: 938 Bytes

Versions: 1

Compression:

Stored size: 938 Bytes

Contents

module Twitter
  class Value
    def self.new(*fields, &block)
      Class.new do
        attr_reader(:hash, *fields)

        const_set :VALUE_ATTRS, fields

        define_method(:initialize) do |hash|
          unexpected_keys = hash.keys - self::VALUE_ATTRS
          if unexpected_keys.any?
            fail ArgumentError.new("Unexpected hash keys: #{unexpected_keys}")
          end

          hash.values_at(*self::VALUE_ATTRS).zip(hash.values) do |field, value|
            instance_variable_set(:"@#{field}", value)
          end

          @hash = self.class.hash ^ hash.values.hash

          freeze
        end

        def ==(other)
          eql?(other)
        end

        def eql?(other)
          self.class == other.class && values == other.values
        end

        def values
          self.class::VALUE_ATTRS.map { |field| send(field) }
        end

        class_eval(&block) if block
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
twitter-5.14.0 lib/twitter/value.rb