Class: OpenC3::MetadataModel
- Inherits:
-
SortedModel
- Object
- Model
- SortedModel
- OpenC3::MetadataModel
- Defined in:
- lib/openc3/models/metadata_model.rb
Constant Summary collapse
- METADATA_TYPE =
'metadata'.freeze
- PRIMARY_KEY =
'__METADATA'.freeze
Constants inherited from SortedModel
Instance Attribute Summary collapse
-
#color ⇒ Object
readonly
Returns the value of attribute color.
-
#constraints ⇒ Object
readonly
Returns the value of attribute constraints.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Attributes inherited from SortedModel
Attributes inherited from Model
#name, #plugin, #scope, #updated_at
Class Method Summary collapse
Instance Method Summary collapse
-
#as_json(*a) ⇒ Hash
(also: #to_s)
Generated from the MetadataModel.
-
#create(update: false) ⇒ Object
Update the Redis hash at primary_key based on the initial passed start The member is set to the JSON generated via calling as_json.
-
#initialize(scope:, start:, color: nil, metadata:, constraints: nil, type: METADATA_TYPE, updated_at: 0) ⇒ MetadataModel
constructor
A new instance of MetadataModel.
-
#update(start: nil, color: nil, metadata: nil, constraints: nil) ⇒ Object
Update the model.
-
#validate(update: false) ⇒ Object
Validates the instance variables: @start, @color, @metadata.
- #validate_color ⇒ Object
- #validate_constraints ⇒ Object
- #validate_metadata ⇒ Object
Methods inherited from SortedModel
all, count, destroy, #destroy, get, get_current_value, #notify, range, range_destroy, #validate_start
Methods inherited from Model
all, #check_disable_erb, #deploy, #destroy, #destroyed?, filter, find_all_by_plugin, from_json, get, get_all_models, get_model, handle_config, names, set, store, store_queued, #undeploy
Constructor Details
#initialize(scope:, start:, color: nil, metadata:, constraints: nil, type: METADATA_TYPE, updated_at: 0) ⇒ MetadataModel
Returns a new instance of MetadataModel.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/openc3/models/metadata_model.rb', line 54 def initialize( scope:, start:, color: nil, metadata:, constraints: nil, type: METADATA_TYPE, updated_at: 0 ) super(start: start, scope: scope, updated_at: updated_at) @start = start @color = color @metadata = @constraints = constraints if constraints @type = type # For the as_json, from_json round trip end |
Instance Attribute Details
#color ⇒ Object (readonly)
Returns the value of attribute color.
47 48 49 |
# File 'lib/openc3/models/metadata_model.rb', line 47 def color @color end |
#constraints ⇒ Object (readonly)
Returns the value of attribute constraints.
47 48 49 |
# File 'lib/openc3/models/metadata_model.rb', line 47 def constraints @constraints end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
47 48 49 |
# File 'lib/openc3/models/metadata_model.rb', line 47 def @metadata end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
47 48 49 |
# File 'lib/openc3/models/metadata_model.rb', line 47 def type @type end |
Class Method Details
.notify(scope:, kind:, start:, stop: nil) ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/openc3/models/metadata_model.rb', line 36 def self.notify(scope:, kind:, start:, stop: nil) json = {'type' => METADATA_TYPE, 'start' => start} json['stop'] = stop if stop notification = { 'data' => JSON.generate(json), 'kind' => kind, 'type' => 'calendar', } CalendarTopic.write_entry(notification, scope: scope) end |
.pk(scope) ⇒ Object
32 33 34 |
# File 'lib/openc3/models/metadata_model.rb', line 32 def self.pk(scope) "#{scope}#{PRIMARY_KEY}" end |
Instance Method Details
#as_json(*a) ⇒ Hash Also known as: to_s
Returns generated from the MetadataModel.
140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/openc3/models/metadata_model.rb', line 140 def as_json(*a) { 'scope' => @scope, 'start' => @start, 'color' => @color, 'metadata' => @metadata.as_json(*a), 'constraints' => @constraints, 'type' => METADATA_TYPE, 'updated_at' => @updated_at, } end |
#create(update: false) ⇒ Object
Update the Redis hash at primary_key based on the initial passed start The member is set to the JSON generated via calling as_json
117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/openc3/models/metadata_model.rb', line 117 def create(update: false) validate(update: update) @updated_at = Time.now.to_nsec_from_epoch MetadataModel.destroy(scope: @scope, start: update) if update Store.zadd(@primary_key, @start, JSON.generate(as_json(:allow_nan => true))) if update notify(kind: 'updated') else notify(kind: 'created') end end |
#update(start: nil, color: nil, metadata: nil, constraints: nil) ⇒ Object
Update the model. All arguments are optional, only those set will be updated.
130 131 132 133 134 135 136 137 |
# File 'lib/openc3/models/metadata_model.rb', line 130 def update(start: nil, color: nil, metadata: nil, constraints: nil) orig_start = @start @start = start if start @color = color if color @metadata = if @constraints = constraints if constraints create(update: orig_start) end |
#validate(update: false) ⇒ Object
Validates the instance variables: @start, @color, @metadata
72 73 74 75 76 77 |
# File 'lib/openc3/models/metadata_model.rb', line 72 def validate(update: false) validate_start(update: update) validate_color() () validate_constraints() if @constraints end |
#validate_color ⇒ Object
79 80 81 82 83 84 85 86 87 |
# File 'lib/openc3/models/metadata_model.rb', line 79 def validate_color() if @color.nil? @color = '#%06x' % (rand * 0xffffff) end unless @color =~ /#?([0-9a-fA-F]{6})/ raise SortedInputError.new "invalid color, must be in hex format, e.g. #FF0000" end @color = "##{@color}" unless @color.start_with?('#') end |
#validate_constraints ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/openc3/models/metadata_model.rb', line 98 def validate_constraints() unless @constraints.is_a?(Hash) raise SortedInputError.new "Constraints must be a hash/object: #{@constraints}" end # Convert keys to strings. This isn't quite as efficient as symbols # but we store as JSON which is all strings and it makes comparisons easier. @constraints = @constraints.transform_keys(&:to_s) unless (@constraints.keys - @metadata.keys).empty? raise SortedInputError.new "Constraints keys must be subset of metadata: #{@constraints.keys} subset #{@metadata.keys}" end @constraints.each do |key, constraint| unless constraint.include?(@metadata[key]) raise SortedInputError.new "Constraint violation! key:#{key} value:#{@metadata[key]} constraint:#{constraint}" end end end |
#validate_metadata ⇒ Object
89 90 91 92 93 94 95 96 |
# File 'lib/openc3/models/metadata_model.rb', line 89 def () unless @metadata.is_a?(Hash) raise SortedInputError.new "Metadata must be a hash/object: #{@metadata}" end # Convert keys to strings. This isn't quite as efficient as symbols # but we store as JSON which is all strings and it makes comparisons easier. @metadata = @metadata.transform_keys(&:to_s) end |