openhab-scripting 2.16.0 → 2.17.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/openhab.rb +12 -16
- data/lib/openhab/core/entity_lookup.rb +162 -0
- data/lib/openhab/core/openhab_setup.rb +31 -0
- data/lib/openhab/core/osgi.rb +61 -0
- data/lib/openhab/dsl/actions.rb +105 -0
- data/lib/openhab/dsl/dsl.rb +47 -0
- data/lib/openhab/{core/dsl → dsl}/gems.rb +0 -1
- data/lib/openhab/dsl/group.rb +100 -0
- data/lib/openhab/dsl/items/items.rb +46 -0
- data/lib/openhab/dsl/items/number_item.rb +352 -0
- data/lib/openhab/dsl/items/string_item.rb +120 -0
- data/lib/openhab/dsl/monkey_patch/actions/actions.rb +4 -0
- data/lib/openhab/dsl/monkey_patch/actions/script_thing_actions.rb +32 -0
- data/lib/openhab/dsl/monkey_patch/events/events.rb +5 -0
- data/lib/openhab/dsl/monkey_patch/events/item_command.rb +23 -0
- data/lib/openhab/dsl/monkey_patch/events/item_state_changed.rb +35 -0
- data/lib/openhab/dsl/monkey_patch/events/thing_status_info.rb +33 -0
- data/lib/openhab/dsl/monkey_patch/items/contact_item.rb +61 -0
- data/lib/openhab/dsl/monkey_patch/items/dimmer_item.rb +193 -0
- data/lib/openhab/dsl/monkey_patch/items/group_item.rb +37 -0
- data/lib/openhab/dsl/monkey_patch/items/items.rb +133 -0
- data/lib/openhab/dsl/monkey_patch/items/metadata.rb +281 -0
- data/lib/openhab/dsl/monkey_patch/items/persistence.rb +70 -0
- data/lib/openhab/dsl/monkey_patch/items/switch_item.rb +95 -0
- data/lib/openhab/dsl/monkey_patch/ruby/number.rb +39 -0
- data/lib/openhab/dsl/monkey_patch/ruby/range.rb +47 -0
- data/lib/openhab/dsl/monkey_patch/ruby/ruby.rb +7 -0
- data/lib/openhab/dsl/monkey_patch/ruby/string.rb +41 -0
- data/lib/openhab/dsl/monkey_patch/types/decimal_type.rb +70 -0
- data/lib/openhab/dsl/monkey_patch/types/on_off_type.rb +51 -0
- data/lib/openhab/dsl/monkey_patch/types/open_closed_type.rb +36 -0
- data/lib/openhab/dsl/monkey_patch/types/percent_type.rb +32 -0
- data/lib/openhab/dsl/monkey_patch/types/quantity_type.rb +69 -0
- data/lib/openhab/dsl/monkey_patch/types/types.rb +8 -0
- data/lib/openhab/dsl/persistence.rb +25 -0
- data/lib/openhab/dsl/rules/automation_rule.rb +342 -0
- data/lib/openhab/dsl/rules/guard.rb +134 -0
- data/lib/openhab/dsl/rules/property.rb +102 -0
- data/lib/openhab/dsl/rules/rule.rb +116 -0
- data/lib/openhab/dsl/rules/rule_config.rb +151 -0
- data/lib/openhab/dsl/rules/triggers/changed.rb +143 -0
- data/lib/openhab/dsl/rules/triggers/channel.rb +53 -0
- data/lib/openhab/dsl/rules/triggers/command.rb +104 -0
- data/lib/openhab/dsl/rules/triggers/cron.rb +177 -0
- data/lib/openhab/dsl/rules/triggers/trigger.rb +124 -0
- data/lib/openhab/dsl/rules/triggers/updated.rb +98 -0
- data/lib/openhab/dsl/states.rb +61 -0
- data/lib/openhab/dsl/things.rb +91 -0
- data/lib/openhab/dsl/time_of_day.rb +228 -0
- data/lib/openhab/dsl/timers.rb +77 -0
- data/lib/openhab/dsl/types/quantity.rb +290 -0
- data/lib/openhab/dsl/units.rb +39 -0
- data/lib/openhab/log/configuration.rb +21 -0
- data/lib/openhab/log/logger.rb +172 -0
- data/lib/openhab/version.rb +1 -1
- metadata +55 -58
- data/lib/openhab/configuration.rb +0 -16
- data/lib/openhab/core/cron.rb +0 -27
- data/lib/openhab/core/debug.rb +0 -34
- data/lib/openhab/core/dsl.rb +0 -51
- data/lib/openhab/core/dsl/actions.rb +0 -107
- data/lib/openhab/core/dsl/entities.rb +0 -140
- data/lib/openhab/core/dsl/group.rb +0 -93
- data/lib/openhab/core/dsl/items/items.rb +0 -51
- data/lib/openhab/core/dsl/items/number_item.rb +0 -323
- data/lib/openhab/core/dsl/items/string_item.rb +0 -122
- data/lib/openhab/core/dsl/monkey_patch/actions/actions.rb +0 -4
- data/lib/openhab/core/dsl/monkey_patch/actions/script_thing_actions.rb +0 -22
- data/lib/openhab/core/dsl/monkey_patch/events.rb +0 -5
- data/lib/openhab/core/dsl/monkey_patch/events/item_command.rb +0 -13
- data/lib/openhab/core/dsl/monkey_patch/events/item_state_changed.rb +0 -25
- data/lib/openhab/core/dsl/monkey_patch/events/thing_status_info.rb +0 -26
- data/lib/openhab/core/dsl/monkey_patch/items/contact_item.rb +0 -54
- data/lib/openhab/core/dsl/monkey_patch/items/dimmer_item.rb +0 -182
- data/lib/openhab/core/dsl/monkey_patch/items/group_item.rb +0 -27
- data/lib/openhab/core/dsl/monkey_patch/items/items.rb +0 -132
- data/lib/openhab/core/dsl/monkey_patch/items/metadata.rb +0 -283
- data/lib/openhab/core/dsl/monkey_patch/items/persistence.rb +0 -72
- data/lib/openhab/core/dsl/monkey_patch/items/switch_item.rb +0 -87
- data/lib/openhab/core/dsl/monkey_patch/ruby/number.rb +0 -41
- data/lib/openhab/core/dsl/monkey_patch/ruby/range.rb +0 -47
- data/lib/openhab/core/dsl/monkey_patch/ruby/ruby.rb +0 -7
- data/lib/openhab/core/dsl/monkey_patch/ruby/string.rb +0 -43
- data/lib/openhab/core/dsl/monkey_patch/types/decimal_type.rb +0 -60
- data/lib/openhab/core/dsl/monkey_patch/types/on_off_type.rb +0 -41
- data/lib/openhab/core/dsl/monkey_patch/types/open_closed_type.rb +0 -25
- data/lib/openhab/core/dsl/monkey_patch/types/percent_type.rb +0 -23
- data/lib/openhab/core/dsl/monkey_patch/types/quantity_type.rb +0 -58
- data/lib/openhab/core/dsl/monkey_patch/types/types.rb +0 -8
- data/lib/openhab/core/dsl/persistence.rb +0 -27
- data/lib/openhab/core/dsl/property.rb +0 -96
- data/lib/openhab/core/dsl/rule/automation_rule.rb +0 -348
- data/lib/openhab/core/dsl/rule/guard.rb +0 -136
- data/lib/openhab/core/dsl/rule/rule.rb +0 -117
- data/lib/openhab/core/dsl/rule/rule_config.rb +0 -153
- data/lib/openhab/core/dsl/rule/triggers/changed.rb +0 -145
- data/lib/openhab/core/dsl/rule/triggers/channel.rb +0 -55
- data/lib/openhab/core/dsl/rule/triggers/command.rb +0 -106
- data/lib/openhab/core/dsl/rule/triggers/cron.rb +0 -160
- data/lib/openhab/core/dsl/rule/triggers/trigger.rb +0 -126
- data/lib/openhab/core/dsl/rule/triggers/updated.rb +0 -100
- data/lib/openhab/core/dsl/states.rb +0 -63
- data/lib/openhab/core/dsl/things.rb +0 -93
- data/lib/openhab/core/dsl/time_of_day.rb +0 -229
- data/lib/openhab/core/dsl/timers.rb +0 -79
- data/lib/openhab/core/dsl/types/quantity.rb +0 -292
- data/lib/openhab/core/dsl/units.rb +0 -41
- data/lib/openhab/core/log.rb +0 -170
- data/lib/openhab/core/patch_load_path.rb +0 -7
- data/lib/openhab/core/startup_delay.rb +0 -23
- data/lib/openhab/osgi.rb +0 -59
@@ -1,100 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'core/log'
|
4
|
-
|
5
|
-
module OpenHAB
|
6
|
-
module Core
|
7
|
-
module DSL
|
8
|
-
module Rule
|
9
|
-
#
|
10
|
-
# Module holds rule triggers
|
11
|
-
#
|
12
|
-
module Triggers
|
13
|
-
include Logging
|
14
|
-
|
15
|
-
#
|
16
|
-
# Create a trigger when item, group or thing is updated
|
17
|
-
#
|
18
|
-
# @param [Array] items array to trigger on updated
|
19
|
-
# @param [State] to to match for tigger
|
20
|
-
#
|
21
|
-
# @return [Trigger] Trigger for updated entity
|
22
|
-
#
|
23
|
-
def updated(*items, to: nil)
|
24
|
-
items.flatten.each do |item|
|
25
|
-
logger.trace("Creating updated trigger for item(#{item}) to(#{to})")
|
26
|
-
[to].flatten.each do |to_state|
|
27
|
-
trigger, config = create_update_trigger(item, to_state)
|
28
|
-
append_trigger(trigger, config)
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
private
|
34
|
-
|
35
|
-
#
|
36
|
-
# Create a trigger for updates
|
37
|
-
#
|
38
|
-
# @param [Object] item Type of item [Group,Thing,Item] to create update trigger for
|
39
|
-
# @param [State] to_state state restriction on trigger
|
40
|
-
#
|
41
|
-
# @return [Array<Hash,String>] first element is a String specifying trigger type
|
42
|
-
# second element is a Hash configuring trigger
|
43
|
-
#
|
44
|
-
def create_update_trigger(item, to_state)
|
45
|
-
case item
|
46
|
-
when GroupItems then group_update(item, to_state)
|
47
|
-
when Thing then thing_update(item, to_state)
|
48
|
-
else item_update(item, to_state)
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
#
|
53
|
-
# Create an update trigger for an item
|
54
|
-
#
|
55
|
-
# @param [Item] item to create trigger for
|
56
|
-
# @param [State] to_state optional state restriction for target
|
57
|
-
#
|
58
|
-
# @return [Array<Hash,String>] first element is a String specifying trigger type
|
59
|
-
# second element is a Hash configuring trigger
|
60
|
-
#
|
61
|
-
def item_update(item, to_state)
|
62
|
-
config = { 'itemName' => item.name }
|
63
|
-
config['state'] = to_state.to_s unless to_state.nil?
|
64
|
-
trigger = Trigger::ITEM_STATE_UPDATE
|
65
|
-
[trigger, config]
|
66
|
-
end
|
67
|
-
|
68
|
-
#
|
69
|
-
# Create an update trigger for a group
|
70
|
-
#
|
71
|
-
# @param [Item] item to create trigger for
|
72
|
-
# @param [State] to_state optional state restriction for target
|
73
|
-
#
|
74
|
-
# @return [Array<Hash,String>] first element is a String specifying trigger type
|
75
|
-
# second element is a Hash configuring trigger
|
76
|
-
#
|
77
|
-
def group_update(item, to_state)
|
78
|
-
config = { 'groupName' => item.group.name }
|
79
|
-
config['state'] = to_state.to_s unless to_state.nil?
|
80
|
-
trigger = Trigger::GROUP_STATE_UPDATE
|
81
|
-
[trigger, config]
|
82
|
-
end
|
83
|
-
|
84
|
-
#
|
85
|
-
# Create an update trigger for a thing
|
86
|
-
#
|
87
|
-
# @param [Thing] thing to create trigger for
|
88
|
-
# @param [State] to_state optional state restriction for target
|
89
|
-
#
|
90
|
-
# @return [Array<Hash,String>] first element is a String specifying trigger type
|
91
|
-
# second element is a Hash configuring trigger
|
92
|
-
#
|
93
|
-
def thing_update(thing, to_state)
|
94
|
-
trigger_for_thing(thing, Trigger::THING_UPDATE, to_state)
|
95
|
-
end
|
96
|
-
end
|
97
|
-
end
|
98
|
-
end
|
99
|
-
end
|
100
|
-
end
|
@@ -1,63 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'delegate'
|
4
|
-
|
5
|
-
module OpenHAB
|
6
|
-
module Core
|
7
|
-
module DSL
|
8
|
-
#
|
9
|
-
# Manages storing and restoring item state
|
10
|
-
#
|
11
|
-
module States
|
12
|
-
java_import org.openhab.core.model.script.actions.BusEvent
|
13
|
-
|
14
|
-
#
|
15
|
-
# Delegates state storage to a Hash providing methods to operate with states
|
16
|
-
#
|
17
|
-
class StateStorage < SimpleDelegator
|
18
|
-
#
|
19
|
-
# Restore the stored states of all items
|
20
|
-
#
|
21
|
-
#
|
22
|
-
def restore
|
23
|
-
BusEvent.restoreStates(to_h)
|
24
|
-
end
|
25
|
-
|
26
|
-
#
|
27
|
-
# Restore states for items that have changed
|
28
|
-
#
|
29
|
-
#
|
30
|
-
def restore_changes
|
31
|
-
BusEvent.restoreStates(select { |item, value| item != value })
|
32
|
-
end
|
33
|
-
|
34
|
-
#
|
35
|
-
# Detect if any item have changed states since being stored
|
36
|
-
#
|
37
|
-
# @return [Boolean] True if any items have changed states, false otherwise
|
38
|
-
#
|
39
|
-
def changed?
|
40
|
-
any? { |item, value| item != value }
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
#
|
45
|
-
# Store states of supplied items
|
46
|
-
#
|
47
|
-
# @param [Array] items to store states of
|
48
|
-
#
|
49
|
-
# @return [StateStorage] item states
|
50
|
-
#
|
51
|
-
def store_states(*items)
|
52
|
-
items = items.flatten.map { |item| item.is_a?(Group) ? item.group : item }
|
53
|
-
states = StateStorage.new(BusEvent.storeStates(*items).to_h)
|
54
|
-
if block_given?
|
55
|
-
yield
|
56
|
-
states.restore
|
57
|
-
end
|
58
|
-
states
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end
|
@@ -1,93 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'java'
|
4
|
-
require 'core/log'
|
5
|
-
require 'core/dsl/actions'
|
6
|
-
require 'delegate'
|
7
|
-
|
8
|
-
module OpenHAB
|
9
|
-
module Core
|
10
|
-
module DSL
|
11
|
-
#
|
12
|
-
# Support for OpenHAB Things
|
13
|
-
#
|
14
|
-
module Things
|
15
|
-
include Logging
|
16
|
-
|
17
|
-
#
|
18
|
-
# Ruby Delegator for Thing
|
19
|
-
#
|
20
|
-
class Thing < SimpleDelegator
|
21
|
-
include OpenHAB::Core::DSL::Actions
|
22
|
-
include Logging
|
23
|
-
|
24
|
-
def initialize(thing)
|
25
|
-
super
|
26
|
-
define_action_methods
|
27
|
-
end
|
28
|
-
|
29
|
-
private
|
30
|
-
|
31
|
-
java_import 'org.openhab.core.automation.annotation.RuleAction'
|
32
|
-
|
33
|
-
#
|
34
|
-
# Define methods from actions mapped to this thing
|
35
|
-
#
|
36
|
-
#
|
37
|
-
def define_action_methods
|
38
|
-
actions_for_thing(uid).each do |action|
|
39
|
-
methods = action.java_class.declared_instance_methods
|
40
|
-
methods.select { |method| method.annotation_present?(RuleAction.java_class) }
|
41
|
-
.each { |method| define_action_method(action: action, method: method.name) }
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
#
|
46
|
-
# Define a method, delegating to supplied action class
|
47
|
-
#
|
48
|
-
# @param [Object] action object to delegate method to
|
49
|
-
# @param [String] method Name of method to delegate
|
50
|
-
#
|
51
|
-
#
|
52
|
-
def define_action_method(action:, method:)
|
53
|
-
logger.trace("Adding action method '#{method}' to thing '#{uid}'")
|
54
|
-
define_singleton_method(method) do |*args|
|
55
|
-
action.public_send(method, *args)
|
56
|
-
end
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
#
|
61
|
-
# Wraps all Things in a delegator to underlying set and provides lookup method
|
62
|
-
#
|
63
|
-
class Things < SimpleDelegator
|
64
|
-
java_import org.openhab.core.thing.ThingUID
|
65
|
-
|
66
|
-
# Gets a specific thing by name in the format binding_id:type_id:thing_id
|
67
|
-
# @return Thing specified by name or nil if name does not exist in thing registry
|
68
|
-
def[](uid)
|
69
|
-
thing_uid = ThingUID.new(*uid.split(':'))
|
70
|
-
# rubocop: disable Style/GlobalVars
|
71
|
-
thing = $things.get(thing_uid)
|
72
|
-
# rubocop: enable Style/GlobalVars
|
73
|
-
return unless thing
|
74
|
-
|
75
|
-
logger.trace("Retrieved Thing(#{thing}) from registry for uid: #{uid}")
|
76
|
-
Thing.new(thing)
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
#
|
81
|
-
# Get all things known to OpenHAB
|
82
|
-
#
|
83
|
-
# @return [Set] of all Thing objects known to openhab
|
84
|
-
#
|
85
|
-
def things
|
86
|
-
# rubocop: disable Style/GlobalVars
|
87
|
-
Things.new($things.getAll.map { |thing| Thing.new(thing) }.to_set)
|
88
|
-
# rubocop: enable Style/GlobalVars
|
89
|
-
end
|
90
|
-
end
|
91
|
-
end
|
92
|
-
end
|
93
|
-
end
|
@@ -1,229 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'java'
|
4
|
-
require 'core/log'
|
5
|
-
require 'time'
|
6
|
-
require 'date'
|
7
|
-
|
8
|
-
module OpenHAB
|
9
|
-
module Core
|
10
|
-
module DSL
|
11
|
-
# Namespace for classes and modules that handle Time Of Day - Times without specific dates e.g. 6:00:00
|
12
|
-
# @author Brian O'Connell
|
13
|
-
# @since 0.0.1
|
14
|
-
module Tod
|
15
|
-
java_import java.time.LocalTime
|
16
|
-
java_import java.time.format.DateTimeFormatterBuilder
|
17
|
-
java_import java.util.Locale
|
18
|
-
|
19
|
-
# Class that encapsulates a Time of Day, often viewed as hour-minute-second
|
20
|
-
# @author Brian O'Connell
|
21
|
-
# @since 0.0.1
|
22
|
-
class TimeOfDay
|
23
|
-
include Comparable
|
24
|
-
|
25
|
-
# Immutable Java object containing Time Of Day
|
26
|
-
# @return [Java.Time.LocalTime] reprsenting the Time Of Day
|
27
|
-
attr_reader :local_time
|
28
|
-
|
29
|
-
# Constructs a TimeOfDay representing the time when called
|
30
|
-
# @since 0.0.1
|
31
|
-
# @return [TimeOfDay] representing time when method was invoked
|
32
|
-
def self.now
|
33
|
-
now = LocalTime.now()
|
34
|
-
TimeOfDay.new(h: now.hour, m: now.minute, s: now.second)
|
35
|
-
end
|
36
|
-
|
37
|
-
# Constructs a TimeOfDay representing midnight
|
38
|
-
# @since 0.0.1
|
39
|
-
# @return [TimeOfDay] representing midnight
|
40
|
-
def self.midnight
|
41
|
-
TimeOfDay.new(h: 0, m: 0, s: 0)
|
42
|
-
end
|
43
|
-
|
44
|
-
# Constructs a TimeOfDay representing noon
|
45
|
-
# @since 0.0.1
|
46
|
-
# @return [TimeOfDay] representing noon
|
47
|
-
def self.noon
|
48
|
-
TimeOfDay.new(h: 12, m: 0, s: 0)
|
49
|
-
end
|
50
|
-
|
51
|
-
# Constructs a TimeOfDay representing the time when called
|
52
|
-
# @since 0.0.1
|
53
|
-
# @param [String] string representation of TimeOfDay. Valid formats include "HH:MM:SS", "HH:MM",
|
54
|
-
# "H:MM", "HH", "H", "H:MM am"
|
55
|
-
# @return [TimeOfDay] object created by parsing supplied string
|
56
|
-
def self.parse(string)
|
57
|
-
format = /(am|pm)$/i.match?(string) ? 'h[:mm[:ss]][ ]a' : 'H[:mm[:ss]]'
|
58
|
-
local_time = LocalTime.parse(string, DateTimeFormatterBuilder.new
|
59
|
-
.parseCaseInsensitive.appendPattern(format).toFormatter(Locale::ENGLISH))
|
60
|
-
TimeOfDay.new(h: local_time.hour, m: local_time.minute, s: local_time.second)
|
61
|
-
rescue java.time.format.DateTimeParseException => e
|
62
|
-
raise ArgumentError, e.message
|
63
|
-
end
|
64
|
-
|
65
|
-
# Constructs a TimeOfDay representing the time when called
|
66
|
-
# @since 0.0.1
|
67
|
-
# @option opts [Number] :h Hour of the day, defaults to 0
|
68
|
-
# @option opts [Number] :m Minute of the day, defaults to 0
|
69
|
-
# @option opts [Number] :s Second of the day, defaults to 0
|
70
|
-
# @return [TimeOfDay] representing time when method was invoked
|
71
|
-
# rubocop: disable Naming/MethodParameterName
|
72
|
-
# This method has a better feel with short parameter names
|
73
|
-
def initialize(h: 0, m: 0, s: 0)
|
74
|
-
@local_time = LocalTime.of(h, m, s)
|
75
|
-
freeze
|
76
|
-
end
|
77
|
-
# rubocop: enable Naming/MethodParameterName
|
78
|
-
|
79
|
-
# Returns true if the time falls within a range
|
80
|
-
def between?(range)
|
81
|
-
between(range).cover? self
|
82
|
-
end
|
83
|
-
|
84
|
-
# Returns the hour of the TimeOfDay
|
85
|
-
# @since 0.0.1
|
86
|
-
# @return [Number] Hour of the day, from 0 to 23
|
87
|
-
def hour
|
88
|
-
@local_time.hour
|
89
|
-
end
|
90
|
-
|
91
|
-
# Returns the minute of the TimeOfDay
|
92
|
-
# @since 0.0.1
|
93
|
-
# @return [Number] minute of the day, from 0 to 59
|
94
|
-
def minute
|
95
|
-
@local_time.minute
|
96
|
-
end
|
97
|
-
|
98
|
-
# Returns the second of the TimeOfDay
|
99
|
-
# @since 0.0.1
|
100
|
-
# @return [Number] second of the day, from 0 to 59
|
101
|
-
def second
|
102
|
-
@local_time.second
|
103
|
-
end
|
104
|
-
|
105
|
-
# Returns the string representation of the TimeOfDay
|
106
|
-
# @since 0.0.1
|
107
|
-
# @return [String] in any of the following formats depending on time representation HH:mm, HH:mm:ss,
|
108
|
-
# HH:mm:ss.SSS, HH:mm:ss.SSSSSS, HH:mm:ss.SSSSSSSSS
|
109
|
-
def to_s
|
110
|
-
@local_time.to_s
|
111
|
-
end
|
112
|
-
|
113
|
-
# Compares one TimeOfDay to another
|
114
|
-
# @since 0.0.1
|
115
|
-
# @return [Number, nil] -1,0,1 if other TimeOfDay is less than, equal to, or greater than this TimeOfDay
|
116
|
-
# or nil if an object other than TimeOfDay is provided
|
117
|
-
def <=>(other)
|
118
|
-
case other
|
119
|
-
when TimeOfDay
|
120
|
-
@local_time.compare_to(other.local_time)
|
121
|
-
when String
|
122
|
-
@local_time.compare_to(TimeOfDay.parse(other).local_time)
|
123
|
-
else
|
124
|
-
-(other <=> self)
|
125
|
-
end
|
126
|
-
end
|
127
|
-
end
|
128
|
-
|
129
|
-
# Modules that refines the Ruby Range object cover? and include? methods to support TimeOfDay ranges
|
130
|
-
class TimeOfDayRangeElement
|
131
|
-
include Comparable
|
132
|
-
include Logging
|
133
|
-
|
134
|
-
NUM_SECONDS_IN_DAY = (60 * 60 * 24)
|
135
|
-
|
136
|
-
attr_reader :sod
|
137
|
-
|
138
|
-
def initialize(sod:, range_begin:)
|
139
|
-
@sod = sod
|
140
|
-
@range_begin = range_begin
|
141
|
-
end
|
142
|
-
|
143
|
-
# Returns the current second of day advanced by 1 second
|
144
|
-
def succ
|
145
|
-
TimeOfDayRangeElement.new(sod: @sod + 1, range_begin: @range_begin)
|
146
|
-
end
|
147
|
-
|
148
|
-
# Compares one TimeOfDayRangeElement to another
|
149
|
-
# @since 2.4.0
|
150
|
-
# @return [Number, nil] -1,0,1 if other is less than, equal to, or greater than this TimeOfDay
|
151
|
-
def <=>(other)
|
152
|
-
other_second_of_day = to_second_of_day(other)
|
153
|
-
logger.trace do
|
154
|
-
"SOD(#{sod}) "\
|
155
|
-
"other SOD(#{other_second_of_day}) "\
|
156
|
-
"Other Class (#{other.class}) "\
|
157
|
-
"Result (#{sod <=> other_second_of_day})"
|
158
|
-
end
|
159
|
-
sod <=> other_second_of_day
|
160
|
-
end
|
161
|
-
|
162
|
-
private
|
163
|
-
|
164
|
-
#
|
165
|
-
# Convert object to the seconds of a day they reprsent
|
166
|
-
#
|
167
|
-
# @param [Object] object TimeofDay,String,Time, or TimeOfDayRangeElement to convert
|
168
|
-
#
|
169
|
-
# @return [Integer] seconds of day represented by supplied object
|
170
|
-
#
|
171
|
-
def to_second_of_day(object)
|
172
|
-
case object
|
173
|
-
when TimeOfDay then adjust_second_of_day(object.local_time.to_second_of_day)
|
174
|
-
when String then adjust_second_of_day(TimeOfDay.parse(object).local_time.to_second_of_day)
|
175
|
-
when Time then adjust_second_of_day(TimeOfDay.new(h: object.hour, m: object.min,
|
176
|
-
s: object.sec).local_time.to_second_of_day)
|
177
|
-
when TimeOfDayRangeElement then object.sod
|
178
|
-
else raise ArgumentError, 'Supplied argument cannot be converted into Time Of Day Object'
|
179
|
-
end
|
180
|
-
end
|
181
|
-
|
182
|
-
def adjust_second_of_day(second_of_day)
|
183
|
-
second_of_day += NUM_SECONDS_IN_DAY if second_of_day < @range_begin
|
184
|
-
second_of_day
|
185
|
-
end
|
186
|
-
end
|
187
|
-
|
188
|
-
# Creates a range that can be compared against time of day objects or strings
|
189
|
-
# to see if they are within the range
|
190
|
-
# @since 2.4.0
|
191
|
-
# @return Range object representing a TimeOfDay Range
|
192
|
-
module_function
|
193
|
-
|
194
|
-
def between(range)
|
195
|
-
raise ArgumentError, 'Supplied object must be a range' unless range.is_a? Range
|
196
|
-
|
197
|
-
start = to_time_of_day(range.begin)
|
198
|
-
ending = to_time_of_day(range.end)
|
199
|
-
|
200
|
-
start_sod = start.local_time.to_second_of_day
|
201
|
-
ending_sod = ending.local_time.to_second_of_day
|
202
|
-
ending_sod += TimeOfDayRangeElement::NUM_SECONDS_IN_DAY if ending_sod < start_sod
|
203
|
-
|
204
|
-
start_range = TimeOfDayRangeElement.new(sod: start_sod, range_begin: start_sod)
|
205
|
-
ending_range = TimeOfDayRangeElement.new(sod: ending_sod, range_begin: start_sod)
|
206
|
-
range.exclude_end? ? (start_range...ending_range) : (start_range..ending_range)
|
207
|
-
end
|
208
|
-
|
209
|
-
#
|
210
|
-
# Convert object to TimeOfDay object
|
211
|
-
#
|
212
|
-
# @param [Object] object TimeOfDay or String to be converted
|
213
|
-
#
|
214
|
-
# @return [TimeOfDay] TimeOfDay created from supplied object
|
215
|
-
#
|
216
|
-
private_class_method def to_time_of_day(object)
|
217
|
-
case object
|
218
|
-
when String then TimeOfDay.parse(object)
|
219
|
-
else object
|
220
|
-
end
|
221
|
-
end
|
222
|
-
|
223
|
-
MIDNIGHT = TimeOfDay.midnight
|
224
|
-
NOON = TimeOfDay.noon
|
225
|
-
ALL_DAY = between(TimeOfDay.new(h: 0, m: 0, s: 0)..TimeOfDay.new(h: 23, m: 59, s: 59))
|
226
|
-
end
|
227
|
-
end
|
228
|
-
end
|
229
|
-
end
|