Class: Corelogic::Collection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/corelogic/collection.rb

Constant Summary collapse

DEFAULT_RECORDS_LIMIT =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, raw_hash) ⇒ Collection

Returns a new instance of Collection.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/corelogic/collection.rb', line 9

def initialize(klass, raw_hash)
  @klass = klass.is_a?(::String) ? ::Object.const_get(klass) : klass
  @raw_hash = raw_hash

  @total_pages = raw_hash[:totalPages] || 1
  @current_page = raw_hash[:pageNumber] || 1
  @limit_value = raw_hash[:pageSize] || DEFAULT_RECORDS_LIMIT
  @total_records = raw_hash[:totalRecords]

  if !@raw_hash[:data].nil? && !@raw_hash[:data].empty?
    @members = @raw_hash[:data].map do |record|
      @klass.new(record)
    end
  else
    @members = []
  end
end

Instance Attribute Details

#current_pageObject (readonly)

Returns the value of attribute current_page.



7
8
9
# File 'lib/corelogic/collection.rb', line 7

def current_page
  @current_page
end

#klassObject (readonly)

Returns the value of attribute klass.



7
8
9
# File 'lib/corelogic/collection.rb', line 7

def klass
  @klass
end

#limit_valueObject (readonly)

Returns the value of attribute limit_value.



7
8
9
# File 'lib/corelogic/collection.rb', line 7

def limit_value
  @limit_value
end

#membersObject (readonly)

Returns the value of attribute members.



7
8
9
# File 'lib/corelogic/collection.rb', line 7

def members
  @members
end

#raw_hashObject (readonly)

Returns the value of attribute raw_hash.



7
8
9
# File 'lib/corelogic/collection.rb', line 7

def raw_hash
  @raw_hash
end

#total_pagesObject (readonly)

Returns the value of attribute total_pages.



7
8
9
# File 'lib/corelogic/collection.rb', line 7

def total_pages
  @total_pages
end

#total_recordsObject (readonly)

Returns the value of attribute total_records.



7
8
9
# File 'lib/corelogic/collection.rb', line 7

def total_records
  @total_records
end

Instance Method Details

#to_aObject



27
28
29
# File 'lib/corelogic/collection.rb', line 27

def to_a
  @members
end