Class: Vizcore::ControlPreset
- Inherits:
-
Object
- Object
- Vizcore::ControlPreset
- 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
-
#initialize(path) ⇒ ControlPreset
constructor
A new instance of ControlPreset.
- #load ⇒ Hash
- #write(payload) ⇒ Hash
Constructor Details
#initialize(path) ⇒ ControlPreset
Returns a new instance of ControlPreset.
24 25 26 |
# File 'lib/vizcore/control_preset.rb', line 24 def initialize(path) @path = Pathname.new(path). end |
Class Method Details
.load(path) ⇒ Hash
12 13 14 |
# File 'lib/vizcore/control_preset.rb', line 12 def self.load(path) new(path).load end |
.write(path, payload) ⇒ 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
#load ⇒ 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.}" end |
#write(payload) ⇒ 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 |