Class: Cosmos::Model

Inherits:
Object show all
Defined in:
lib/cosmos/models/model.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(primary_key, **kw_args) ⇒ Model

Store the primary key and keyword arguments



131
132
133
134
135
136
137
# File 'lib/cosmos/models/model.rb', line 131

def initialize(primary_key, **kw_args)
  @primary_key = primary_key
  @name = kw_args[:name]
  @updated_at = kw_args[:updated_at]
  @plugin = kw_args[:plugin]
  @scope = kw_args[:scope]
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



25
26
27
# File 'lib/cosmos/models/model.rb', line 25

def name
  @name
end

#pluginObject

Returns the value of attribute plugin.



27
28
29
# File 'lib/cosmos/models/model.rb', line 27

def plugin
  @plugin
end

#scopeObject

Returns the value of attribute scope.



28
29
30
# File 'lib/cosmos/models/model.rb', line 28

def scope
  @scope
end

#updated_atObject

Returns the value of attribute updated_at.



26
27
28
# File 'lib/cosmos/models/model.rb', line 26

def updated_at
  @updated_at
end

Class Method Details

.all(primary_key) ⇒ Array<Hash>

Returns All the models (as Hash objects) stored under the primary key.

Returns:

  • (Array<Hash>)

    All the models (as Hash objects) stored under the primary key



49
50
51
52
53
54
55
# File 'lib/cosmos/models/model.rb', line 49

def self.all(primary_key)
  hash = Store.hgetall(primary_key)
  hash.each do |key, value|
    hash[key] = JSON.parse(value)
  end
  hash
end

.filter(key, value, scope:) ⇒ Object

Loops over all items and returns objects that match a key value pair



59
60
61
62
63
64
65
66
67
68
# File 'lib/cosmos/models/model.rb', line 59

def self.filter(key, value, scope:)
  filtered = {}
  results = all(scope: scope)
  results.each do |name, result|
    if result[key] == value
      filtered[name] = result
    end
  end
  return filtered
end

.find_all_by_plugin(plugin:, scope:) ⇒ Array<Object>

Returns All the models (as Model objects) stored under the primary key which have the plugin attribute.

Returns:

  • (Array<Object>)

    All the models (as Model objects) stored under the primary key which have the plugin attribute



107
108
109
110
111
112
113
114
# File 'lib/cosmos/models/model.rb', line 107

def self.find_all_by_plugin(plugin:, scope:)
  result = {}
  models = get_all_models(scope: scope)
  models.each do |name, model|
    result[name] = model if model.plugin == plugin
  end
  result
end

.from_json(json, scope:) ⇒ Model

Returns Model generated from the passed JSON.

Returns:

  • (Model)

    Model generated from the passed JSON



78
79
80
81
82
83
84
85
# File 'lib/cosmos/models/model.rb', line 78

def self.from_json(json, scope:)
  json = JSON.parse(json) if String === json
  raise "json data is nil" if json.nil?

  json[:scope] = scope
  json.transform_keys!(&:to_sym)
  self.new(**json, scope: scope)
end

.get(primary_key, name:) ⇒ Hash|nil

Returns Hash of this model or nil if name not found under primary_key.

Returns:

  • (Hash|nil)

    Hash of this model or nil if name not found under primary_key



34
35
36
37
38
39
40
41
# File 'lib/cosmos/models/model.rb', line 34

def self.get(primary_key, name:)
  json = Store.hget(primary_key, name)
  if json
    return JSON.parse(json)
  else
    return nil
  end
end

.get_all_models(scope:) ⇒ Array<Object>

Returns All the models (as Model objects) stored under the primary key.

Returns:

  • (Array<Object>)

    All the models (as Model objects) stored under the primary key



99
100
101
102
103
# File 'lib/cosmos/models/model.rb', line 99

def self.get_all_models(scope:)
  models = {}
  all(scope: scope).each { |name, json| models[name] = from_json(json, scope: scope) }
  models
end

.get_model(name:, scope:) ⇒ Object|nil

Calls self.get and then from_json to turn the Hash configuration into a Ruby Model object.

Returns:

  • (Object|nil)

    Model object or nil if name not found under primary_key



89
90
91
92
93
94
95
96
# File 'lib/cosmos/models/model.rb', line 89

def self.get_model(name:, scope:)
  json = get(name: name, scope: scope)
  if json
    return from_json(json, scope: scope)
  else
    return nil
  end
end

.handle_config(parser, model, keyword, parameters) ⇒ Object



116
117
118
# File 'lib/cosmos/models/model.rb', line 116

def self.handle_config(parser, model, keyword, parameters)
  raise "must be implemented by subclass"
end

.names(primary_key) ⇒ Array<String>

Returns All the names stored under the primary key.

Returns:

  • (Array<String>)

    All the names stored under the primary key



44
45
46
# File 'lib/cosmos/models/model.rb', line 44

def self.names(primary_key)
  Store.hkeys(primary_key).sort
end

.set(json, scope:) ⇒ Object

Sets (updates) the redis hash of this model



71
72
73
74
75
# File 'lib/cosmos/models/model.rb', line 71

def self.set(json, scope:)
  json[:scope] = scope
  json.transform_keys!(&:to_sym)
  self.new(**json).create(force: true)
end

Instance Method Details

#as_configObject

TODO: Not currently used but may be used by a XTCE or other format to COSMOS conversion



185
186
187
# File 'lib/cosmos/models/model.rb', line 185

def as_config
  ""
end

#as_jsonHash

Returns JSON encoding of this model.

Returns:

  • (Hash)

    JSON encoding of this model



177
178
179
180
181
182
# File 'lib/cosmos/models/model.rb', line 177

def as_json
  { 'name' => @name,
    'updated_at' => @updated_at,
    'plugin' => @plugin,
    'scope' => @scope }
end

#create(update: false, force: false) ⇒ Object

Update the Redis hash at primary_key and set the field “name” to the JSON generated via calling as_json



141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/cosmos/models/model.rb', line 141

def create(update: false, force: false)
  unless force
    existing = Store.hget(@primary_key, @name)
    if existing
      raise "#{@primary_key}:#{@name} already exists at create" unless update
    else
      raise "#{@primary_key}:#{@name} doesn't exist at update" if update
    end
  end
  @updated_at = Time.now.to_nsec_from_epoch
  Store.hset(@primary_key, @name, JSON.generate(self.as_json))
end

#deploy(gem_path, variables) ⇒ Object

Deploy the model into the COSMOS system. Subclasses must implement this and typically create MicroserviceModels to implement.



161
162
163
# File 'lib/cosmos/models/model.rb', line 161

def deploy(gem_path, variables)
  raise "must be implemented by subclass"
end

#destroyObject

Delete the model from the Store



171
172
173
174
# File 'lib/cosmos/models/model.rb', line 171

def destroy
  undeploy()
  Store.hdel(@primary_key, @name)
end

#undeployObject

Undo the actions of deploy and remove the model from COSMOS. Subclasses must implement this as by default it is a noop.



167
168
# File 'lib/cosmos/models/model.rb', line 167

def undeploy
end

#updateObject

Alias for create(update: true)



155
156
157
# File 'lib/cosmos/models/model.rb', line 155

def update
  create(update: true)
end