Class: Yuyi::Menu

Inherits:
Object
  • Object
show all
Defined in:
lib/yuyi/menu.rb

Constant Summary collapse

nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Menu

Create a new menu that stores the raw yaml file along with source, roll, and roll model instances



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/yuyi/menu.rb', line 15

def initialize path
  # make menu instance accessible by the class
  @@menu = self
  @rolls = {}
  @sources = []

  # load a yaml file
  load_from_file path

  # create objects from the menu
  set_sources
  set_roll_models
  set_rolls
end

Instance Attribute Details

#progressbarObject

Returns the value of attribute progressbar.



5
6
7
# File 'lib/yuyi/menu.rb', line 5

def progressbar
  @progressbar
end

Class Method Details

.add_roll(file_name, klass) ⇒ Object

Add rolls to hash in format of RollInstance Called from Yuyi::Roll when a roll is inherited



43
44
45
46
47
# File 'lib/yuyi/menu.rb', line 43

def self.add_roll file_name, klass
  unless klass.to_s.include? 'RollModel'
    rolls[file_name] = klass.new
  end
end

.find_roll(name, options = {}) ⇒ Object



53
54
55
# File 'lib/yuyi/menu.rb', line 53

def self.find_roll name, options = {}
  menu.send :find_roll, name, options
end

.load_from_fileObject



57
58
59
# File 'lib/yuyi/menu.rb', line 57

def self.load_from_file
  menu.send :load_from_file
end

.on_the_menu?(roll) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/yuyi/menu.rb', line 49

def self.on_the_menu? roll
  menu.send :on_the_menu?, roll
end

Instance Method Details

#find_roll(name, options = {}) ⇒ Object

Find the best roll match from the sources and require it



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/yuyi/menu.rb', line 63

def find_roll name, options = {}
  return if on_the_menu? name

  # return specific source roll if specified in the menu
  #
  if source = options[:source]
    require_roll name, File.join(source, name.to_s)
    return

  # look through the sources for the first roll that matches.
  # sources are listed in the menu in order of priority
  else
    @sources.each do |source|
      if path = source.rolls[name]
        require_roll name, path
        return
      end
    end
  end

  # no roll was found
  Yuyi.say "You ordered the '#{name}' roll off the menu, but we are fresh out...", :type => :fail
  Yuyi.say 'Check your menu to make sure a source with your roll is listed.', :type => :warn
  Yuyi.say
end

#options(roll) ⇒ Object



30
31
32
# File 'lib/yuyi/menu.rb', line 30

def options roll
  @yaml[:rolls][roll] || {}
end