Class: Checkpoint::Permits::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/checkpoint/permits.rb

Overview

A query object based on agents, credentials, and resources.

This is a helper to capture a set of agents, credentials, and resources, and manage assembly of placeholder variables and binding expressions in the way Sequel expects them. It can take single items or arrays and converts them all to their tokens for query purposes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(agents, credentials, resources, scope: Checkpoint::DB::Permit) ⇒ Query

Returns a new instance of Query.



44
45
46
47
48
49
# File 'lib/checkpoint/permits.rb', line 44

def initialize(agents, credentials, resources, scope: Checkpoint::DB::Permit)
  @agents      = tokenize(agents)
  @credentials = tokenize(credentials)
  @resources   = tokenize(resources)
  @scope       = scope
end

Instance Attribute Details

#agentsObject (readonly)

Returns the value of attribute agents.



42
43
44
# File 'lib/checkpoint/permits.rb', line 42

def agents
  @agents
end

#credentialsObject (readonly)

Returns the value of attribute credentials.



42
43
44
# File 'lib/checkpoint/permits.rb', line 42

def credentials
  @credentials
end

#resourcesObject (readonly)

Returns the value of attribute resources.



42
43
44
# File 'lib/checkpoint/permits.rb', line 42

def resources
  @resources
end

#scopeObject (readonly)

Returns the value of attribute scope.



42
43
44
# File 'lib/checkpoint/permits.rb', line 42

def scope
  @scope
end

Instance Method Details

#agent_paramsObject



79
80
81
# File 'lib/checkpoint/permits.rb', line 79

def agent_params
  Params.new(agents, 'at')
end

#conditionsObject



63
64
65
66
67
68
69
70
# File 'lib/checkpoint/permits.rb', line 63

def conditions
  {
    agent_token:      agent_params.placeholders,
    credential_token: credential_params.placeholders,
    resource_token:   resource_params.placeholders,
    zone_id: :$zone_id
  }
end

#credential_paramsObject



83
84
85
# File 'lib/checkpoint/permits.rb', line 83

def credential_params
  Params.new(credentials, 'ct')
end

#firstObject



59
60
61
# File 'lib/checkpoint/permits.rb', line 59

def first
  exec(:first)
end

#parametersObject



72
73
74
75
76
77
# File 'lib/checkpoint/permits.rb', line 72

def parameters
  (agent_params.values +
   credential_params.values +
   resource_params.values +
   [[:zone_id, DB::Permit.default_zone]]).to_h
end

#queryObject



51
52
53
# File 'lib/checkpoint/permits.rb', line 51

def query
  scope.where(conditions)
end

#resource_paramsObject



87
88
89
# File 'lib/checkpoint/permits.rb', line 87

def resource_params
  Params.new(resources, 'rt')
end

#selectObject



55
56
57
# File 'lib/checkpoint/permits.rb', line 55

def select
  exec(:select)
end