Class: OpenC3::NoteModel
- Inherits:
-
SortedModel
- Object
- Model
- SortedModel
- OpenC3::NoteModel
- Defined in:
- lib/openc3/models/note_model.rb
Constant Summary collapse
- NOTE_TYPE =
'note'.freeze
- PRIMARY_KEY =
'__NOTE'.freeze
Constants inherited from SortedModel
Instance Attribute Summary collapse
-
#color ⇒ Object
readonly
Returns the value of attribute color.
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#stop ⇒ Object
readonly
Returns the value of attribute stop.
-
#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 NoteModel.
-
#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:, stop:, color: nil, description:, type: NOTE_TYPE, updated_at: 0) ⇒ NoteModel
constructor
A new instance of NoteModel.
-
#update(start:, stop:, color:, description:) ⇒ Object
Update the Redis hash at primary_key.
-
#validate(update: false) ⇒ Object
Validates the instance variables: @start, @stop, @color, @description.
- #validate_color ⇒ Object
- #validate_stop ⇒ Object
Methods inherited from SortedModel
all, count, destroy, #destroy, get, get_current_value, #notify, range, range_destroy, #validate_start
Methods inherited from Model
all, #as_config, #deploy, #destroy, #destroyed?, filter, find_all_by_plugin, from_json, get, get_all_models, get_model, handle_config, names, set, store, #undeploy
Constructor Details
#initialize(scope:, start:, stop:, color: nil, description:, type: NOTE_TYPE, updated_at: 0) ⇒ NoteModel
Returns a new instance of NoteModel.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/openc3/models/note_model.rb', line 40 def initialize( scope:, start:, stop:, color: nil, description:, type: NOTE_TYPE, updated_at: 0 ) super(start: start, scope: scope, updated_at: updated_at) @start = start @stop = stop @color = color @description = description @type = type # For the as_json, from_json round trip end |
Instance Attribute Details
#color ⇒ Object (readonly)
Returns the value of attribute color.
33 34 35 |
# File 'lib/openc3/models/note_model.rb', line 33 def color @color end |
#description ⇒ Object (readonly)
Returns the value of attribute description.
33 34 35 |
# File 'lib/openc3/models/note_model.rb', line 33 def description @description end |
#stop ⇒ Object (readonly)
Returns the value of attribute stop.
33 34 35 |
# File 'lib/openc3/models/note_model.rb', line 33 def stop @stop end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
33 34 35 |
# File 'lib/openc3/models/note_model.rb', line 33 def type @type end |
Class Method Details
.pk(scope) ⇒ Object
29 30 31 |
# File 'lib/openc3/models/note_model.rb', line 29 def self.pk(scope) "#{scope}#{PRIMARY_KEY}" end |
Instance Method Details
#as_json(*a) ⇒ Hash Also known as: to_s
Returns generated from the NoteModel.
109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/openc3/models/note_model.rb', line 109 def as_json(*a) return { 'scope' => @scope, 'start' => @start, 'stop' => @stop, 'color' => @color, 'description' => @description, 'type' => NOTE_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
86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/openc3/models/note_model.rb', line 86 def create(update: false) validate(update: update) @updated_at = Time.now.to_nsec_from_epoch NoteModel.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:, stop:, color:, description:) ⇒ Object
Update the Redis hash at primary_key
99 100 101 102 103 104 105 106 |
# File 'lib/openc3/models/note_model.rb', line 99 def update(start:, stop:, color:, description:) orig_start = @start @start = start @stop = stop @color = color @description = description create(update: orig_start) end |
#validate(update: false) ⇒ Object
Validates the instance variables: @start, @stop, @color, @description
58 59 60 61 62 |
# File 'lib/openc3/models/note_model.rb', line 58 def validate(update: false) validate_start(update: update) validate_stop() validate_color() end |
#validate_color ⇒ Object
74 75 76 77 78 79 80 81 82 |
# File 'lib/openc3/models/note_model.rb', line 74 def validate_color() if @color.nil? @color = '#%06x' % (rand * 0xffffff) end unless @color =~ /(#*)([0-9,a-f,A-f]{6})/ raise SortedInputError.new "invalid color, must be in hex format, e.g. #FF0000" end @color = "##{@color}" unless @color.start_with?('#') end |
#validate_stop ⇒ Object
64 65 66 67 68 69 70 71 72 |
# File 'lib/openc3/models/note_model.rb', line 64 def validate_stop() unless @stop.is_a?(Integer) raise SortedInputError.new "stop must be integer: #{@stop}" end if @stop.to_i < @start raise SortedInputError.new "stop: #{@stop} must be >= start: #{@start}" end @stop = @stop.to_i end |