Class: Eaco::Cucumber::World

Inherits:
Object
  • Object
show all
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

Constructor Details

#initializeWorld

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

#actorsHash

All registered Actor instances.

Returns:

  • (Hash)

    actors keyed by name


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

Parameters:

  • name (String)

    the model name

  • definition (String)

    the DSL code

See Also:

  • Eaco::Cucumber::World.{{#find_model}

152
153
154
155
156
# File 'lib/eaco/cucumber/world.rb', line 152

def authorize_model(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

Parameters:

  • names (String)

    the document names, separated by ,

  • block (Proc)

    the code to run on each Document

See Also:


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.

Parameters:

  • code (String)

    the DSL code to eval

  • model (Class) (defaults to: nil)

    the model name to substitute (optional)


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.

Parameters:

  • name (String)

    the actor name

Returns:

  • (Actor)

    the registered actor name

Raises:

  • (RuntimeError)

    if the actor is not found in the registry


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.

Parameters:

  • model (String)

    the Resource model name

  • name (String)

    the Resource 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

Returns a model in the ActiveRecord namespace.

Example:

World.find_model('Document')

Parameters:

  • model_name (String)

    the model name

Returns:

  • (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.

Parameters:

  • model (String)

    the Actor model name

  • name (String)

    the Actor instance name

  • options (Boolean) (defaults to: {})

    the only supported one is admin, and specifies whether this Actor an admin

Returns:


168
169
170
171
172
173
174
175
176
# File 'lib/eaco/cucumber/world.rb', line 168

def register_actor(model, name, options = {})
  actor_model = find_model(model)

  actors[name] = actor_model.new.tap do |actor|
    actor.name  = name
    actor.admin = options.fetch(:admin, false)
    actor.save!
  end
end

#register_resource(model, name) ⇒ Resource

Registers and persists Resource instance with the given name.

Parameters:

  • model (String)

    the Resource model name

  • name (String)

    the Resource name

Returns:


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

#resourcesHash

All registered Resource instances.

Returns:

  • (Hash)

    resources keyed by model name with Hashes as values keyed by resource name.


242
243
244
# File 'lib/eaco/cucumber/world.rb', line 242

def resources
  @resources ||= {}
end