Class: Goods::Category
- Inherits:
-
Object
- Object
- Goods::Category
- Includes:
- Containable
- Defined in:
- lib/goods/category.rb
Instance Attribute Summary collapse
-
#parent ⇒ Object
Returns the value of attribute parent.
Instance Method Summary collapse
-
#initialize(info_hash) ⇒ Category
constructor
A new instance of Category.
- #level ⇒ Object
- #parent_at_level(level) ⇒ Object
- #root ⇒ Object
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
#parent ⇒ Object
Returns the value of attribute parent.
4 5 6 |
# File 'lib/goods/category.rb', line 4 def parent @parent end |
Instance Method Details
#level ⇒ Object
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 |
#root ⇒ Object
24 25 26 27 |
# File 'lib/goods/category.rb', line 24 def root return self unless parent @root ||= parent.root end |