Class: Badginator

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/badginator.rb,
lib/badginator/badge.rb,
lib/badginator/status.rb,
lib/badginator/nominee.rb,
lib/badginator/version.rb,
lib/generators/badginator/install_generator.rb

Defined Under Namespace

Modules: Nominee Classes: Badge, InstallGenerator, Status

Constant Summary collapse

DID_NOT_WIN =
1
WON =
2
ALREADY_WON =
3
ERROR =
4
VERSION =
"0.0.5"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBadginator

Returns a new instance of Badginator.



15
16
17
# File 'lib/badginator.rb', line 15

def initialize
  @badges = {}
end

Class Method Details

.badgesObject



27
28
29
# File 'lib/badginator.rb', line 27

def self.badges
  self.instance.badges
end

.define_badge(*args, &block) ⇒ Object



43
44
45
# File 'lib/badginator.rb', line 43

def self.define_badge(*args, &block)
  self.instance.define_badge(*args, &block)
end

.get_badge(badge_code) ⇒ Object



47
48
49
# File 'lib/badginator.rb', line 47

def self.get_badge(badge_code)
  self.instance.get_badge(badge_code)
end

.Status(status_code, badge = nil) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/badginator.rb', line 51

def self.Status(status_code, badge = nil)
  case status_code
    when DID_NOT_WIN, WON, ALREADY_WON, ERROR
      Badginator::Status.new code: status_code, badge: badge
    else
      rails TypeError, "Cannot convert #{status_code} to Status"
  end
end

Instance Method Details

#badgesObject



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

def badges
 @badges.values.select { |badge| ! badge.disabled }
end

#define_badge(*args, &block) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/badginator.rb', line 31

def define_badge(*args, &block)
  badge = Badge.new
  badge.build_badge &block
  badge.freeze

  if @badges.key?(badge.code)
    raise "badge code '#{badge.code}' already defined."
  end

  @badges[badge.code] = badge
end

#get_badge(badge_code) ⇒ Object



19
20
21
# File 'lib/badginator.rb', line 19

def get_badge(badge_code)
  @badges.fetch(badge_code)
end