piece 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +2 -2
- data/TODO +2 -5
- data/lib/piece/rules.rb +56 -8
- data/lib/piece/version.rb +1 -1
- data/lib/piece.rb +4 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a6a7526736094521b146d63d9483aa5aa2b01a2f
|
4
|
+
data.tar.gz: 2a9102a633868f9f05258390129cbada6226e82d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bdde9cf422140e520832f9ce56e151977d69f570413cd6ca640fcf2888c6cf7780d1527e1be4be4c12e39a5f5441a60284199373e589d7972dc72e57cef1a5c7
|
7
|
+
data.tar.gz: ae8d17cc64bcfb582a253b7845d1614dc4d97c5194b0626fc5c05a0dd014612863b4370c2be1e761b78319dc4e4e04201b36065612711d75727949315af1c01b
|
data/.travis.yml
CHANGED
data/TODO
CHANGED
@@ -1,7 +1,4 @@
|
|
1
|
-
* load rules by string format: group:group:*, group:group:action
|
2
|
-
* append rules
|
3
1
|
* serialize to yaml
|
4
2
|
* save to DB
|
5
|
-
*
|
6
|
-
* print
|
7
|
-
* print all matching rules given a key
|
3
|
+
* print reason: why match? respond true / false
|
4
|
+
* print all matching rules given an action
|
data/lib/piece/rules.rb
CHANGED
@@ -5,22 +5,70 @@ module Piece
|
|
5
5
|
end
|
6
6
|
|
7
7
|
class Rules
|
8
|
-
def initialize(data)
|
8
|
+
def initialize(data={})
|
9
9
|
@data = data
|
10
10
|
end
|
11
11
|
|
12
|
-
def
|
13
|
-
|
12
|
+
def <<(rule)
|
13
|
+
parts = rule_parts(rule)
|
14
|
+
group = parts[0..-3].inject(@data) do |data, part|
|
15
|
+
data[part] ||= {}
|
16
|
+
end
|
17
|
+
last = parts.last
|
18
|
+
group[parts[-2]] = dequote(last).split(',').map(&:strip)
|
19
|
+
end
|
20
|
+
alias :add :<<
|
21
|
+
|
22
|
+
def delete(*rule)
|
23
|
+
return if rule.empty?
|
24
|
+
parts = rule_parts(rule)
|
25
|
+
|
26
|
+
group = parts[0..-2].inject(@data) do |data, part|
|
27
|
+
data[part].tap do |ret|
|
28
|
+
return if ret.nil?
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
if group
|
33
|
+
group.delete_if {|rule| rule == parts[-1]}
|
34
|
+
if group.empty?
|
35
|
+
self.delete(*(parts[0..-2]))
|
36
|
+
end
|
37
|
+
end
|
14
38
|
end
|
15
39
|
|
16
|
-
def
|
17
|
-
|
40
|
+
def match?(*action)
|
41
|
+
!!self[*action]
|
42
|
+
end
|
43
|
+
|
44
|
+
def [](*action)
|
45
|
+
get(@data, action_parts(action))
|
18
46
|
end
|
19
47
|
|
20
48
|
private
|
21
|
-
def
|
22
|
-
|
23
|
-
|
49
|
+
def action_parts(action)
|
50
|
+
action.map{|part| part.to_s.split(':')}.flatten.map(&:strip).tap do |ret|
|
51
|
+
if ret.any?{|part| part.include?('*')}
|
52
|
+
raise InvalidAction, "Should not include '*' in an action"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def rule_parts(rule)
|
58
|
+
Array(rule).map do |part|
|
59
|
+
case part
|
60
|
+
when Array
|
61
|
+
part.join(',')
|
62
|
+
when Symbol
|
63
|
+
part.to_s
|
64
|
+
else
|
65
|
+
part.to_s
|
66
|
+
end.split(':')
|
67
|
+
end.flatten.map(&:strip)
|
68
|
+
end
|
69
|
+
|
70
|
+
def dequote(str)
|
71
|
+
str =~ /^[\['"](.*)[\]'"]$/ ? $1 : str
|
24
72
|
end
|
25
73
|
|
26
74
|
def apply(group, actions)
|
data/lib/piece/version.rb
CHANGED
data/lib/piece.rb
CHANGED
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.4
|
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-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|