2013-03-01 20:07:44 +08:00
|
|
|
require_dependency 'enum'
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-09-05 06:42:18 +08:00
|
|
|
class InvalidTrustLevel < StandardError; end
|
2013-03-01 20:07:44 +08:00
|
|
|
class TrustLevel
|
2013-02-06 03:16:51 +08:00
|
|
|
attr_reader :id, :name
|
|
|
|
|
2013-02-28 21:08:56 +08:00
|
|
|
class << self
|
2014-09-05 06:42:18 +08:00
|
|
|
|
2014-09-05 13:20:39 +08:00
|
|
|
def[](level)
|
|
|
|
if !valid?(level)
|
2014-09-05 06:42:18 +08:00
|
|
|
raise InvalidTrustLevel
|
|
|
|
end
|
2014-09-05 13:20:39 +08:00
|
|
|
level
|
2014-09-05 06:42:18 +08:00
|
|
|
end
|
|
|
|
|
2013-02-28 21:08:56 +08:00
|
|
|
def levels
|
2013-03-01 20:07:44 +08:00
|
|
|
@levels ||= Enum.new(
|
2013-04-18 07:11:13 +08:00
|
|
|
:newuser, :basic, :regular, :leader, :elder, start: 0
|
2013-03-01 20:07:44 +08:00
|
|
|
)
|
2013-02-28 21:08:56 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def all
|
|
|
|
levels.map do |name_key, id|
|
|
|
|
TrustLevel.new(name_key, id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-09-05 13:20:39 +08:00
|
|
|
def valid?(level)
|
|
|
|
valid_range === level
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid_range
|
|
|
|
(0..4)
|
2013-02-28 21:08:56 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def compare(current_level, level)
|
2014-09-05 13:20:39 +08:00
|
|
|
(current_level || 0) >= level
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(name_key, id)
|
2013-02-26 00:42:20 +08:00
|
|
|
@name = I18n.t("trust_levels.#{name_key}.title")
|
2013-02-06 03:16:51 +08:00
|
|
|
@id = id
|
|
|
|
end
|
|
|
|
|
|
|
|
def serializable_hash
|
2013-02-28 21:08:56 +08:00
|
|
|
{ id: @id, name: @name }
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|