Class: Tinybucket::Model::Page

Inherits:
Object
  • Object
show all
Defined in:
lib/tinybucket/model/page.rb

Overview

Page

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json, item_klass) ⇒ Page

Initialize with json and Model class.

Parameters:

  • json (Hash)
  • item_klass (Class)


25
26
27
28
# File 'lib/tinybucket/model/page.rb', line 25

def initialize(json, item_klass)
  @attrs = parse_attrs(json)
  @items = parse_values(json, item_klass)
end

Instance Attribute Details

#attrsHash (readonly)

This attribute is a Hash object which contains ‘size’, ‘page’, ‘pagelen’, ‘next’, ‘previous’ key/value pairs.

Returns:

  • (Hash)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/tinybucket/model/page.rb', line 17

class Page
  attr_reader :attrs
  attr_reader :items

  # Initialize with json and Model class.
  #
  # @param json [Hash]
  # @param item_klass [Class]
  def initialize(json, item_klass)
    @attrs = parse_attrs(json)
    @items = parse_values(json, item_klass)
  end

  private

  def parse_attrs(json)
    %w(size page pagelen next previous).map do |attr|
      { attr.to_sym => json[attr] }
    end.reduce(&:merge)
  end

  def parse_values(json, item_klass)
    return [] if json['values'].nil? || !json['values'].is_a?(Array)

    json['values'].map { |hash| item_klass.new(hash) }
  end
end

#itemsObject (readonly)

This attribute is a array of model instance created with ‘values’ attribute in json.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/tinybucket/model/page.rb', line 17

class Page
  attr_reader :attrs
  attr_reader :items

  # Initialize with json and Model class.
  #
  # @param json [Hash]
  # @param item_klass [Class]
  def initialize(json, item_klass)
    @attrs = parse_attrs(json)
    @items = parse_values(json, item_klass)
  end

  private

  def parse_attrs(json)
    %w(size page pagelen next previous).map do |attr|
      { attr.to_sym => json[attr] }
    end.reduce(&:merge)
  end

  def parse_values(json, item_klass)
    return [] if json['values'].nil? || !json['values'].is_a?(Array)

    json['values'].map { |hash| item_klass.new(hash) }
  end
end