Class: Goods::Category

Inherits:
Object
  • Object
show all
Includes:
Containable
Defined in:
lib/goods/category.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Containable

#id, included, #invalid_fields, #valid?

Constructor Details

#initialize(info_hash) ⇒ Category

Returns a new instance of Category.



8
9
10
11
# File 'lib/goods/category.rb', line 8

def initialize(info_hash)
  self._info_hash = info_hash
  @parent_at_level = {}
end

Instance Attribute Details

#parentObject

Returns the value of attribute parent.



4
5
6
# File 'lib/goods/category.rb', line 4

def parent
  @parent
end

Instance Method Details

#levelObject



13
14
15
16
17
18
19
20
21
22
# File 'lib/goods/category.rb', line 13

def level
  @level ||=
    begin
      if parent
        parent.level + 1
      else
        0
      end
    end
end

#parent_at_level(level) ⇒ Object



29
30
31
32
33
34
# File 'lib/goods/category.rb', line 29

def parent_at_level(level)
  valid_parent_level?(level) or raise ArgumentError.new('incorrect level')
  return self if self.level == level

  @parent_at_level[level] ||= parent.parent_at_level(level)
end

#rootObject



24
25
26
27
# File 'lib/goods/category.rb', line 24

def root
  return self unless parent
  @root ||= parent.root
end