Class: Tinybucket::Model::Page
- Inherits:
-
Object
- Object
- Tinybucket::Model::Page
- Defined in:
- lib/tinybucket/model/page.rb
Overview
Page
Instance Attribute Summary collapse
-
#attrs ⇒ Hash
readonly
This attribute is a Hash object which contains ‘size’, ‘page’, ‘pagelen’, ‘next’, ‘previous’ key/value pairs.
-
#items ⇒ Object
readonly
This attribute is a array of model instance created with ‘values’ attribute in json.
Instance Method Summary collapse
-
#initialize(json, item_klass) ⇒ Page
constructor
Initialize with json and Model class.
Constructor Details
#initialize(json, item_klass) ⇒ Page
Initialize with json and Model 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
#attrs ⇒ Hash (readonly)
This attribute is a Hash object which contains ‘size’, ‘page’, ‘pagelen’, ‘next’, ‘previous’ key/value pairs.
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 |
#items ⇒ Object (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 |