Class: Operations::Operation

Inherits:
Object
  • Object
show all
Defined in:
lib/operations/operation.rb

Defined Under Namespace

Classes: OperationValue

Instance Method Summary collapse

Constructor Details

#initialize(**options, &block) ⇒ Operation

Returns a new instance of Operation.



5
6
7
8
9
10
11
12
13
14
15
16
17
# 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

Instance Method Details

#<(sc_op) ⇒ Object



137
138
139
140
141
142
143
144
145
146
# 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) ⇒ Object



152
153
154
# File 'lib/operations/operation.rb', line 152

def <=(sc_op)
  self == sc_op || self < sc_op
end

#==(sc_op) ⇒ Object



118
119
120
121
122
123
124
# 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) ⇒ Object



126
127
128
129
130
131
132
133
134
135
# 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) ⇒ Object



148
149
150
# File 'lib/operations/operation.rb', line 148

def >=(sc_op)
  self == sc_op || self > sc_op
end

#accepts_scope?(scope_name) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
71
72
73
74
# 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_rolesObject



76
77
78
# File 'lib/operations/operation.rb', line 76

def allowed_roles
  Operations.user_roles
end

#as_json(options = {}) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# 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_rolesObject



80
81
# File 'lib/operations/operation.rb', line 80

def denied_roles
end

#descriptionObject



27
28
29
# File 'lib/operations/operation.rb', line 27

def description
  values.description
end

#has_valid_name?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/operations/operation.rb', line 52

def has_valid_name?
  Operations::Config.operation_name_regex === values.name
end

#has_valid_scope?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/operations/operation.rb', line 56

def has_valid_scope?
  Operations::Config.operation_scope_regex === scope
end

#inspectObject



114
115
116
# File 'lib/operations/operation.rb', line 114

def inspect
  "\#<#{self.class.name} `#{self.name}' allowed for `#{scope}'>"
end

#invalid_scopeObject



60
61
62
# File 'lib/operations/operation.rb', line 60

def invalid_scope
  return scope unless has_valid_scope?
end

#is_scope?(scope_name) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/operations/operation.rb', line 64

def is_scope?(scope_name)
  scope.to_s == scope_name.to_s
end

#is_valid?Boolean

def save

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

end

Returns:

  • (Boolean)


48
49
50
# File 'lib/operations/operation.rb', line 48

def is_valid?
  has_valid_name? && has_valid_scope? # && !user.nil?
end

#nameObject



23
24
25
# File 'lib/operations/operation.rb', line 23

def name
  has_valid_name? ? values.name : 'N/A (Invalid operation name)'
end

#to_jsonObject



110
111
112
# File 'lib/operations/operation.rb', line 110

def to_json
  as_json.to_json
end

#usersObject



31
32
33
34
35
# 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

#uuidObject



104
105
106
107
108
# 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

#valuesObject



19
20
21
# File 'lib/operations/operation.rb', line 19

def values
  @values ||= OperationValue.new(nil, :nobody)
end