piece 0.1.0 → 0.1.1

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: 6a32b1e236bdb20a821224472acb68f2252731dd
4
- data.tar.gz: f90826f899d653027275792b73a1c5635341c305
3
+ metadata.gz: 6c0e382628e754ff3f582c807b4bf0eaa886c7b7
4
+ data.tar.gz: 0a47754336996a600d5bc2aab010474c0eef05d1
5
5
  SHA512:
6
- metadata.gz: 2b21a4f0be7d3ea735e6eae5d0a46755fbc38c66ecb0190aa09f14b7b796dfedbf39d2438197c736106bd220da0e1862324eedecec830825e61312f4db00cf47
7
- data.tar.gz: 4ccfaebcac23f6bd4f61f88d50cc48c97e73265f175138d0d267d652486f937b68b3bb024fefb353ff7175c5317fc77171f4aeb67d638d6cabf2398fa74593bd
6
+ metadata.gz: 86432c9943f405ca9d6589a05982142ddc4cead8f420c470fc21a1f1441269cce2b22e6ab900f81fb417336f301539203965e809a5c57e15e8eaf9e8a306110f
7
+ data.tar.gz: e7f0fcc690cbb437ed12eab042e7bf362f24ebb9329566789d527866a6709d7c69aeb7e9072ee8a56d7d6bdadd176e6edb61b9a404a2c8083482d2aa78c9d58b
data/Rakefile CHANGED
@@ -1 +1,11 @@
1
1
  require "bundler/gem_tasks"
2
+
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs << "test"
7
+ t.test_files = FileList['test/*test.rb']
8
+ t.verbose = true
9
+ end
10
+
11
+ task :default => :test
@@ -0,0 +1,147 @@
1
+ #
2
+ # DO NOT MODIFY!!!!
3
+ # This file is automatically generated by Racc 1.4.12
4
+ # from Racc grammer file "".
5
+ #
6
+
7
+ require 'racc/parser.rb'
8
+
9
+ require 'strscan'
10
+ module Piece
11
+ class ExpressionParser < Racc::Parser
12
+
13
+ module_eval(<<'...end expression.y/module_eval...', 'expression.y', 22)
14
+ class Exp < Struct.new(:op, :left, :right)
15
+ end
16
+
17
+ class Id < Struct.new(:val)
18
+ end
19
+
20
+ def parse(str)
21
+ @tokens = []
22
+ str = "" if str.nil?
23
+ scanner = StringScanner.new(str + ' ')
24
+
25
+ until scanner.eos?
26
+ case
27
+ when scanner.scan(/\s+/)
28
+ # ignore space
29
+ when m = scanner.scan(/[+-]/i)
30
+ @tokens.push [:OP, m]
31
+ when m = scanner.scan(/(\w+)\b/i)
32
+ @tokens.push [:ID, m]
33
+ when m = scanner.scan(/\*/i)
34
+ @tokens.push [:ID, m]
35
+ else
36
+ raise "unexpected characters #{scanner.peek(5)}"
37
+ end
38
+ end
39
+ @tokens.push [false, false]
40
+ do_parse
41
+ end
42
+
43
+ def next_token
44
+ @tokens.shift
45
+ end
46
+ ...end expression.y/module_eval...
47
+ ##### State transition tables begin ###
48
+
49
+ racc_action_table = [
50
+ 3, 4, 5, 6, 3 ]
51
+
52
+ racc_action_check = [
53
+ 0, 1, 2, 4, 5 ]
54
+
55
+ racc_action_pointer = [
56
+ -3, 1, 0, nil, 3, 1, nil, nil ]
57
+
58
+ racc_action_default = [
59
+ -4, -4, -1, -3, -4, -4, 8, -2 ]
60
+
61
+ racc_goto_table = [
62
+ 2, 1, nil, nil, nil, 7 ]
63
+
64
+ racc_goto_check = [
65
+ 2, 1, nil, nil, nil, 2 ]
66
+
67
+ racc_goto_pointer = [
68
+ nil, 1, 0 ]
69
+
70
+ racc_goto_default = [
71
+ nil, nil, nil ]
72
+
73
+ racc_reduce_table = [
74
+ 0, 0, :racc_error,
75
+ 1, 5, :_reduce_1,
76
+ 3, 6, :_reduce_2,
77
+ 1, 6, :_reduce_3 ]
78
+
79
+ racc_reduce_n = 4
80
+
81
+ racc_shift_n = 8
82
+
83
+ racc_token_table = {
84
+ false => 0,
85
+ :error => 1,
86
+ :OP => 2,
87
+ :ID => 3 }
88
+
89
+ racc_nt_base = 4
90
+
91
+ racc_use_result_var = false
92
+
93
+ Racc_arg = [
94
+ racc_action_table,
95
+ racc_action_check,
96
+ racc_action_default,
97
+ racc_action_pointer,
98
+ racc_goto_table,
99
+ racc_goto_check,
100
+ racc_goto_default,
101
+ racc_goto_pointer,
102
+ racc_nt_base,
103
+ racc_reduce_table,
104
+ racc_token_table,
105
+ racc_shift_n,
106
+ racc_reduce_n,
107
+ racc_use_result_var ]
108
+
109
+ Racc_token_to_s_table = [
110
+ "$end",
111
+ "error",
112
+ "OP",
113
+ "ID",
114
+ "$start",
115
+ "target",
116
+ "exp" ]
117
+
118
+ Racc_debug_parser = false
119
+
120
+ ##### State transition tables end #####
121
+
122
+ # reduce 0 omitted
123
+
124
+ module_eval(<<'.,.,', 'expression.y', 11)
125
+ def _reduce_1(val, _values)
126
+ val[0]
127
+ end
128
+ .,.,
129
+
130
+ module_eval(<<'.,.,', 'expression.y', 14)
131
+ def _reduce_2(val, _values)
132
+ Exp.new(val[1], val[0], val[2])
133
+ end
134
+ .,.,
135
+
136
+ module_eval(<<'.,.,', 'expression.y', 15)
137
+ def _reduce_3(val, _values)
138
+ Id.new(val[0])
139
+ end
140
+ .,.,
141
+
142
+ def _reduce_none(val, _values)
143
+ val[0]
144
+ end
145
+
146
+ end # class ExpressionParser
147
+ end # module Piece
@@ -0,0 +1,53 @@
1
+ class Piece::ExpressionParser
2
+ options no_result_var
3
+
4
+ prechigh
5
+ left OP
6
+ preclow
7
+
8
+ token ID OP
9
+
10
+ rule
11
+ target
12
+ : exp { val[0] }
13
+
14
+ exp
15
+ : exp OP exp { Exp.new(val[1], val[0], val[2]) }
16
+ | ID { Id.new(val[0]) }
17
+ ;
18
+
19
+ ---- header ----
20
+ require 'strscan'
21
+ ---- inner ----
22
+ class Exp < Struct.new(:op, :left, :right)
23
+ end
24
+
25
+ class Id < Struct.new(:val)
26
+ end
27
+
28
+ def parse(str)
29
+ @tokens = []
30
+ str = "" if str.nil?
31
+ scanner = StringScanner.new(str + ' ')
32
+
33
+ until scanner.eos?
34
+ case
35
+ when scanner.scan(/\s+/)
36
+ # ignore space
37
+ when m = scanner.scan(/[+-]/i)
38
+ @tokens.push [:OP, m]
39
+ when m = scanner.scan(/(\w+)\b/i)
40
+ @tokens.push [:ID, m]
41
+ when m = scanner.scan(/\*/i)
42
+ @tokens.push [:ID, m]
43
+ else
44
+ raise "unexpected characters #{scanner.peek(5)}"
45
+ end
46
+ end
47
+ @tokens.push [false, false]
48
+ do_parse
49
+ end
50
+
51
+ def next_token
52
+ @tokens.shift
53
+ end
data/lib/piece/pieces.rb CHANGED
@@ -1,5 +1,9 @@
1
+ require 'piece/expression.tab'
1
2
 
2
3
  module Piece
4
+ class InvalidKey < StandardError
5
+ end
6
+
3
7
  class Pieces
4
8
  def initialize(data)
5
9
  @data = data
@@ -15,22 +19,52 @@ module Piece
15
19
 
16
20
  private
17
21
  def parts(key)
22
+ raise InvalidKey, "Should not include '*' in a key" if key.include?('*')
18
23
  key.split(':')
19
24
  end
20
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
+ apply(group.val, keys)
39
+ else
40
+ if @data.has_key?(group)
41
+ get(@data[group], keys)
42
+ else
43
+ raise "Unknown group: #{group}"
44
+ end
45
+ end
46
+ end
47
+
21
48
  def get(data, keys)
22
49
  return data.nil? ? nil : Array(data) if keys.empty?
23
-
24
50
  case data
25
51
  when Hash
26
- get(data[keys.shift], keys)
52
+ get(data[keys.first], keys[1..-1])
27
53
  when Array
28
- '*' if data.include?('*') || data.include?(keys.shift)
54
+ get(data.first, keys) || get(data[1..-1], keys)
29
55
  when NilClass
30
56
  nil
57
+ when ExpressionParser::Exp
58
+ apply(data, keys)
59
+ when ExpressionParser::Id
60
+ match?(data.val, keys.first) ? '*' : nil
31
61
  else
32
- get([data], keys)
62
+ get(ExpressionParser.new.parse(data), keys)
33
63
  end
34
64
  end
65
+
66
+ def match?(a, b)
67
+ a == b || a.nil? || a == '*' || b == '*'
68
+ end
35
69
  end
36
70
  end
data/lib/piece/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Piece
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  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 it simpler}
13
+ spec.description = %q{It's time 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)/}) }
@@ -21,4 +21,5 @@ Gem::Specification.new do |spec|
21
21
  spec.add_development_dependency "bundler", "~> 1.10"
22
22
  spec.add_development_dependency "rake", "~> 10.0"
23
23
  spec.add_development_dependency "test-unit", "~> 3.0"
24
+ spec.add_development_dependency "racc", "~> 1.4"
24
25
  end
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.0
4
+ version: 0.1.1
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-02 00:00:00.000000000 Z
11
+ date: 2015-06-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,7 +52,21 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.0'
55
- description: It's time to make it simpler
55
+ - !ruby/object:Gem::Dependency
56
+ name: racc
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.4'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.4'
69
+ description: It's time to make user privileges and feature toggle simpler
56
70
  email:
57
71
  - swing1979@gmail.com
58
72
  executables: []
@@ -68,6 +82,8 @@ files:
68
82
  - bin/console
69
83
  - bin/setup
70
84
  - lib/piece.rb
85
+ - lib/piece/expression.tab.rb
86
+ - lib/piece/expression.y
71
87
  - lib/piece/pieces.rb
72
88
  - lib/piece/version.rb
73
89
  - piece.gemspec