Class: AdHonorem::Badge

Inherits:
StaticRecord::Base
  • Object
show all
Includes:
HookingConcern, MetaConcern, ObjectiveConcern, RewardConcern, UserContextedConcern
Defined in:
lib/adhonorem/models/badge.rb

Overview

AdHonorem core class, must be inherited from

Instance Attribute Summary

Attributes included from ObjectiveConcern

#objectives

Attributes included from UserContextedConcern

#user

Instance Method Summary collapse

Methods included from RewardConcern

#add_reward

Methods included from MetaConcern

#meta?

Methods included from ObjectiveConcern

#add_objective, #objectives_count

Methods included from UserContextedConcern

#check_context, #reload_context!, #set_context

Constructor Details

#initializeBadge

Returns a new instance of Badge.



14
15
16
17
18
19
20
# File 'lib/adhonorem/models/badge.rb', line 14

def initialize
  super

  @sub_badges = []
  @rewards    = []
  @objectives = {}
end

Instance Method Details

#complete?(objective = nil) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/adhonorem/models/badge.rb', line 51

def complete?(objective = nil)
  # Meta badges don't handle per-objective completion
  return complete_meta? if meta?
  check_context
  selection = objectives.keys
  selection.select! { |slug| slug == objective } if objective
  selection.each do |slug|
    objective = objectives[slug]
    return false unless get_progression_for(objective).done?
  end
  true
end

#get_objective(objective_slug) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/adhonorem/models/badge.rb', line 64

def get_objective(objective_slug)
  objective = objective_slug
  unless objective.is_a?(AdHonorem::Objective)
    objective = objectives[objective_slug]
    err = "Objective #{objective_slug} could not be found"
    raise ObjectiveNotFound, err unless objective
  end

  objective
end

#legacy?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/adhonorem/models/badge.rb', line 37

def legacy?
  legacy || false
end

#progress(progress_type = :step) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/adhonorem/models/badge.rb', line 22

def progress(progress_type = :step)
  check_context
  return progress_meta(progress_type) if meta?

  case progress_type
  when :step
    objectives.keys.map do |slug|
      name = objectives[slug].name
      "#{name} : #{get_progression_for(slug).progress(:stringified)}"
    end
  when :global
    objectives.keys.map { |slug| get_progression_for(slug).progress(:percentage) }.sum / objectives_count
  end
end

#trigger(objective_slug, params = nil) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/adhonorem/models/badge.rb', line 41

def trigger(objective_slug, params = nil)
  return trigger_meta(objective_slug, params || {}) if meta?

  return :already_done if complete?(objective_slug) || complete?
  return :legacy_badge if legacy?

  params ||= {}
  proceed(objective_slug, params)
end