Class: Cosmos::MetadataModel
- Inherits:
-
SortedModel
- Object
- Model
- SortedModel
- Cosmos::MetadataModel
- Defined in:
- lib/cosmos/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.
-
#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 ⇒ Hash
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:, type: METADATA_TYPE, updated_at: 0) ⇒ MetadataModel
constructor
A new instance of MetadataModel.
-
#to_s ⇒ String
String view of metadata.
-
#update(start:, color:, metadata:) ⇒ Object
Update the Redis hash at primary_key.
-
#validate(update: false) ⇒ Object
Validates the instance variables: @start, @color, @metadata.
- #validate_color ⇒ 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, #as_config, #deploy, #destroy, filter, find_all_by_plugin, from_json, get, get_all_models, get_model, handle_config, names, set, store, #undeploy
Constructor Details
#initialize(scope:, start:, color: nil, metadata:, type: METADATA_TYPE, updated_at: 0) ⇒ MetadataModel
Returns a new instance of MetadataModel.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/cosmos/models/metadata_model.rb', line 39 def initialize( scope:, start:, color: nil, metadata:, type: METADATA_TYPE, updated_at: 0 ) super(start: start, scope: scope, updated_at: updated_at) @start = start @color = color @metadata = @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/cosmos/models/metadata_model.rb', line 33 def color @color end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
33 34 35 |
# File 'lib/cosmos/models/metadata_model.rb', line 33 def @metadata end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
33 34 35 |
# File 'lib/cosmos/models/metadata_model.rb', line 33 def type @type end |
Class Method Details
.pk(scope) ⇒ Object
29 30 31 |
# File 'lib/cosmos/models/metadata_model.rb', line 29 def self.pk(scope) "#{scope}#{PRIMARY_KEY}" end |
Instance Method Details
#as_json ⇒ Hash
Returns generated from the MetadataModel.
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/cosmos/models/metadata_model.rb', line 99 def as_json return { 'scope' => @scope, 'start' => @start, 'color' => @color, 'metadata' => @metadata, '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
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/cosmos/models/metadata_model.rb', line 79 def create(update: false) validate(update: update) @updated_at = Time.now.to_nsec_from_epoch Store.zadd(@primary_key, @start, JSON.generate(as_json())) if update notify(kind: 'updated') else notify(kind: 'created') end end |
#to_s ⇒ String
Returns string view of metadata.
111 112 113 |
# File 'lib/cosmos/models/metadata_model.rb', line 111 def to_s return "<MetadataModel s: #{@start}, c: #{@color}, m: #{@metadata}>" end |
#update(start:, color:, metadata:) ⇒ Object
Update the Redis hash at primary_key
91 92 93 94 95 96 |
# File 'lib/cosmos/models/metadata_model.rb', line 91 def update(start:, color:, metadata:) @start = start @color = color @metadata = create(update: true) end |
#validate(update: false) ⇒ Object
Validates the instance variables: @start, @color, @metadata
55 56 57 58 59 |
# File 'lib/cosmos/models/metadata_model.rb', line 55 def validate(update: false) validate_start(update: update) validate_color() () end |
#validate_color ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/cosmos/models/metadata_model.rb', line 61 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_metadata ⇒ Object
71 72 73 74 75 |
# File 'lib/cosmos/models/metadata_model.rb', line 71 def () unless @metadata.is_a?(Hash) raise SortedInputError.new "Metadata must be a hash/object: #{@metadata}" end end |