rflags 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cf27a90e02da78826dbd62e502b922adf03603b0
4
+ data.tar.gz: ca3cf6a07354aa10ea55db8debb8c6d8d6cc9198
5
+ SHA512:
6
+ metadata.gz: 21bb9423b1983708393e03b3f19d10201c1c7f8b029824823b0b5a1acc278f6dbd5ab96bfd2f4b90738101d27ee385aba0ac27f08059c7fe0ffc275292553fd2
7
+ data.tar.gz: 2c4a4b36f5350a8174e7cb448fce1cb44b05b03553b8cfa994c0ed3ef25ac6531c7c45371a87e16ae9ec36551cfa53499729a0788e0f3e51dbc3e2274e02b4ec
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Michael
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
@@ -0,0 +1,10 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs << 'test'
5
+ t.ruby_opts << '-rtest_helper -rrflags'
6
+ t.test_files = FileList['test/test*.rb']
7
+ t.verbose = true
8
+ end
9
+
10
+ task default: :test
@@ -0,0 +1,43 @@
1
+ require 'rflags/flag'
2
+ require 'rflags/file_flag'
3
+ require 'rflags/redis_flag'
4
+
5
+
6
+ module RFlags
7
+ def flag(definition)
8
+ case definition
9
+ when Symbol, String
10
+ define_flag(definition, RFlags::Flag.new)
11
+ when Hash
12
+ definition.each do |name, backend|
13
+ define_flag(name, resolve_flag_backend(backend))
14
+ end
15
+ else
16
+ raise TypeError,
17
+ "RFlags#flag expects Symbol, String or Hash, but have #{definition.class}"
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def define_flag(name, backend)
24
+ instance_variable_set(:"@#{name}", backend)
25
+ instance_eval(<<-MTD, __FILE__, __LINE__)
26
+ def #{name}; @#{name}; end
27
+ MTD
28
+ end
29
+
30
+ def resolve_flag_backend(backend)
31
+ case backend
32
+ when String
33
+ RFlags::FileFlag.new(backend)
34
+ when Array
35
+ ::Object.const_get("::RFlags::#{backend[0].class}Flag")
36
+ .new(*backend)
37
+ else
38
+ raise TypeError,
39
+ "Wrong backend definition for RFlags#flag.\n" <<
40
+ "Backend should be instance of String or Array"
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,21 @@
1
+ module RFlags
2
+ class FileFlag
3
+ attr_reader :path
4
+
5
+ def initialize(path)
6
+ @path = path
7
+ end
8
+
9
+ def set?
10
+ File.file?(path)
11
+ end
12
+
13
+ def set
14
+ File.write(path, '')
15
+ end
16
+
17
+ def reset
18
+ File.delete(path) if File.file?(path)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,19 @@
1
+ module RFlags
2
+ class Flag
3
+ def initialize
4
+ @state = false
5
+ end
6
+
7
+ def set?
8
+ @state
9
+ end
10
+
11
+ def reset
12
+ @state = false
13
+ end
14
+
15
+ def set
16
+ @state = true
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,22 @@
1
+ module RFlags
2
+ class RedisFlag
3
+ attr_reader :key, :redis
4
+
5
+ def initialize(redis, key)
6
+ @redis = redis
7
+ @key = key
8
+ end
9
+
10
+ def set?
11
+ !redis.get(key).nil?
12
+ end
13
+
14
+ def set
15
+ redis.set(key, '')
16
+ end
17
+
18
+ def reset
19
+ redis.del(key)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,12 @@
1
+ class TestFileFlag < Minitest::Test
2
+ def setup
3
+ @flag = RFlags::FileFlag.new('tmp/file_flag')
4
+ FileUtils.rm_f('tmp/file_flag')
5
+ end
6
+
7
+ def teardown
8
+ FileUtils.rm_f('tmp/file_flag')
9
+ end
10
+
11
+ include FlagAssetions
12
+ end
@@ -0,0 +1,7 @@
1
+ class TestFlag < Minitest::Test
2
+ def setup
3
+ @flag = RFlags::Flag.new
4
+ end
5
+
6
+ include FlagAssetions
7
+ end
@@ -0,0 +1,26 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ Bundler.setup
4
+ Bundler.require
5
+ require 'minitest/autorun'
6
+ require 'minitest/test'
7
+ require 'minitest/mock'
8
+
9
+ module FlagAssetions
10
+ def test_it_is_unset_by_default
11
+ assert_equal false, @flag.set?
12
+ end
13
+
14
+ def test_it_can_be_set
15
+ assert_equal false, @flag.set?
16
+ @flag.set
17
+ assert_equal true, @flag.set?
18
+ end
19
+
20
+ def test_it_can_be_reset
21
+ @flag.set
22
+ assert_equal true, @flag.set?
23
+ @flag.reset
24
+ assert_equal false, @flag.set?
25
+ end
26
+ end
@@ -0,0 +1,25 @@
1
+ class TestRedisFlag < Minitest::Test
2
+ class RedisMock
3
+ def initialize
4
+ @store = {}
5
+ end
6
+
7
+ def get(key)
8
+ @store[key]
9
+ end
10
+
11
+ def set(key, val)
12
+ @store[key] = val
13
+ end
14
+
15
+ def del(key)
16
+ @store.delete(key)
17
+ end
18
+ end
19
+
20
+ def setup
21
+ @flag = RFlags::RedisFlag.new(RedisMock.new, 'key')
22
+ end
23
+
24
+ include FlagAssetions
25
+ end
@@ -0,0 +1,28 @@
1
+ class TestRFlags < Minitest::Test
2
+ Redis = ::Minitest::Mock.new
3
+ Redis.expect(:class, 'Redis')
4
+
5
+ class Flags
6
+ extend RFlags
7
+
8
+ flag :flag_a
9
+ flag :flag_b => 'tmp/flag_b'
10
+ flag :flag_c => [Redis, 'key']
11
+ end
12
+
13
+ def test_it_defines_flag_method
14
+ assert Flags.respond_to?(:flag), 'Flags should have .flag method'
15
+ end
16
+
17
+ def test_it_defines_instance_variable_flag
18
+ assert_instance_of RFlags::Flag, Flags.flag_a
19
+ end
20
+
21
+ def test_it_defines_file_flag
22
+ assert_instance_of RFlags::FileFlag, Flags.flag_b
23
+ end
24
+
25
+ def test_it_defines_redis_flag
26
+ assert_instance_of RFlags::RedisFlag, Flags.flag_c
27
+ end
28
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rflags
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Michael Lutsiuk
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '10.2'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '10.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '5.5'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '5.5'
41
+ description: Stores boolean flags in file, redis or memory
42
+ email: mmaccoffe@gmail.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - Gemfile
48
+ - LICENSE
49
+ - Rakefile
50
+ - lib/rflags.rb
51
+ - lib/rflags/file_flag.rb
52
+ - lib/rflags/flag.rb
53
+ - lib/rflags/redis_flag.rb
54
+ - test/test_file_flag.rb
55
+ - test/test_flag.rb
56
+ - test/test_helper.rb
57
+ - test/test_redis_flag.rb
58
+ - test/test_rflags.rb
59
+ homepage: https://github.com/mediaslave24/rflags
60
+ licenses:
61
+ - MIT
62
+ metadata: {}
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubyforge_project:
79
+ rubygems_version: 2.4.5
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: Boolean flags with different backends
83
+ test_files: []