Class: HiveRuby::Device

Inherits:
Object
  • Object
show all
Defined in:
lib/hive_ruby/device.rb

Overview

Device class

Constant Summary collapse

DEFAULT_FILE =
'/etc/device.json'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file: DEFAULT_FILE) ⇒ Device

Returns a new instance of Device.



11
12
13
14
15
16
# File 'lib/hive_ruby/device.rb', line 11

def initialize(file: DEFAULT_FILE)
  @file = File.expand_path(file) if file
  raise "No such file #{@file}" unless File.exist? @file
  @data = read
  @actions = []
end

Instance Attribute Details

#actionsObject

Returns the value of attribute actions.



6
7
8
# File 'lib/hive_ruby/device.rb', line 6

def actions
  @actions
end

#dataObject

Returns the value of attribute data.



7
8
9
# File 'lib/hive_ruby/device.rb', line 7

def data
  @data
end

#fileObject

Returns the value of attribute file.



8
9
10
# File 'lib/hive_ruby/device.rb', line 8

def file
  @file
end

Instance Method Details

#execute_action(label, input = nil) ⇒ Object



27
28
29
30
31
# File 'lib/hive_ruby/device.rb', line 27

def execute_action(label, input = nil)
  arr = @actions.select { |e| e[:label].eql? label } || []
  raise 'No such label' if arr.empty?
  arr.first[:action].call input
end

#register_action(label, &block) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/hive_ruby/device.rb', line 18

def register_action(label, &block)
  arr = @data['device_actions'].select { |e| e['label'].eql? label } || []
  raise 'No such label' if arr.empty?
  @actions << {
    label: label,
    action: block
  }
end