Module: Redis::FeatureControl

Defined in:
lib/redis/feature_control.rb,
lib/redis/feature_control/version.rb

Defined Under Namespace

Classes: UnknownFeatureError

Constant Summary collapse

Version =
'0.0.2'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.featuresObject

Returns the value of attribute features.



23
24
25
# File 'lib/redis/feature_control.rb', line 23

def features
  @features
end

Class Method Details

.check_feature!(feature) ⇒ Object



118
119
120
# File 'lib/redis/feature_control.rb', line 118

def check_feature!(feature)
  raise Redis::FeatureControl::UnknownFeatureError unless features.include?(feature.to_sym)
end

.connection=(redis) ⇒ Object

Set the redis instance directly.



44
45
46
# File 'lib/redis/feature_control.rb', line 44

def connection=(redis)
  @redis = redis
end

.connection_string=(value) ⇒ Object

Redis::FeatureControl



38
39
40
41
# File 'lib/redis/feature_control.rb', line 38

def connection_string=(value)
  @host, @port = value.split(":", 2)
  redis_connect!
end

.disable!(feature) ⇒ Object



91
92
93
94
95
96
97
98
99
100
# File 'lib/redis/feature_control.rb', line 91

def disable!(feature)
  check_feature!(feature)
  if mock?
    mock_feature_hash[feature.to_s] = false
  else
    redis.set(feature.to_s, 0)
  end
rescue Errno::ECONNREFUSED
  # Ignore
end

.disabled?(feature) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
67
# File 'lib/redis/feature_control.rb', line 64

def disabled?(feature)
  check_feature!(feature)
  !enabled?(feature)
end

.enable!(feature) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/redis/feature_control.rb', line 80

def enable!(feature)
  check_feature!(feature)
  if mock?
    mock_feature_hash[feature.to_s] = true
  else
    redis.set(feature.to_s, 1)
  end
rescue Errno::ECONNREFUSED
  # Ignore
end

.enabled?(feature) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
72
73
74
75
76
77
78
# File 'lib/redis/feature_control.rb', line 69

def enabled?(feature)
  check_feature!(feature)
  if mock?
    mock_feature_hash[feature.to_s].nil? || true == mock_feature_hash[feature.to_s]
  else
    (redis.get(feature.to_s) || 1).to_i == 1
  end
rescue Errno::ECONNREFUSED
  true # default to enabled if we can't connect to redis
end

.hostObject



29
30
31
# File 'lib/redis/feature_control.rb', line 29

def host
  @host ||= 'localhost'
end

.mock!Object

This redfines enabled/disabled to only use class variables instead of connecting to redis. This allows you to run tests w/out a connection to redis



124
125
126
# File 'lib/redis/feature_control.rb', line 124

def mock!
  @mock = true
end

.mock?Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/redis/feature_control.rb', line 132

def mock?
  @mock
end

.mock_feature_hashObject



136
137
138
# File 'lib/redis/feature_control.rb', line 136

def mock_feature_hash
  @feature_hash ||= {}
end

.portObject



33
34
35
# File 'lib/redis/feature_control.rb', line 33

def port
  @port ||= 6379
end

.redisObject



54
55
56
57
58
59
60
61
62
# File 'lib/redis/feature_control.rb', line 54

def redis
  if @redis.nil?
    begin
      redis_connect!
    rescue Errno::ECONNREFUSED
    end
  end
  @redis
end

.redis_connect!Object

Connects to redis on the current host/port and sets the redis object



49
50
51
52
# File 'lib/redis/feature_control.rb', line 49

def redis_connect!
  redis = Redis.new(:host => host, :port => port, :thread_safe => true)
  @redis = Redis::Namespace.new(:feature_control, :redis => redis)
end

.set_status(feature, value) ⇒ Object

value >=1 enable the feature value <1 disable the feature



104
105
106
107
108
109
110
111
# File 'lib/redis/feature_control.rb', line 104

def set_status(feature, value)
  value = value.to_i
  if value >= 1
    enable!(feature)
  else
    disable!(feature)
  end
end

.state(feature) ⇒ Object

Returns a string for the state of the feature



114
115
116
# File 'lib/redis/feature_control.rb', line 114

def state(feature)
  enabled?(feature) ? 'enabled' : 'disabled'
end

.unmock!Object



128
129
130
# File 'lib/redis/feature_control.rb', line 128

def unmock!
  @mock = false
end