Class: Cheatorious::CheatSheet

Inherits:
Object
  • Object
show all
Defined in:
lib/cheatorious/cheatsheet.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, &block) ⇒ CheatSheet

Returns a new instance of CheatSheet.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/cheatorious/cheatsheet.rb', line 13

def initialize(name, &block)
  @name       = name
  @block      = block
  @keys       = {}
  @cheat_hash = {
    :info       => { :name => name },
    :cheatsheet => {
      :root    => []
    }
  }
  @current_section = @cheat_hash[:cheatsheet][:root]
  @stack           = []
  @separator       = ""
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



77
78
79
80
81
82
83
84
85
86
# File 'lib/cheatorious/cheatsheet.rb', line 77

def method_missing(method, *args)
  method_name = method.to_s

  if method_name.start_with?("_")
    method_name = method_name[/_(.*)/,1].to_sym
    return @keys[method_name] + (args[0].nil? ? "" : @separator + args[0].to_s) if @keys.key?(method_name)
  end
  
  super
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/cheatorious/cheatsheet.rb', line 11

def name
  @name
end

Class Method Details

.compile(name, output = nil, &block) ⇒ Object



5
6
7
8
# File 'lib/cheatorious/cheatsheet.rb', line 5

def compile(name, output = nil, &block)
  c = self.new(name, &block)
  c.compile!(output)
end

Instance Method Details

#__(name, *values) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/cheatorious/cheatsheet.rb', line 67

def __(name, *values) 
  new_entry = [name]
  
  values.each do |v|
    new_entry << v
  end
  
  @current_section << new_entry
end

#author(*args) ⇒ Object



41
42
43
# File 'lib/cheatorious/cheatsheet.rb', line 41

def author(*args)
  root[:info][:author] = args
end

#compile!(output = nil) ⇒ Object



28
29
30
31
# File 'lib/cheatorious/cheatsheet.rb', line 28

def compile!(output = nil)
  self.instance_eval(&@block)
  return output ? Utils.serialize(@cheat_hash, output) : @cheat_hash
end

#description(info) ⇒ Object



33
34
35
# File 'lib/cheatorious/cheatsheet.rb', line 33

def description(info) 
  root[:info][:description] = info
end

#key(identifier, value) ⇒ Object



49
50
51
# File 'lib/cheatorious/cheatsheet.rb', line 49

def key(identifier, value)
  @keys[identifier] = value
end

#key_separator(separator) ⇒ Object



45
46
47
# File 'lib/cheatorious/cheatsheet.rb', line 45

def key_separator(separator)
  @separator = separator
end

#section(name, &block) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/cheatorious/cheatsheet.rb', line 53

def section(name, &block)
  parent_section = @current_section
  
  new_section = { name => [] }
  @current_section = new_section[name]
  
  @stack.push(name)
  self.instance_eval(&block)
  @stack.pop
  
  @current_section = parent_section
  @current_section << new_section
end

#version(numbers) ⇒ Object



37
38
39
# File 'lib/cheatorious/cheatsheet.rb', line 37

def version(numbers)
  root[:info][:version] = numbers.to_s
end