Class: Eaco::Cucumber::World
- Inherits:
-
Object
- Object
- Eaco::Cucumber::World
- Defined in:
- lib/eaco/cucumber/world.rb
Overview
The world in which scenarios are run. This is a story and an example, real-world data model that can be effectively protected by Eaco.
But before { some :art }
AYANAMI REI
__.-"..--,__
__..---" | _| "-_\
__.---" | V|::.-"-._D
_--"".-.._ ,,::::::'"\/""'-:-:/
_.-""::_:_:::::'-8b---" "'
.-/ ::::< |\::::::"\
\/:::/::::'\\ |:::b::\
/|::/:::/::::-::b:%b:\|
\/::::d:|8:::b:"%%%%%\
|\:b:dP:d.:::%%%%%"""-,
\:\.V-/ _\b%P_ / .-._
'|T\ "%j d:::--\.( "-.
::d< -" d%|:::do%P"-:. "-,
|:I _ /%%%o::o8P "\. "\
\8b d%%%%%%P""-._ _ \::. \
\%%8 _./Y%%P/ .::'-oMMo )
H"'|V | A:::...:odMMMMMM( ./
H /_.--"JMMMMbo:d##########b/
.-'o dMMMMMMMMMMMMMMP""
/" / YMMMMMMMMM|
/ . . "MMMMMMMM/
:..::..:::.. MMMMMMM:|
\:/ \::::::::JMMMP":/
:Ao ':__.-'MMMP:::Y
dMM"./:::::::::-.Y
_|b::od8::/:YM::/
I HMMMP::/:/"Y/"
\'""' '':|
| -::::\
| :-._ '::\
|,.| \ _:"o
| d" / " \_:\.
".Y. \ \::\
\ \ \ MM\:Y
Y \ | MM \:b
>\ Y .MM MM
.IY L_ MP' MP
| \:| JM JP
| :\| MP MM
| ::: JM' JP|
| ':' JP JM |
L : JP MP |
0 | Y JM |
0 | JP" |
0 | JP |
m | JP #
I | JM" Y
l | MP :"
|\ :- :|
| | '.\ :|
| | "| \ :|
\ \ \ :|
| | | \ :|
| | | \ :|
| \ \ | '.
| |:\ | :|
\ |::\..| :\
". /::::::' :||
:|::/:::| /:\
| \/::|: \' ::|
| :::|| ::|
| ::|| ::|
| ::|| ::|
| ::|| ::|
| ': | .:|
| : | :|
| : | :|
| :|| .:|
| ::\ .:|
| ::: .::|
/ ::| :::|
__/ .::| ':|
...----"" ::/ ::
/m_ AMm '/ .:::
""MmmMMM#mmMMMMMMM" .:::m
"""YMMM""""""P ':mMI
_' _MMMM
_.-" mm mMMMMMMMM"
/ MMMMMMM""
mmmmmmMMMM"
ch1x0r
http://ascii.co.uk/art/anime
Scenario
In this imaginary world we are N E R V, a Top Secret organization that handles very confidential documents. Some users can read them, some can edit them, and very few bosses can destroy them.
The organization employs internal staff and employs consultants. Staff members have official positions in the organization hierarchy, and they belong to units within departments. They have the big picture.
Consultants, on the other hand, come and go, and work on small parts of the documents, for specific purposes. They do not have the big picture.
Departments own the documents, not people. Documents are of interest of departments, sometimes they should be accessed by the whole house, some other time only few selected users, some times two specific departments or some units.
Either way, most of the time, access is granted to who owns a peculiar authority within the organization and not to a specific person. People may change, authorities and rules change less often.
Mapping Eaco concepts
The Document is a Resource
Each instance of a Document has an ACL .acl attribute.
The :reader, :editor and :owner are Roles on the Document resource, and each role is granted a Permission.
The User is a Actor.
Having an user account is the Designator of type :user. Occupying an official position is the Designator of type :position. Belonging to a department is the Designator of type :department
Instance Method Summary collapse
-
#actors ⇒ Hash
All registered Actor instances.
-
#authorize_model(name, definition)
Authorizes model with the given DSL.
-
#check_documents(names, &block)
Checks the given block on the given set of
Document. -
#eval_dsl(code, model = nil)
Evaluates the given DSL code, substituting the $MODEL string with the given model name.
-
#fetch_actor(name) ⇒ Actor
Fetches an Actor instance by name.
-
#fetch_resource(model, name) ⇒ Object
Fetches a Resource instance by name.
-
#find_model(model_name) ⇒ Class
Returns a model in the ActiveRecord namespace.
-
#initialize ⇒ World
constructor
Sets up the World:.
-
#register_actor(model, name, options = {}) ⇒ Actor
Registers and persists an Actor instance with the given
name. -
#register_resource(model, name) ⇒ Resource
Registers and persists Resource instance with the given name.
-
#resources ⇒ Hash
All registered Resource instances.
Constructor Details
#initialize ⇒ World
Sets up the World:
-
Connect to ActiveRecord
139 140 141 |
# File 'lib/eaco/cucumber/world.rb', line 139 def initialize Eaco::Cucumber::ActiveRecord.connect! end |
Instance Method Details
#actors ⇒ Hash
All registered Actor instances.
232 233 234 |
# File 'lib/eaco/cucumber/world.rb', line 232 def actors @actors ||= {} end |
#authorize_model(name, definition)
This method returns an undefined value.
Authorizes model with the given DSL
152 153 154 155 156 |
# File 'lib/eaco/cucumber/world.rb', line 152 def (name, definition) model = find_model(name) eval_dsl definition, model end |
#check_documents(names, &block)
This method returns an undefined value.
Checks the given block on the given set of Document
256 257 258 259 260 |
# File 'lib/eaco/cucumber/world.rb', line 256 def check_documents(names, &block) model = find_model('Document') names = names.split(/,\s*/) model.where(name: names).each(&block) end |
#eval_dsl(code, model = nil)
This method returns an undefined value.
Evaluates the given DSL code, substituting the $MODEL string with the given model name.
285 286 287 288 289 |
# File 'lib/eaco/cucumber/world.rb', line 285 def eval_dsl(code, model = nil) # Sub in place to print final code when running cucumber code.sub! '$MODEL', model.name if model Eaco.eval! code, '(feature)' end |
#fetch_actor(name) ⇒ Actor
Fetches an Actor instance by name.
185 186 187 188 189 190 191 |
# File 'lib/eaco/cucumber/world.rb', line 185 def fetch_actor(name) actors.fetch(name) rescue KeyError # :nocov: raise "Actor '#{name}' not found in registry" # :nocov: end |
#fetch_resource(model, name) ⇒ Object
Fetches a Resource instance by name.
219 220 221 222 223 224 225 |
# File 'lib/eaco/cucumber/world.rb', line 219 def fetch_resource(model, name) resources.fetch(model).fetch(name) rescue KeyError # :nocov: raise "Resource #{model} '#{name}' not found in registry" # :nocov: end |
#find_model(model_name) ⇒ Class
272 273 274 |
# File 'lib/eaco/cucumber/world.rb', line 272 def find_model(model_name) Eaco::Cucumber::ActiveRecord.const_get(model_name) end |
#register_actor(model, name, options = {}) ⇒ Actor
Registers and persists an Actor instance with the given name.
168 169 170 171 172 173 174 175 176 |
# File 'lib/eaco/cucumber/world.rb', line 168 def register_actor(model, name, = {}) actor_model = find_model(model) actors[name] = actor_model.new.tap do |actor| actor.name = name actor.admin = .fetch(:admin, false) actor.save! end end |
#register_resource(model, name) ⇒ Resource
Registers and persists Resource instance with the given name.
201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/eaco/cucumber/world.rb', line 201 def register_resource(model, name) resource_model = find_model(model) resource = resource_model.new.tap do |resource| resource.name = name resource.save! end resources[model] ||= {} resources[model][name] = resource end |
#resources ⇒ Hash
All registered Resource instances.
242 243 244 |
# File 'lib/eaco/cucumber/world.rb', line 242 def resources @resources ||= {} end |