Class: Merit::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/merit/rule.rb

Overview

Rules has a badge name and level, a target to badge, a conditions block and a temporary option. Could split this class between badges and rankings functionality

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#badge_idObject

Returns the value of attribute badge_id.



6
7
8
# File 'lib/merit/rule.rb', line 6

def badge_id
  @badge_id
end

#badge_nameObject

Returns the value of attribute badge_name.



6
7
8
# File 'lib/merit/rule.rb', line 6

def badge_name
  @badge_name
end

#blockObject

Returns the value of attribute block.



6
7
8
# File 'lib/merit/rule.rb', line 6

def block
  @block
end

#categoryObject

Returns the value of attribute category.



6
7
8
# File 'lib/merit/rule.rb', line 6

def category
  @category
end

#levelObject

Returns the value of attribute level.



6
7
8
# File 'lib/merit/rule.rb', line 6

def level
  @level
end

#level_nameObject

Returns the value of attribute level_name.



6
7
8
# File 'lib/merit/rule.rb', line 6

def level_name
  @level_name
end

#model_nameObject

Returns the value of attribute model_name.



6
7
8
# File 'lib/merit/rule.rb', line 6

def model_name
  @model_name
end

#multipleObject

Returns the value of attribute multiple.



6
7
8
# File 'lib/merit/rule.rb', line 6

def multiple
  @multiple
end

#scoreObject

Returns the value of attribute score.



6
7
8
# File 'lib/merit/rule.rb', line 6

def score
  @score
end

#temporaryObject

Returns the value of attribute temporary.



6
7
8
# File 'lib/merit/rule.rb', line 6

def temporary
  @temporary
end

#toObject

Returns the value of attribute to.



6
7
8
# File 'lib/merit/rule.rb', line 6

def to
  @to
end

Instance Method Details

#applies?(target_obj = nil) ⇒ Boolean

Does this rule’s condition block apply?

Returns:

  • (Boolean)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/merit/rule.rb', line 10

def applies?(target_obj = nil)
  return true if block.nil? # no block given: always true

  case block.arity
  when 1 # Expects target object
    if target_obj.present?
      block.call(target_obj)
    else
      Rails.logger.warn '[merit] no target_obj found on Rule#applies?'
      false
    end
  when 0
    block.call
  end
end

#badgeObject

Get rule’s related Badge.



27
28
29
30
31
32
33
# File 'lib/merit/rule.rb', line 27

def badge
  if badge_id
    Merit::Badge.find(badge_id)
  else
    Merit::Badge.find_by_name_and_level(badge_name, level)
  end
end