Class: Vizcore::ControlPreset

Inherits:
Object
  • Object
show all
Defined in:
lib/vizcore/control_preset.rb

Overview

Loads browser control presets shared through the runtime endpoint.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ ControlPreset

Returns a new instance of ControlPreset.

Parameters:

  • path (String, Pathname)


24
25
26
# File 'lib/vizcore/control_preset.rb', line 24

def initialize(path)
  @path = Pathname.new(path).expand_path
end

Class Method Details

.load(path) ⇒ Hash

Parameters:

  • path (String, Pathname)

Returns:

  • (Hash)


12
13
14
# File 'lib/vizcore/control_preset.rb', line 12

def self.load(path)
  new(path).load
end

.write(path, payload) ⇒ Hash

Parameters:

  • path (String, Pathname)
  • payload (Hash)

Returns:

  • (Hash)


19
20
21
# File 'lib/vizcore/control_preset.rb', line 19

def self.write(path, payload)
  new(path).write(payload)
end

Instance Method Details

#loadHash

Returns:

  • (Hash)


29
30
31
32
33
34
35
36
# File 'lib/vizcore/control_preset.rb', line 29

def load
  raise ArgumentError, "Control preset file not found: #{@path}" unless @path.file?

  parsed = JSON.parse(@path.read)
  normalize_payload(parsed)
rescue JSON::ParserError => e
  raise ArgumentError, "Invalid control preset JSON #{@path}: #{e.message}"
end

#write(payload) ⇒ Hash

Parameters:

  • payload (Hash)

Returns:

  • (Hash)


40
41
42
43
44
45
# File 'lib/vizcore/control_preset.rb', line 40

def write(payload)
  normalized = normalize_payload(payload)
  FileUtils.mkdir_p(@path.dirname)
  @path.write(JSON.pretty_generate(normalized) << "\n")
  normalized
end