Class: AboutYou::SDK::Model::Category

Inherits:
Object
  • Object
show all
Defined in:
lib/AboutYou/Model/category.rb

Overview

This class represents a category model

Constant Summary collapse

ALL =

all categories

false
ACTIVE_ONLY =

only active categories

true

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCategory

Constructor for the AboutYou::SDK::Model::Category class

  • Returns :

    • an instance of AboutYou::SDK::Model::Category



38
39
40
41
42
43
# File 'lib/AboutYou/Model/category.rb', line 38

def initialize
  self.all_subcategories = []
  self.active_subcategories = []

  self
end

Instance Attribute Details

#active_subcategoriesObject

Array containing all active subcategories



26
27
28
# File 'lib/AboutYou/Model/category.rb', line 26

def active_subcategories
  @active_subcategories
end

#all_subcategoriesObject

Array containing all subcategories



24
25
26
# File 'lib/AboutYou/Model/category.rb', line 24

def all_subcategories
  @all_subcategories
end

#category_managerObject

instance of AboutYou::SDK::Model::CategoryManager::DefaultCategoryManager



30
31
32
# File 'lib/AboutYou/Model/category.rb', line 30

def category_manager
  @category_manager
end

#idObject

id of the category



14
15
16
# File 'lib/AboutYou/Model/category.rb', line 14

def id
  @id
end

#is_activeObject

boolean determining whether the category is active



18
19
20
# File 'lib/AboutYou/Model/category.rb', line 18

def is_active
  @is_active
end

#nameObject

name of the category



16
17
18
# File 'lib/AboutYou/Model/category.rb', line 16

def name
  @name
end

#parent_idObject

id of the parent category



22
23
24
# File 'lib/AboutYou/Model/category.rb', line 22

def parent_id
  @parent_id
end

#positionObject

position of the category



20
21
22
# File 'lib/AboutYou/Model/category.rb', line 20

def position
  @position
end

#product_countObject

count of products in category



28
29
30
# File 'lib/AboutYou/Model/category.rb', line 28

def product_count
  @product_count
end

Class Method Details

.create_from_json(json_object, cat_manager) ⇒ Object

This method is used for creating an instance of this class by a json_object.

  • Args :

    • json_object -> the json_object received from the api

    • cat_manager -> instance of AboutYou::SDK::Model::CategoryManager::DefaultCategoryManager

  • Returns :

    • Instance of AboutYou::SDK::Model::Category



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/AboutYou/Model/category.rb', line 55

def self.create_from_json(json_object, cat_manager)
  category = new

  category.parent_id        = json_object['parent']
  category.id               = json_object['id']
  category.name             = json_object['name']
  category.is_active        = json_object['active']
  category.position         = json_object['position']
  category.category_manager = cat_manager

  category
end

Instance Method Details

This method is used for getting the breadcrumb up to this category

  • Returns :

    • Array containing instances of AboutYou::SDK::Model::Category



119
120
121
122
123
124
# File 'lib/AboutYou/Model/category.rb', line 119

def breadcrumb
  breadcrumb = parent ? parent.breadcrumb : []
  breadcrumb.push(self)

  breadcrumb
end

#parentObject

This method returns the parent category

  • Returns :

    • Either instance of AboutYou::SDK::Model::Category or nil if no parent category is set



85
86
87
88
89
# File 'lib/AboutYou/Model/category.rb', line 85

def parent
  return nil unless parent_id

  category_manager.category(parent_id)
end

#path_active?Boolean

This method checks if the complete category path up to this category is active

  • Returns :

    • Boolean determining whether the complete path up to this category is active or not

Returns:

  • (Boolean)


75
76
77
# File 'lib/AboutYou/Model/category.rb', line 75

def path_active?
  is_active && (parent.nil? || parent.path_active?)
end

#subcategories(active_only = ACTIVE_ONLY) ⇒ Object

This method is used for getting all subcategories

  • Args :

    • active_only -> boolean controlling whether the result should only contain active categories or not

  • Returns :

    • Hash containing pairs of category_id => instance of AboutYou::SDK::Model::Category



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/AboutYou/Model/category.rb', line 100

def subcategories(active_only = ACTIVE_ONLY)
  subcategories = category_manager.subcategories(id, active_only)
  return subcategories if active_only == ALL

  result = {}

  subcategories.each do |key, subcat|
    result[key] = subcat if subcat.is_active
  end

  result
end