piece 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 289a5f8c1c137ad1391abf6d9087ef97a4ae67f7
4
- data.tar.gz: 405dedb61e347a8f317b72235b34b4fc80f3e18c
3
+ metadata.gz: 5876eb39c8e8bec6130a3855658854f15d5ff385
4
+ data.tar.gz: b25527aa9e5f9ccbef6873325a6f3f66063137d5
5
5
  SHA512:
6
- metadata.gz: f2501af98f9d4e3a29ae02cd6dee95df081d2705311a48e8e9b524e406399260d80936422813c9b118161ec3a8ce70cd6469e100b330b9b3a21d8fd757233bcc
7
- data.tar.gz: 05d2ec120b5289e5886e6824770b0062b78903549fd9283b42ce114eeeea60180a99d750772cfea3660936a32590586b6e60ba0aece8746480de4e1492e5bb78
6
+ metadata.gz: 281df8890ffa99a0aa7ac8872e3309cb7fc3c3a4608d4ace604f2a9906f5b60626badd9befb2df897517bd1fb2fb114c2ad34fa14fbc28a4cdf8140cfe323cb8
7
+ data.tar.gz: d4206dc8301e95c7d569e8fdf2f07964b376caf1c5be403589086716a10b750c21ecdbf0f922cb5a1239cd23b48df8d0f933e6eac56aa526f6cf70b5261ca8b6
data/.travis.yml CHANGED
@@ -3,7 +3,6 @@ env:
3
3
  - JRUBY_OPTS="$JRUBY_OPTS --debug"
4
4
  gemfile:
5
5
  - Gemfile
6
- - Gemfile.rack-1.3.x
7
6
  language: ruby
8
7
  rvm:
9
8
  # - 1.8.7
data/README.md CHANGED
@@ -22,14 +22,14 @@ Or install it yourself as:
22
22
 
23
23
  ## Usage
24
24
 
25
- TODO: Write usage instructions here
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 false` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
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/[USERNAME]/piece.
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ThoughtWorksStudios/piece.
data/TODO ADDED
@@ -0,0 +1,7 @@
1
+ * load rules by string format: group:group:*, group:group:action
2
+ * append rules
3
+ * serialize to yaml
4
+ * save to DB
5
+ * delete piece by key: group:group:*
6
+ * print reason: why has? respond true / false
7
+ * print all matching rules given a key
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Piece
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
data/lib/piece.rb CHANGED
@@ -1,11 +1,11 @@
1
1
  require 'yaml'
2
2
 
3
3
  require "piece/version"
4
- require 'piece/pieces'
4
+ require 'piece/rules'
5
5
 
6
6
  module Piece
7
7
  module_function
8
- def load(pieces)
9
- Pieces.new(YAML.load(pieces))
8
+ def load(rules)
9
+ Rules.new(YAML.load(rules))
10
10
  end
11
11
  end
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{It's time to make user privileges and feature toggle simpler}
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.2
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-03 00:00:00.000000000 Z
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: It's time to make user privileges and feature toggle simpler
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/pieces.rb
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