Sha256: 92897433d8b314fb211115efce3d950970622c558aeea039858b86e93715c8f3

Contents?: true

Size: 985 Bytes

Versions: 29

Compression:

Stored size: 985 Bytes

Contents

module Merit
  # 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
  class Rule
    attr_accessor :badge_id, :badge_name, :level, :to, :model_name, :level_name,
                  :multiple, :temporary, :score, :block, :category

    # Does this rule's condition block apply?
    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

    # Get rule's related Badge.
    def badge
      if badge_id
        Merit::Badge.find(badge_id)
      else
        Merit::Badge.find_by_name_and_level(badge_name, level)
      end
    end
  end
end

Version data entries

29 entries across 29 versions & 2 rubygems

Version Path
merit-4.0.3 lib/merit/rule.rb
qalam_merit-4.5.12 lib/merit/rule.rb
merit-4.0.2 lib/merit/rule.rb
qalam_merit-4.5.11 lib/merit/rule.rb
qalam_merit-4.5.10 lib/merit/rule.rb
qalam_merit-4.5.09 lib/merit/rule.rb
qalam_merit-4.5.08 lib/merit/rule.rb
qalam_merit-4.5.07 lib/merit/rule.rb
qalam_merit-4.5.06 lib/merit/rule.rb
qalam_merit-4.5.05 lib/merit/rule.rb
qalam_merit-4.5.04 lib/merit/rule.rb
qalam_merit-4.5.03 lib/merit/rule.rb
qalam_merit-4.5.02 lib/merit/rule.rb
qalam_merit-4.5.01 lib/merit/rule.rb
qalam_merit-4.5.00 lib/merit/rule.rb
qalam_merit-4.0.4 lib/merit/rule.rb
qalam_merit-4.0.35 lib/merit/rule.rb
merit-4.0.1 lib/merit/rule.rb
merit-4.0.0 lib/merit/rule.rb
merit-3.0.3 lib/merit/rule.rb