Class: Envoku::Feature

Inherits:
Object
  • Object
show all
Defined in:
lib/envoku/feature.rb

Constant Summary collapse

ENV_NAMESPACE =
"ENVOKU_FEATURE_"
REDIS_NAMESPACE =
"envoku:features:"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Feature


30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/envoku/feature.rb', line 30

def initialize(name)
  @name = name
  @enabled = false
  @attributes = {}
  @description = nil
  var_string = ENV["#{ENV_NAMESPACE}#{name}"]
  return nil unless var_string
  @attributes = ::YAML.safe_load(var_string) rescue {}
  @description = @attributes['description']
  @enabled = !!@attributes['enabled']
  @attributes.delete('description')
  @attributes.delete('enabled')
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes


21
22
23
# File 'lib/envoku/feature.rb', line 21

def attributes
  @attributes
end

#descriptionObject (readonly)

Returns the value of attribute description


20
21
22
# File 'lib/envoku/feature.rb', line 20

def description
  @description
end

#nameObject (readonly)

Returns the value of attribute name


19
20
21
# File 'lib/envoku/feature.rb', line 19

def name
  @name
end

Class Method Details

.allObject


23
24
25
26
27
28
# File 'lib/envoku/feature.rb', line 23

def self.all
  keys = ENV.select { |key, value| key.index(ENV_NAMESPACE) == 0 }
  keys.keys.map do |key|
    Feature.new(key[(ENV_NAMESPACE.length)..-1])
  end
end

Instance Method Details

#disable_for!(resource) ⇒ Object


61
62
63
64
65
66
67
# File 'lib/envoku/feature.rb', line 61

def disable_for!(resource)
  Envoku.logger.warn("feature #{name} is not permitted for #{resource.class.name}") unless permitted_for?(resource.class.name)
  Envoku.redis.multi do
    Envoku.redis.del("#{REDIS_NAMESPACE}#{@name}:#{resource.class.name}")
    Envoku.redis.del("#{REDIS_NAMESPACE}#{resource.class.name}:#{resource.id}")
  end
end

#enable_for!(resource) ⇒ Object


53
54
55
56
57
58
59
# File 'lib/envoku/feature.rb', line 53

def enable_for!(resource)
  Envoku.logger.warn("feature #{name} is not permitted for #{resource.class.name}") unless permitted_for?(resource.class.name)
  Envoku.redis.multi do
    Envoku.redis.sadd("#{REDIS_NAMESPACE}#{@name}:#{resource.class.name}", resource.id.to_s)
    Envoku.redis.sadd("#{REDIS_NAMESPACE}#{resource.class.name}:#{resource.id}", @name)
  end
end

#enabled?Boolean


44
45
46
# File 'lib/envoku/feature.rb', line 44

def enabled?
  @enabled
end

#enabled_for?(resource) ⇒ Boolean


48
49
50
51
# File 'lib/envoku/feature.rb', line 48

def enabled_for?(resource)
  return true if enabled?
  Envoku.redis.sismember("#{REDIS_NAMESPACE}#{@name}:#{resource.class.name}", "#{resource.id}")
end

#permitted_for?(resource_klass) ⇒ Boolean


73
74
75
76
77
# File 'lib/envoku/feature.rb', line 73

def permitted_for?(resource_klass)
  return true unless @attributes.has_key?('permitted_resources')
  resource_klass = resource_klass.class.name unless resource_klass.is_a?(String)
  @attributes['permitted_resources'].split(',').include?(resource_klass)
end

#resourcesObject


79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/envoku/feature.rb', line 79

def resources
  list = []
  feature_prefix = "#{REDIS_NAMESPACE}#{@name}"
  klasses = Envoku.redis.keys("#{feature_prefix}:*").map { |klass| klass[(feature_prefix.length + 1)..-1] }
  klasses.each do |klass|
    ids = Envoku.redis.smembers("#{feature_prefix}:#{klass}")
    ids.each do |id|
      list.push("#{klass}:#{id}")
    end
  end
  list
end

#toggle_for!(resource) ⇒ Object


69
70
71
# File 'lib/envoku/feature.rb', line 69

def toggle_for!(resource)
  enabled_for?(resource) ? disable_for!(resource) : enable_for!(resource)
end