Sha256: f3649e7ec36b374fc3771f2c15baf09b374eb465bdbda058ac95f062f27bbdd2

Contents?: true

Size: 923 Bytes

Versions: 3

Compression:

Stored size: 923 Bytes

Contents

module Trajectory
  class Users < SimpleDelegator
    alias :users :__getobj__

    # Creates a new collection of {User}
    #
    # @param users [Array<User>] a arbitrary lenght list of {User} objects
    def initialize(*users)
      super(users)
    end

    # Create a new collection of {User} from a JSON array of attributes from trajectory API
    #
    # @param json_attributes [Hash] the hash of attributes of each user of the collection
    def self.from_json(json_attributes)
      new(*json_attributes.map do |attributes|
        User.new(attributes.symbolize_keys!)
      end)
    end

    # Returns the the first user with the given id in the collection or false if
    # no user can be found with the id
    #
    # @param id [Integer] the id of the user to find
    # @return [User, false] the user with the given id
    def find_by_id(id)
      users.find { |user| user.id == id } || false
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
trajectory-0.1.2 lib/trajectory/domain/users.rb
trajectory-0.1.1 lib/trajectory/domain/users.rb
trajectory-0.1.0 lib/trajectory/domain/users.rb