Class: SmartBrain::Contracts::ScopeRef

Inherits:
Object
  • Object
show all
Defined in:
lib/smart_brain/contracts/scope_ref.rb

Constant Summary collapse

DEFAULT_ALLOWED_TYPES =
%w[global project expert task session].freeze

Class Method Summary collapse

Class Method Details

.key(value) ⇒ Object



19
20
21
22
# File 'lib/smart_brain/contracts/scope_ref.rb', line 19

def self.key(value)
  ref = normalize(value)
  "#{ref[:type]}:#{ref[:id]}"
end

.normalize(value, allowed_types: DEFAULT_ALLOWED_TYPES) ⇒ Object

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
17
# File 'lib/smart_brain/contracts/scope_ref.rb', line 8

def self.normalize(value, allowed_types: DEFAULT_ALLOWED_TYPES)
  raise ArgumentError, 'scope ref must be an object' unless value.respond_to?(:[])

  type = (value[:type] || value['type']).to_s
  external_id = (value[:id] || value['id']).to_s
  raise ArgumentError, "invalid scope type: #{type}" unless Array(allowed_types).map(&:to_s).include?(type)
  raise ArgumentError, 'scope id is required' if external_id.strip.empty?

  { type: type, id: external_id }
end