Class: MdnQuery::List

Inherits:
Object
  • Object
show all
Defined in:
lib/mdn_query/list.rb

Overview

A list from a search result.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query, *items) ⇒ MdnQuery::List

Creates a new list of search results.

Parameters:

  • query (String)

    the query that was searched for

  • items (MdnQuery::Entry)

    the items in the list



15
16
17
18
19
# File 'lib/mdn_query/list.rb', line 15

def initialize(query, *items)
  items = [] if items.nil?
  @query = query
  @items = items
end

Instance Attribute Details

#itemsArray<MdnQuery::Entry> (readonly)

Returns:



8
9
10
# File 'lib/mdn_query/list.rb', line 8

def items
  @items
end

#queryString (readonly)

Returns the query that was searched for.

Returns:

  • (String)

    the query that was searched for



5
6
7
# File 'lib/mdn_query/list.rb', line 5

def query
  @query
end

Instance Method Details

#[](pos) ⇒ MdnQuery::Entry

Retrieves the item at the given position.

Parameters:

  • pos (Fixnum)

    the position of the item

Returns:



32
33
34
# File 'lib/mdn_query/list.rb', line 32

def [](pos)
  items[pos]
end

#each(&block) ⇒ void

This method returns an undefined value.

Calls the given block for every item.

Parameters:

  • block (Block)

    block to be executed for every item.



54
55
56
# File 'lib/mdn_query/list.rb', line 54

def each(&block)
  items.each(&block)
end

#empty?Boolean

Returns whether the list is empty.

Returns:

  • (Boolean)

    whether the list is empty



39
40
41
# File 'lib/mdn_query/list.rb', line 39

def empty?
  items.empty?
end

#firstMdnQuery::Entry

Returns the first item in the list.

Returns:



24
25
26
# File 'lib/mdn_query/list.rb', line 24

def first
  items.first
end

#sizeFixnum

Returns the number of items in the list.

Returns:

  • (Fixnum)

    the number of items



46
47
48
# File 'lib/mdn_query/list.rb', line 46

def size
  items.size
end

#to_sString

Returns the string representation of the list.

Returns:

  • (String)


61
62
63
64
# File 'lib/mdn_query/list.rb', line 61

def to_s
  return "No results for '#{query}'" if empty?
  "Results for '#{query}':\n#{number_items(items).join("\n")}\n"
end