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

- (Object) badge_name

Returns the value of attribute badge_name



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

def badge_name
  @badge_name
end

- (Object) block

Returns the value of attribute block



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

def block
  @block
end

- (Object) category

Returns the value of attribute category



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

def category
  @category
end

- (Object) level

Returns the value of attribute level



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

def level
  @level
end

- (Object) level_name

Returns the value of attribute level_name



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

def level_name
  @level_name
end

- (Object) model_name

Returns the value of attribute model_name



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

def model_name
  @model_name
end

- (Object) multiple

Returns the value of attribute multiple



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

def multiple
  @multiple
end

- (Object) score

Returns the value of attribute score



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

def score
  @score
end

- (Object) temporary

Returns the value of attribute temporary



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

def temporary
  @temporary
end

- (Object) to

Returns the value of attribute to



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

def to
  @to
end

Instance Method Details

- (Boolean) applies?(target_obj = nil)

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

- (Object) badge

Get rule's related Badge.



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

def badge
  @badge ||= Merit::Badge.find_by_name_and_level(badge_name, level)
end