class Operations::Operation

Constants

OperationValue

Public Class Methods

new(**options) { |values| ... } click to toggle source
# File lib/operations/operation.rb, line 5
def initialize(**options, &block)
  if block_given?
    yield values
  else
    options.each do |key, value|
      if values.respond_to?(key)
        values.send(key.to_s + '=', value)
      end
    end
  end
  ensure_operation_is_valid!
  self
end

Public Instance Methods

<(sc_op) click to toggle source
# File lib/operations/operation.rb, line 137
def <(sc_op)
  if sc_op.class == self.class
    return false if self == sc_op
    return sc_op.accepts_scope?(scope)
  elsif sc_op.class == Symbol
    !accepts_scope?(sc_op)
  else
    false
  end
end
<=(sc_op) click to toggle source
# File lib/operations/operation.rb, line 152
def <=(sc_op)
  self == sc_op || self < sc_op
end
==(sc_op) click to toggle source
# File lib/operations/operation.rb, line 118
def ==(sc_op)
  if sc_op.class == self.class
    sc_op.name == name && sc_op.uuid == uuid
  else
    is_scope?(sc_op)
  end
end
>(sc_op) click to toggle source
# File lib/operations/operation.rb, line 126
def >(sc_op)
  if sc_op.class == self.class
    return false if self == sc_op
    return !sc_op.accepts_scope?(scope)
  elsif sc_op.class == Symbol
    accepts_scope?(sc_op)
  else
    false
  end
end
>=(sc_op) click to toggle source
# File lib/operations/operation.rb, line 148
def >=(sc_op)
  self == sc_op || self > sc_op
end
accepts_scope?(scope_name) click to toggle source
# File lib/operations/operation.rb, line 68
def accepts_scope?(scope_name)
  scope_name = scope_name.to_sym
  return true if scope == :all
  return false if scope == :nobody
  return true if is_scope?(scope_name)
  Operations.allowed_named_roles_for(scope).include?(scope_name)
end
allowed_roles() click to toggle source
# File lib/operations/operation.rb, line 76
def allowed_roles
  Operations.user_roles
end
as_json(options={}) click to toggle source
# File lib/operations/operation.rb, line 83
def as_json(options={})
  additional_hash = {uuid: uuid, users_count: users.count}
  if (methods = options[:methods])
    if methods.class != Array
      methods = [methods]
    end
    methods.each do |method|
      begin
        additional_hash[method] = send(method)
      rescue NoMethodError
        # ignore
        next
      end
    end
  end
  values
      .as_json(options)
      .merge(additional_hash)
      .as_json
end
denied_roles() click to toggle source
# File lib/operations/operation.rb, line 80
def denied_roles
end
description() click to toggle source
# File lib/operations/operation.rb, line 27
def description
  values.description
end
has_valid_name?() click to toggle source
# File lib/operations/operation.rb, line 52
def has_valid_name?
  Operations::Config.operation_name_regex === values.name
end
has_valid_scope?() click to toggle source
# File lib/operations/operation.rb, line 56
def has_valid_scope?
  Operations::Config.operation_scope_regex === scope
end
inspect() click to toggle source
# File lib/operations/operation.rb, line 114
def inspect
  "\#<#{self.class.name} `#{self.name}' allowed for `#{scope}'>"
end
invalid_scope() click to toggle source
# File lib/operations/operation.rb, line 60
def invalid_scope
  return scope unless has_valid_scope?
end
is_scope?(scope_name) click to toggle source
# File lib/operations/operation.rb, line 64
def is_scope?(scope_name)
  scope.to_s == scope_name.to_s
end
is_valid?() click to toggle source

def save

return false if user.nil?
ensure_operation_is_valid!("Cannot save user #{user}")
result = user.update_attribute :operations, self.to_json

end

# File lib/operations/operation.rb, line 48
def is_valid?
  has_valid_name? && has_valid_scope? # && !user.nil?
end
name() click to toggle source
# File lib/operations/operation.rb, line 23
def name
  has_valid_name? ? values.name : 'N/A (Invalid operation name)'
end
to_json() click to toggle source
# File lib/operations/operation.rb, line 110
def to_json
  as_json.to_json
end
users() click to toggle source
# File lib/operations/operation.rb, line 31
def users
  return User.where(id: []) if scope == :nobody
  return User.all if scope == :all
  @users ||= Operations.users_acting_as(scope)
end
uuid() click to toggle source
# File lib/operations/operation.rb, line 104
def uuid
  alg = Operations::Config.operation_uuid_algorithm
  return Operations::Errors::BaseException("The UUID Algorithm has be a class") unless alg.class == Class
  alg.new(name.to_s).to_s
end
values() click to toggle source
# File lib/operations/operation.rb, line 19
def values
  @values ||= OperationValue.new(nil, :nobody)
end

Private Instance Methods

ensure_operation_is_valid!(msg=nil) click to toggle source
# File lib/operations/operation.rb, line 158
def ensure_operation_is_valid!(msg=nil)
  raise Operations::Errors::InvalidOperationError.new(self, nil) unless is_valid?
end
scope() click to toggle source

Hide the scope for security reasons

# File lib/operations/operation.rb, line 163
def scope
  self.values.scope || :nobody
end