Class: Platanus::Canned::ProfileManager::BProfile
- Inherits:
-
Object
- Object
- Platanus::Canned::ProfileManager::BProfile
- Defined in:
- lib/platanus/canned.rb
Overview
Auxiliary class used by profile manager to store profiles.
Instance Attribute Summary collapse
-
#rules ⇒ Object
readonly
Returns the value of attribute rules.
Instance Method Summary collapse
-
#ability(*_args) ⇒ Object
Adds a new ability.
-
#clean(_name) ⇒ Object
Removes an action by its name.
-
#initialize(_owner, _base, _def_test = :equals_int) ⇒ BProfile
constructor
The initializer takes another profile as rules base.
-
#test(_action, _action_feats, _user_feats) ⇒ Object
Test an action agaist this profile.
-
#test_aux(_tests, _action_feats, _user_feats) ⇒ Object
Run a test group over a set of features.
Constructor Details
#initialize(_owner, _base, _def_test = :equals_int) ⇒ BProfile
The initializer takes another profile as rules base.
116 117 118 119 120 121 122 |
# File 'lib/platanus/canned.rb', line 116 def initialize(_owner, _base, _def_test=:equals_int) @owner = _owner @rules = Hash.new { |h, k| h[k] = [] } _base.rules.each { |k, tests| @rules[k] = tests.clone } unless _base.nil? raise Error.new 'Must provide a default test' if _def_test.nil? @def_test = _def_test end |
Instance Attribute Details
#rules ⇒ Object (readonly)
Returns the value of attribute rules.
113 114 115 |
# File 'lib/platanus/canned.rb', line 113 def rules @rules end |
Instance Method Details
#ability(*_args) ⇒ Object
Adds a new ability.
125 126 127 128 129 130 131 132 133 134 |
# File 'lib/platanus/canned.rb', line 125 def ability(*_args) tests = {} if _args.last.is_a? Hash _args[1...-1].each { |sym| tests[sym] = {} } tests.merge!(_args.last) else _args[1..-1].each { |sym| tests[sym] = {} } end @rules[_args.first] << tests end |
#clean(_name) ⇒ Object
Removes an action by its name.
137 138 139 |
# File 'lib/platanus/canned.rb', line 137 def clean(_name) @rules.delete(_name) end |
#test(_action, _action_feats, _user_feats) ⇒ Object
Test an action agaist this profile
142 143 144 145 146 147 148 149 150 151 |
# File 'lib/platanus/canned.rb', line 142 def test(_action, _action_feats, _user_feats) return false unless @rules.has_key? _action # if any of the test groups passes, then test is passed. @rules[_action].each do |tests| return true if self.test_aux(tests, _action_feats, _user_feats) end return false end |
#test_aux(_tests, _action_feats, _user_feats) ⇒ Object
Run a test group over a set of features
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/platanus/canned.rb', line 154 def test_aux(_tests, _action_feats, _user_feats) _tests.each do |sym, test| # extract action feature action_feat = _action_feats[sym] # special "nil" test test_name = test.fetch(:test, @def_test) if test_name == :nil return false unless action_feat.nil? next end # make sure action feat is avaliable return false if action_feat.nil? # extract user feature if test.has_key? :value user_feat = test[:value] else user_feat = _user_feats[test.fetch(:key, sym)] # TODO: maybe the following tests belong to the "compare features" section return false if user_feat.nil? next if user_feat == :wildcard # Wildcard matches always end # compare features. case test_name when :equals; return false unless user_feat == action_feat when :equals_int; return false unless user_feat.to_i == action_feat.to_i when :if_higher; return false unless user_feat > action_feat when :if_lower; return false unless user_feat < action_feat else # call custom test if @owner.respond_to? test_name return false unless @owner.send(test_name, action_feat, user_feat) end end end return true end |