Class: Cosmos::WidgetModel

Inherits:
Model show all
Defined in:
lib/cosmos/models/widget_model.rb

Constant Summary collapse

PRIMARY_KEY =
'cosmos_widgets'

Instance Attribute Summary collapse

Attributes inherited from Model

#plugin, #scope, #updated_at

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Model

#create, #destroy, filter, find_all_by_plugin, from_json, get_all_models, get_model, set, #update

Constructor Details

#initialize(name:, updated_at: nil, plugin: nil, needs_dependencies: false, scope:) ⇒ WidgetModel

Returns a new instance of WidgetModel.



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/cosmos/models/widget_model.rb', line 79

def initialize(
  name:,
  updated_at: nil,
  plugin: nil,
  needs_dependencies: false,
  scope:
)
  super("#{scope}__#{PRIMARY_KEY}", name: name, plugin: plugin, updated_at: updated_at, scope: scope)
  @full_name = @name.capitalize + 'Widget'
  @filename = @full_name + '.umd.min.js'
  @s3_key = 'widgets/' + @full_name + '/' + @filename
  @needs_dependencies = needs_dependencies
end

Instance Attribute Details

#filenameObject

Returns the value of attribute filename.



31
32
33
# File 'lib/cosmos/models/widget_model.rb', line 31

def filename
  @filename
end

#full_nameObject

Returns the value of attribute full_name.



30
31
32
# File 'lib/cosmos/models/widget_model.rb', line 30

def full_name
  @full_name
end

#nameObject

Returns the value of attribute name.



29
30
31
# File 'lib/cosmos/models/widget_model.rb', line 29

def name
  @name
end

#needs_dependenciesObject

Returns the value of attribute needs_dependencies.



33
34
35
# File 'lib/cosmos/models/widget_model.rb', line 33

def needs_dependencies
  @needs_dependencies
end

#s3_keyObject

Returns the value of attribute s3_key.



32
33
34
# File 'lib/cosmos/models/widget_model.rb', line 32

def s3_key
  @s3_key
end

Class Method Details

.all(scope: nil) ⇒ Object



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

def self.all(scope: nil)
  tools = Store.hgetall("#{scope}__#{PRIMARY_KEY}")
  tools.each do |key, value|
    tools[key] = JSON.parse(value)
  end
  return tools
end

.all_scopesObject



57
58
59
60
61
62
63
64
65
# File 'lib/cosmos/models/widget_model.rb', line 57

def self.all_scopes
  result = {}
  scopes = Cosmos::ScopeModel.all
  scopes.each do |key, _scope|
    widgets = all(scope: key)
    result.merge!(widgets)
  end
  result
end

.get(name:, scope: nil) ⇒ Object

NOTE: The following three class methods are used by the ModelController and are reimplemented to enable various Model class methods to work



37
38
39
# File 'lib/cosmos/models/widget_model.rb', line 37

def self.get(name:, scope: nil)
  super("#{scope}__#{PRIMARY_KEY}", name: name)
end

.handle_config(parser, keyword, parameters, plugin: nil, needs_dependencies: false, scope:) ⇒ Object

Called by the PluginModel to allow this class to validate it’s top-level keyword: “WIDGET”



68
69
70
71
72
73
74
75
76
77
# File 'lib/cosmos/models/widget_model.rb', line 68

def self.handle_config(parser, keyword, parameters, plugin: nil, needs_dependencies: false, scope:)
  case keyword
  when 'WIDGET'
    parser.verify_num_parameters(1, 1, "WIDGET <Name>")
    return self.new(name: parameters[0], plugin: plugin, scope: scope)
  else
    raise ConfigParser::Error.new(parser, "Unknown keyword and parameters for Widget: #{keyword} #{parameters.join(" ")}")
  end
  return nil
end

.names(scope: nil) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/cosmos/models/widget_model.rb', line 41

def self.names(scope: nil)
  array = []
  all(scope: scope).each do |name, _widget|
    array << name
  end
  array
end

Instance Method Details

#as_configObject



102
103
104
105
# File 'lib/cosmos/models/widget_model.rb', line 102

def as_config
  result = "WIDGET \"#{@name}\"\n"
  result
end

#as_jsonObject



93
94
95
96
97
98
99
100
# File 'lib/cosmos/models/widget_model.rb', line 93

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

#deploy(gem_path, variables) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/cosmos/models/widget_model.rb', line 111

def deploy(gem_path, variables)
  rubys3_client = Aws::S3::Client.new

  # Ensure tools bucket exists
  Cosmos::S3Utilities.ensure_public_bucket('tools')

  filename = gem_path + "/tools/widgets/" + @full_name + '/' + @filename

  cache_control = Cosmos::S3Utilities.get_cache_control(@filename)

  # Load widget file
  data = File.read(filename, mode: "rb")
  Cosmos.set_working_dir(File.dirname(filename)) do
    data = ERB.new(data).result(binding.set_variables(variables)) if data.is_printable?
  end
  # TODO: support widgets that aren't just a single js file (and its associated map file)
  rubys3_client.put_object(bucket: 'tools', content_type: 'application/javascript', cache_control: cache_control, key: @s3_key, body: data)
  data = File.read(filename + '.map', mode: "rb")
  rubys3_client.put_object(bucket: 'tools', content_type: 'application/json', cache_control: cache_control, key: @s3_key + '.map', body: data)
end

#handle_config(parser, keyword, parameters) ⇒ Object



107
108
109
# File 'lib/cosmos/models/widget_model.rb', line 107

def handle_config(parser, keyword, parameters)
  raise ConfigParser::Error.new(parser, "Unknown keyword and parameters for Widget: #{keyword} #{parameters.join(" ")}")
end

#undeployObject



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

def undeploy
  rubys3_client = Aws::S3::Client.new
  rubys3_client.delete_object(bucket: 'tools', key: @s3_key)
  rubys3_client.delete_object(bucket: 'tools', key: @s3_key + '.map')
end