Class: Mycommands::CategoryModel
- Defined in:
- lib/mycommands/models/category.rb
Instance Method Summary collapse
- #categories(choices = @choices) ⇒ Object
- #category ⇒ Object
- #choose(choice) ⇒ Object
- #go_back ⇒ Object
-
#initialize ⇒ CategoryModel
constructor
A new instance of CategoryModel.
- #sort_yaml_file! ⇒ Object
Constructor Details
#initialize ⇒ CategoryModel
Returns a new instance of CategoryModel.
3 4 5 |
# File 'lib/mycommands/models/category.rb', line 3 def initialize @choices = [] end |
Instance Method Details
#categories(choices = @choices) ⇒ Object
17 18 19 20 21 |
# File 'lib/mycommands/models/category.rb', line 17 def categories choices = @choices categories = choices.inject(all_categories) {|categories, choice| categories = categories[choice].children} return nil if categories.nil? categories end |
#category ⇒ Object
23 24 25 26 |
# File 'lib/mycommands/models/category.rb', line 23 def category return nil if @choices.empty? categories(@choices[0..-2])[@choices[-1]].name end |
#choose(choice) ⇒ Object
7 8 9 10 |
# File 'lib/mycommands/models/category.rb', line 7 def choose choice @choices.push choice.to_i - 1 self end |
#go_back ⇒ Object
12 13 14 15 |
# File 'lib/mycommands/models/category.rb', line 12 def go_back @choices.pop self end |
#sort_yaml_file! ⇒ Object
28 29 30 31 32 33 |
# File 'lib/mycommands/models/category.rb', line 28 def sort_yaml_file! category_hash = all_categories.inject({}) {|category_hash, c| category_hash.merge!(c.to_hash)} file = File.open(default_or_user_yml('categories.yml'), 'w') file.write(category_hash.to_yaml) file.path end |