Class: Badgeable::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/badgeable/config.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



10
11
12
13
# File 'lib/badgeable/config.rb', line 10

def initialize
  @conditions_array = [Proc.new {true}]
  subject :user
end

Class Attribute Details

.badge_definitionsObject

Returns the value of attribute badge_definitions.



5
6
7
# File 'lib/badgeable/config.rb', line 5

def badge_definitions
  @badge_definitions
end

Instance Attribute Details

#conditions_arrayObject (readonly)

Returns the value of attribute conditions_array.



8
9
10
# File 'lib/badgeable/config.rb', line 8

def conditions_array
  @conditions_array
end

#klassObject (readonly)

Returns the value of attribute klass.



8
9
10
# File 'lib/badgeable/config.rb', line 8

def klass
  @klass
end

#subject_procObject (readonly)

Returns the value of attribute subject_proc.



8
9
10
# File 'lib/badgeable/config.rb', line 8

def subject_proc
  @subject_proc
end

#thresholdObject (readonly)

Returns the value of attribute threshold.



8
9
10
# File 'lib/badgeable/config.rb', line 8

def threshold
  @threshold
end

Instance Method Details

#conditions(&block) ⇒ Object



51
52
53
54
55
# File 'lib/badgeable/config.rb', line 51

def conditions(&block)
  @conditions_array << Proc.new {|instance|
    block.call(instance)
  }
end

#count(n = 1, &block) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/badgeable/config.rb', line 32

def count(n = 1, &block)
  if block_given?
    count_proc = Proc.new { |instance|
      block.call(instance)
    }
  else
    count_proc = Proc.new { |instance|
      instance.instance_eval %Q{
        #{klass}.where(:#{@subject_name}_id => #{@subject_name}.id).count >= #{n}
      }
    }
  end
  @conditions_array << count_proc
end

#description(*args) ⇒ Object

Raises:

  • (ArgumentError)


66
67
68
69
70
71
72
73
# File 'lib/badgeable/config.rb', line 66

def description(*args)
  raise ArgumentError unless args.length <= 1
  if args.length == 1
    @description = args[0]
  else
    @description
  end
end

#icon(*args) ⇒ Object

Raises:

  • (ArgumentError)


57
58
59
60
61
62
63
64
# File 'lib/badgeable/config.rb', line 57

def icon(*args)
  raise ArgumentError unless args.length <= 1
  if args.length == 1
    @icon = args[0]
  else
    @icon
  end
end

#subject(sym = nil, &block) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/badgeable/config.rb', line 15

def subject(sym = nil, &block)
  @subject_name = sym
  if block_given?
    @subject_proc = Proc.new {|instance|
      block.call(instance)
    }
  else
    @subject_proc = Proc.new { |instance|
      if klass.to_s.underscore.to_sym == @subject_name
        instance
      else
        instance.send(sym)
      end
    }
  end
end

#thing(klass) ⇒ Object



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

def thing(klass)
  @klass = klass
end