Sha256: 198564312fc9a6ab33e666a83240bb792faef696462829716f2264caecf21add

Contents?: true

Size: 798 Bytes

Versions: 2

Compression:

Stored size: 798 Bytes

Contents

# frozen_string_literal: true

require_relative 'basic_model'

module Redd
  module Models
    # A backward-expading listing of items.
    # @see Stream
    class Listing < BasicModel
      include Enumerable

      # Make a Listing from a basic Hash.
      # @return [Listing]
      def self.from_response(client, hash)
        hash[:children].map! { |el| client.unmarshal(el) }
        new(client, hash)
      end

      # @return [Array<Comment, Submission, PrivateMessage>] an array representation of self
      def to_ary
        get_attribute(:children)
      end

      %i([] each empty? first last).each do |method_name|
        define_method(method_name) do |*args, &block|
          get_attribute(:children).public_send(method_name, *args, &block)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
redd-0.8.1 lib/redd/models/listing.rb
redd-0.8.0 lib/redd/models/listing.rb