Class: HiveRuby::Device
- Inherits:
-
Object
- Object
- HiveRuby::Device
- Defined in:
- lib/hive_ruby/device.rb
Overview
Device class
Constant Summary collapse
- DEFAULT_FILE =
'/etc/device.json'.freeze
Instance Attribute Summary collapse
-
#actions ⇒ Object
Returns the value of attribute actions.
-
#data ⇒ Object
Returns the value of attribute data.
-
#file ⇒ Object
Returns the value of attribute file.
Instance Method Summary collapse
- #execute_action(label, input = nil) ⇒ Object
-
#initialize(file: DEFAULT_FILE) ⇒ Device
constructor
A new instance of Device.
- #register_action(label, &block) ⇒ Object
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.(file) if file raise "No such file #{@file}" unless File.exist? @file @data = read @actions = [] end |
Instance Attribute Details
#actions ⇒ Object
Returns the value of attribute actions.
6 7 8 |
# File 'lib/hive_ruby/device.rb', line 6 def actions @actions end |
#data ⇒ Object
Returns the value of attribute data.
7 8 9 |
# File 'lib/hive_ruby/device.rb', line 7 def data @data end |
#file ⇒ Object
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 |