Class: Mycommands::Category

Inherits:
Object
  • Object
show all
Defined in:
lib/mycommands/models/category.rb

Instance Method Summary collapse

Constructor Details

#initialize(category) ⇒ Category

Returns a new instance of Category.



46
47
48
49
50
51
# File 'lib/mycommands/models/category.rb', line 46

def initialize category
  @category = category
  if has_children?
    self.children = children.to_a.map{|c| Category.new(c)}.sort
  end
end

Instance Method Details

#<=>(other) ⇒ Object



69
70
71
# File 'lib/mycommands/models/category.rb', line 69

def <=> other
  self.name <=> other.name
end

#childrenObject



53
54
55
# File 'lib/mycommands/models/category.rb', line 53

def children
  @category[1]
end

#children=(children) ⇒ Object



57
58
59
# File 'lib/mycommands/models/category.rb', line 57

def children= children
  @category[1] = children
end

#has_children?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/mycommands/models/category.rb', line 61

def has_children?
  !children.nil?
end

#nameObject



65
66
67
# File 'lib/mycommands/models/category.rb', line 65

def name
  @category[0]
end

#to_hashObject



73
74
75
# File 'lib/mycommands/models/category.rb', line 73

def to_hash
  {@category[0] => has_children? ? children.inject({}) {|category_hash, c| category_hash.merge!(c.to_hash)} : nil }
end