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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5876eb39c8e8bec6130a3855658854f15d5ff385
4
- data.tar.gz: b25527aa9e5f9ccbef6873325a6f3f66063137d5
3
+ metadata.gz: a6a7526736094521b146d63d9483aa5aa2b01a2f
4
+ data.tar.gz: 2a9102a633868f9f05258390129cbada6226e82d
5
5
  SHA512:
6
- metadata.gz: 281df8890ffa99a0aa7ac8872e3309cb7fc3c3a4608d4ace604f2a9906f5b60626badd9befb2df897517bd1fb2fb114c2ad34fa14fbc28a4cdf8140cfe323cb8
7
- data.tar.gz: d4206dc8301e95c7d569e8fdf2f07964b376caf1c5be403589086716a10b750c21ecdbf0f922cb5a1239cd23b48df8d0f933e6eac56aa526f6cf70b5261ca8b6
6
+ metadata.gz: bdde9cf422140e520832f9ce56e151977d69f570413cd6ca640fcf2888c6cf7780d1527e1be4be4c12e39a5f5441a60284199373e589d7972dc72e57cef1a5c7
7
+ data.tar.gz: ae8d17cc64bcfb582a253b7845d1614dc4d97c5194b0626fc5c05a0dd014612863b4370c2be1e761b78319dc4e4e04201b36065612711d75727949315af1c01b
data/.travis.yml CHANGED
@@ -5,12 +5,12 @@ gemfile:
5
5
  - Gemfile
6
6
  language: ruby
7
7
  rvm:
8
- # - 1.8.7
8
+ - 1.8.7
9
9
  - 1.9.3
10
10
  - 2.0.0
11
11
  - 2.1
12
12
  - 2.2.1
13
- # - jruby-18mode
13
+ - jruby-18mode
14
14
  - jruby-19mode
15
15
  - jruby-head
16
16
  - rbx-2
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
- * delete piece by key: group:group:*
6
- * print reason: why has? respond true / false
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 match?(action)
13
- !!get(@data, parts(action))
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 [](action)
17
- get(@data, parts(action))
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 parts(action)
22
- raise InvalidAction, "Should not include '*' in a rule" if action.include?('*')
23
- action.split(':')
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
@@ -1,3 +1,3 @@
1
1
  module Piece
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
data/lib/piece.rb CHANGED
@@ -8,4 +8,8 @@ module Piece
8
8
  def load(rules)
9
9
  Rules.new(YAML.load(rules))
10
10
  end
11
+
12
+ def rules
13
+ Rules.new
14
+ end
11
15
  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.3
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-04 00:00:00.000000000 Z
11
+ date: 2015-06-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler