piece 0.1.2 → 0.1.3
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.
- checksums.yaml +4 -4
- data/.travis.yml +0 -1
- data/README.md +3 -3
- data/TODO +7 -0
- data/lib/piece/rules.rb +70 -0
- data/lib/piece/version.rb +1 -1
- data/lib/piece.rb +3 -3
- data/piece.gemspec +1 -1
- metadata +6 -4
- data/lib/piece/pieces.rb +0 -70
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5876eb39c8e8bec6130a3855658854f15d5ff385
|
4
|
+
data.tar.gz: b25527aa9e5f9ccbef6873325a6f3f66063137d5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 281df8890ffa99a0aa7ac8872e3309cb7fc3c3a4608d4ace604f2a9906f5b60626badd9befb2df897517bd1fb2fb114c2ad34fa14fbc28a4cdf8140cfe323cb8
|
7
|
+
data.tar.gz: d4206dc8301e95c7d569e8fdf2f07964b376caf1c5be403589086716a10b750c21ecdbf0f922cb5a1239cd23b48df8d0f933e6eac56aa526f6cf70b5261ca8b6
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -22,14 +22,14 @@ Or install it yourself as:
|
|
22
22
|
|
23
23
|
## Usage
|
24
24
|
|
25
|
-
|
25
|
+
See test/piece_test.rb
|
26
26
|
|
27
27
|
## Development
|
28
28
|
|
29
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
30
|
|
31
31
|
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
32
|
|
33
33
|
## Contributing
|
34
34
|
|
35
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/ThoughtWorksStudios/piece.
|
data/TODO
ADDED
data/lib/piece/rules.rb
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'piece/expression.tab'
|
2
|
+
|
3
|
+
module Piece
|
4
|
+
class InvalidAction < StandardError
|
5
|
+
end
|
6
|
+
|
7
|
+
class Rules
|
8
|
+
def initialize(data)
|
9
|
+
@data = data
|
10
|
+
end
|
11
|
+
|
12
|
+
def match?(action)
|
13
|
+
!!get(@data, parts(action))
|
14
|
+
end
|
15
|
+
|
16
|
+
def [](action)
|
17
|
+
get(@data, parts(action))
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
def parts(action)
|
22
|
+
raise InvalidAction, "Should not include '*' in a rule" if action.include?('*')
|
23
|
+
action.split(':')
|
24
|
+
end
|
25
|
+
|
26
|
+
def apply(group, actions)
|
27
|
+
case group
|
28
|
+
when ExpressionParser::Exp
|
29
|
+
case group.op
|
30
|
+
when '+'
|
31
|
+
apply(group.left, actions) || apply(group.right, actions)
|
32
|
+
when '-'
|
33
|
+
apply(group.left, actions) && apply(group.right, actions).nil?
|
34
|
+
else
|
35
|
+
raise "Unknown operator: #{group.op}"
|
36
|
+
end
|
37
|
+
when ExpressionParser::Id
|
38
|
+
if @data.has_key?(group.val)
|
39
|
+
get(@data[group.val], actions)
|
40
|
+
else
|
41
|
+
get(group, actions)
|
42
|
+
end
|
43
|
+
else
|
44
|
+
raise "Unknown type: #{group.inspect}"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def get(data, actions)
|
49
|
+
return data.nil? ? nil : Array(data) if actions.empty?
|
50
|
+
case data
|
51
|
+
when Hash
|
52
|
+
get(data[actions.first], actions[1..-1])
|
53
|
+
when Array
|
54
|
+
get(data.first, actions) || get(data[1..-1], actions)
|
55
|
+
when NilClass
|
56
|
+
nil
|
57
|
+
when ExpressionParser::Id
|
58
|
+
_match_?(data.val, actions.first) ? '*' : nil
|
59
|
+
when String
|
60
|
+
apply(ExpressionParser.new.parse(data), actions)
|
61
|
+
else
|
62
|
+
raise "Unknown type: #{group.inspect}"
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def _match_?(a, b)
|
67
|
+
a == b || a.nil? || a == '*' || b == '*'
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
data/lib/piece/version.rb
CHANGED
data/lib/piece.rb
CHANGED
data/piece.gemspec
CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.email = ["swing1979@gmail.com"]
|
11
11
|
|
12
12
|
spec.summary = %q{User privileges and feature toggle}
|
13
|
-
spec.description = %q{
|
13
|
+
spec.description = %q{A model based on Rails controller actions to make user privileges and feature toggle simpler}
|
14
14
|
spec.homepage = "https://github.com/ThoughtWorksStudios/piece"
|
15
15
|
|
16
16
|
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: piece
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Xiao Li
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-06-
|
11
|
+
date: 2015-06-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -66,7 +66,8 @@ dependencies:
|
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '1.4'
|
69
|
-
description:
|
69
|
+
description: A model based on Rails controller actions to make user privileges and
|
70
|
+
feature toggle simpler
|
70
71
|
email:
|
71
72
|
- swing1979@gmail.com
|
72
73
|
executables: []
|
@@ -79,12 +80,13 @@ files:
|
|
79
80
|
- Gemfile
|
80
81
|
- README.md
|
81
82
|
- Rakefile
|
83
|
+
- TODO
|
82
84
|
- bin/console
|
83
85
|
- bin/setup
|
84
86
|
- lib/piece.rb
|
85
87
|
- lib/piece/expression.tab.rb
|
86
88
|
- lib/piece/expression.y
|
87
|
-
- lib/piece/
|
89
|
+
- lib/piece/rules.rb
|
88
90
|
- lib/piece/version.rb
|
89
91
|
- piece.gemspec
|
90
92
|
homepage: https://github.com/ThoughtWorksStudios/piece
|
data/lib/piece/pieces.rb
DELETED
@@ -1,70 +0,0 @@
|
|
1
|
-
require 'piece/expression.tab'
|
2
|
-
|
3
|
-
module Piece
|
4
|
-
class InvalidKey < StandardError
|
5
|
-
end
|
6
|
-
|
7
|
-
class Pieces
|
8
|
-
def initialize(data)
|
9
|
-
@data = data
|
10
|
-
end
|
11
|
-
|
12
|
-
def has?(key)
|
13
|
-
!!get(@data, parts(key))
|
14
|
-
end
|
15
|
-
|
16
|
-
def [](key)
|
17
|
-
get(@data, parts(key))
|
18
|
-
end
|
19
|
-
|
20
|
-
private
|
21
|
-
def parts(key)
|
22
|
-
raise InvalidKey, "Should not include '*' in a key" if key.include?('*')
|
23
|
-
key.split(':')
|
24
|
-
end
|
25
|
-
|
26
|
-
def apply(group, keys)
|
27
|
-
case group
|
28
|
-
when ExpressionParser::Exp
|
29
|
-
case group.op
|
30
|
-
when '+'
|
31
|
-
apply(group.left, keys) || apply(group.right, keys)
|
32
|
-
when '-'
|
33
|
-
apply(group.left, keys) && apply(group.right, keys).nil?
|
34
|
-
else
|
35
|
-
raise "Unknown operator: #{group.op}"
|
36
|
-
end
|
37
|
-
when ExpressionParser::Id
|
38
|
-
if @data.has_key?(group.val)
|
39
|
-
get(@data[group.val], keys)
|
40
|
-
else
|
41
|
-
get(group, keys)
|
42
|
-
end
|
43
|
-
else
|
44
|
-
raise "Unknown type: #{group.inspect}"
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
def get(data, keys)
|
49
|
-
return data.nil? ? nil : Array(data) if keys.empty?
|
50
|
-
case data
|
51
|
-
when Hash
|
52
|
-
get(data[keys.first], keys[1..-1])
|
53
|
-
when Array
|
54
|
-
get(data.first, keys) || get(data[1..-1], keys)
|
55
|
-
when NilClass
|
56
|
-
nil
|
57
|
-
when ExpressionParser::Id
|
58
|
-
match?(data.val, keys.first) ? '*' : nil
|
59
|
-
when String
|
60
|
-
apply(ExpressionParser.new.parse(data), keys)
|
61
|
-
else
|
62
|
-
raise "Unknown type: #{group.inspect}"
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
def match?(a, b)
|
67
|
-
a == b || a.nil? || a == '*' || b == '*'
|
68
|
-
end
|
69
|
-
end
|
70
|
-
end
|