bali 1.0.0rc2 → 1.0.0rc3

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: c00de772752e74795255bcb26a90b5c9dee8e829
4
- data.tar.gz: d22ec8ebd7fb7be2f179dd3c9bec50a700f00990
3
+ metadata.gz: 724bb6eef32c67456ef17908c706056bbe650894
4
+ data.tar.gz: 7db4ffe4112029cf64d89b693f3bb365e720122c
5
5
  SHA512:
6
- metadata.gz: bc1e0ccba9e178f59f0a792c52a91315c81e755c83508f9a528b2c4a20127b5f586441e2273cef1c97e2ea59ca4952ea60fac6101d2b1493dde772acc937d237
7
- data.tar.gz: da2384e2836663c6a7e99d7531fc040ea605c5659e5d8fe6f63bbd968bb0679d4ac45f8319aef214efab099c3cd4709ff3ffc19210176c7d34e6496a3313a3de
6
+ metadata.gz: 994dcb6cb86ceba1e4086ba713bf10fe62fb7987e5ba82e63a05f90600c22a4d70ce742a7c52cb39715d6864cad833f565ad079c509a014097c2c8fb2e82fe95
7
+ data.tar.gz: f3aa272ed470a2ee72d476643f728b5bc8a5e06957ddb306b487ab572710468f5a53f82f70f5ec8c522fad5b5757cd4fe5f14a5dd02ec83cb723e288c4e43dc8
data/README.md CHANGED
@@ -58,40 +58,56 @@ Say:
58
58
 
59
59
  ```ruby
60
60
  class My::Transaction
61
+ include Bali::Objector
62
+
61
63
  attr_accessor :is_settled
62
64
  attr_accessor :payment_channel
63
65
 
64
66
  alias :is_settled? :is_settled
65
67
  end
68
+
69
+ class My::Employee
70
+ include Bali::Objector
71
+
72
+ # working experience in the company
73
+ attr_accessor :exp_years
74
+ end
66
75
  ```
67
76
 
68
77
  Assuming that there exist a variable `transaction` which is an instance of `My::Transaction`, we can query about whether the subtarget is granted to perform certain operation:
69
78
 
70
79
  ```ruby
71
- transaction.cant?(:general_user, :delete) # => true
72
- transaction.can("general user", :update) # => true
73
- transaction.can?(:finance_user, :delete) # depend on context
74
- transaction.can?(:monitoring_user, :view) # => true
75
- transaction.can?("monitoring user", :view) # => true
76
- transaction.can?(:admin_user, :cancel) # depend on context
77
- transaction.can?(:supreme_user, :cancel) # => true
78
- transaction.can?(:guest, :view) # => false
80
+ transaction.cant?(:general_user, :delete) # => true
81
+ transaction.can("general user", :update) # => true
82
+ transaction.can?(:finance_user, :delete) # depend on context
83
+ transaction.can?(:monitoring_user, :view) # => true
84
+ transaction.can?("monitoring user", :view) # => true
85
+ transaction.can?(:admin_user, :cancel) # depend on context
86
+ transaction.can?(:supreme_user, :cancel) # => true
87
+ transaction.can?(:guest, :view) # => false
88
+ transaction.can?(:undefined_subtarget, :see) # => false
89
+ transaction.cant?(:undefined_subtarget, :new) # => true
79
90
  ```
80
91
 
81
92
  If a rule is depending on a certain context, then the context will be evaluated to determine whether the subtarget is authorized or not.
82
93
 
83
94
  In the above example, deletion of `transaction` is only allowed if the subtarget is a "finance user" and, the `transaction` itself is already settled.
84
95
 
85
- Rule can also be called on a class, instead of on an object:
96
+ Also, asking `can?` on which the subtarget is not yet defined will always return `false`. In the example above, as `undefined_subtarget` is by itself has never been defined in `describe` under `My::Transaction` rule class, `can?` for `undefined_subtarget` will always return `false`. But, `cant` on simillar ocassion will return `true`.
97
+
98
+ Rule can also be tested on a class:
86
99
 
87
100
  ```ruby
88
101
  My::Transaction.can?(:supreme_user, :new) # => true
89
102
  My::Transaction.can?(:guest, :view) # => false
103
+ My::Employee.can?(:undefined_subtarget, :new) # => false, rule class for this is by its own undefined
90
104
  ```
91
105
 
106
+ As we have never define the `rules_for` My::Employee before, any attempt to `can?` for `My::Employee` will return `false`, so does any attempt to object `cant?` on which will only return `true` for any given subtarget and operation.
107
+
92
108
  ## Contributing
93
109
 
94
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/bali. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
110
+ Bug reports and pull requests are welcome on GitHub at https://github.com/saveav/bali. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
95
111
 
96
112
 
97
113
  ## License
@@ -110,4 +126,12 @@ The gem is available as open source under the terms of the [MIT License](http://
110
126
  #### Version 1.0.0rc2
111
127
  1. [Fix bug when class's name, as a constant, is reloaded](http://stackoverflow.com/questions/2509350/rails-class-object-id-changes-after-i-make-a-request) (re-allocated to different address in the memory)
112
128
  2. Allow describing rule for `nil`, useful if user is not authenticated thus role is probably `nil`
113
- 3. Remove pry from development dependency
129
+ 3. Remove pry from development dependency
130
+
131
+ #### Version 1.0.0rc3
132
+ 1. Each target class should includes `Bali::Objector`, for the following reasons:
133
+ - Makes it clear that class do want to include the Bali::Objector
134
+ - Transparant, and thus less confusing as to where "can?" and "cant" come from
135
+ - When ruby re-parse the class's codes for any reasons, parser will be for sure include Bali::Objector
136
+ 2. Return `true` to any `can?` for undefined target/subtarget alike
137
+ 3. Return `false` to any `cant?` for undefined target/subtarget alike
data/lib/bali.rb CHANGED
@@ -28,22 +28,23 @@ module Bali
28
28
  def rule_class_for(target)
29
29
  if target.is_a?(Symbol)
30
30
  class_name = ALIASED_RULE_CLASS_MAP[target]
31
- raise Bali::DslError, "Rule class is not defined for: #{target}" if class_name.nil?
32
-
33
- rule_class_for(class_name)
31
+ return class_name.nil? ? nil : rule_class_for(class_name)
34
32
  else
35
33
  raise Bali::DslError, "Target must be a class" unless target.is_a?(Class)
36
34
  rule_class = RULE_CLASS_MAP[target.to_s]
37
- raise Bali::DslError, "Rule class is not defined for: #{target}" if rule_class.nil?
38
- rule_class
35
+ return rule_class.nil? ? nil : rule_class
39
36
  end
40
37
  end
41
38
 
39
+ # attempt to search the rule group, but if not exist, will return nil
42
40
  def rule_group_for(target_class, subtarget)
43
41
  rule_class = Bali.rule_class_for(target_class)
44
- rule_group = rule_class.rules_for(subtarget)
45
-
46
- rule_group
42
+ if rule_class
43
+ rule_group = rule_class.rules_for(subtarget)
44
+ return rule_group
45
+ else
46
+ return nil
47
+ end
47
48
  end
48
49
 
49
50
  def add_rule_class(rule_class)
@@ -94,8 +95,6 @@ module Bali
94
95
 
95
96
  # done processing the block, now add the rule class
96
97
  Bali.add_rule_class(self.current_rule_class)
97
-
98
- target_class.include(Bali::Objector) unless target_class.include?(Bali::Objector)
99
98
  end
100
99
  end
101
100
  end
data/lib/bali/objector.rb CHANGED
@@ -24,6 +24,11 @@ module Bali::Objector::Statics
24
24
  rule_group = Bali.rule_group_for(self.class, subtarget)
25
25
  end
26
26
 
27
+ # default of can? is false whenever RuleClass for that class is undefined
28
+ # or RuleGroup for that subtarget is not defined
29
+ return false if rule_group.nil?
30
+
31
+ # get the specific rule
27
32
  rule = rule_group.get_rule(:can, operation)
28
33
 
29
34
  # godly subtarget is allowed to do as he wishes
@@ -57,6 +62,10 @@ module Bali::Objector::Statics
57
62
  rule_group = Bali.rule_group_for(self.class, subtarget)
58
63
  end
59
64
 
65
+ # default of cant? is true whenever RuleClass for that class is undefined
66
+ # or RuleGroup for that subtarget is not defined
67
+ return true if rule_group.nil?
68
+
60
69
  rule = rule_group.get_rule(:cant, operation)
61
70
 
62
71
  # godly subtarget is not to be prohibited in his endeavours
data/lib/bali/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Bali
2
- VERSION = "1.0.0rc2"
2
+ VERSION = "1.0.0rc3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bali
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0rc2
4
+ version: 1.0.0rc3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Pahlevi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-08-15 00:00:00.000000000 Z
11
+ date: 2015-08-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler