Class: R2do::State

Inherits:
Object
  • Object
show all
Defined in:
lib/r2do/state.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeState

Creates a new instance of the State



29
30
31
# File 'lib/r2do/state.rb', line 29

def initialize()
  init()
end

Instance Attribute Details

#categoriesHash

Returns the collection of categories created by the user.

Returns:

  • (Hash)

    the collection of categories created by the user.



21
22
23
# File 'lib/r2do/state.rb', line 21

def categories
  @categories
end

#current_categoryCategory

Returns the current category the user is working on.

Returns:

  • (Category)

    the current category the user is working on.



23
24
25
# File 'lib/r2do/state.rb', line 23

def current_category
  @current_category
end

#modifiedbool

Returns true if the state has been modified.

Returns:

  • (bool)

    true if the state has been modified



25
26
27
# File 'lib/r2do/state.rb', line 25

def modified
  @modified
end

Instance Method Details

#add(category) ⇒ void

This method returns an undefined value.

Adds a category.

Parameters:

  • category (Category)

    the category to add.



79
80
81
# File 'lib/r2do/state.rb', line 79

def add(category)
  @categories[category.name] = category
end

#clear_current_categoryObject

Clears the current category

return [void]



55
56
57
# File 'lib/r2do/state.rb', line 55

def clear_current_category()
  @current_category = nil
end

#contains?(category_name) ⇒ bool

Checks if a category with a specific name already exists.

Parameters:

  • category_name (String)

    the name of the category to check.

Returns:

  • (bool)

    true if the category is already present.



63
64
65
# File 'lib/r2do/state.rb', line 63

def contains?(category_name)
  @categories.has_key?(category_name)
end

#get(category_name) ⇒ Category

Retrieves a specific Category.

Parameters:

  • category_name (String)

    the name of the category to retrieve.

Returns:

  • (Category)

    the category identified by category_name.



71
72
73
# File 'lib/r2do/state.rb', line 71

def get(category_name)
  @categories[category_name]
end

#initObject



33
34
35
36
37
# File 'lib/r2do/state.rb', line 33

def init()
  @categories = Hash.new
  @current_category = nil
  @modified = false
end

#refresh(original_name, category) ⇒ Object



84
85
86
87
# File 'lib/r2do/state.rb', line 84

def refresh(original_name, category)
  @categories.delete(original_name) { raise CategoryNotFoundError.new() }
  @categories[category.name] = category
end

#remove(category) ⇒ void

This method returns an undefined value.

Removes the category from the state.

Parameters:

  • category (Category)

    the category to remove.

Raises:

  • (Exceptions::CategoryNotFoundError)

    if category is not found.



94
95
96
# File 'lib/r2do/state.rb', line 94

def remove(category)
  @categories.delete(category.name) { raise CategoryNotFoundError.new() }
end

#resetObject



39
40
41
42
# File 'lib/r2do/state.rb', line 39

def reset()
  init()
  @modified = true
end

#set_current(category) ⇒ void

This method returns an undefined value.

Sets a Category as the current one.

Parameters:

  • category (Category)

    the category to be set as current.



48
49
50
# File 'lib/r2do/state.rb', line 48

def set_current(category)
  @current_category = category
end