openhab-scripting 2.14.3 → 2.16.3
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 -54
- 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 -47
- 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 -318
- data/lib/openhab/core/dsl/items/string_item.rb +0 -120
- 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 -167
- data/lib/openhab/core/dsl/monkey_patch/items/group_item.rb +0 -27
- data/lib/openhab/core/dsl/monkey_patch/items/items.rb +0 -130
- data/lib/openhab/core/dsl/monkey_patch/items/metadata.rb +0 -283
- 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 -6
- data/lib/openhab/core/dsl/monkey_patch/types/decimal_type.rb +0 -24
- 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/types.rb +0 -7
- 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,93 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'delegate'
|
4
|
-
require 'forwardable'
|
5
|
-
require 'openhab/core/dsl/entities'
|
6
|
-
|
7
|
-
module OpenHAB
|
8
|
-
module Core
|
9
|
-
module DSL
|
10
|
-
#
|
11
|
-
# Provides access to OpenHAB Groups
|
12
|
-
#
|
13
|
-
module Groups
|
14
|
-
#
|
15
|
-
# Indicator struct interpreted by rules to trigger based on items contained in a group
|
16
|
-
#
|
17
|
-
GroupItems = Struct.new(:group, keyword_init: true)
|
18
|
-
|
19
|
-
#
|
20
|
-
# Provide access to groups as a set
|
21
|
-
#
|
22
|
-
class Groups < SimpleDelegator
|
23
|
-
#
|
24
|
-
# Get a OpenHAB Group by name
|
25
|
-
# @param [String] name of the group to retrieve
|
26
|
-
#
|
27
|
-
# @return [Set] of OpenHAB Groups
|
28
|
-
#
|
29
|
-
def[](name)
|
30
|
-
group = EntityLookup.lookup_item(name)
|
31
|
-
group.is_a?(Group) ? group : nil
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
#
|
36
|
-
# Retreive all OpenHAB groups
|
37
|
-
#
|
38
|
-
# @return [Set] of OpenHAB Groups
|
39
|
-
#
|
40
|
-
def groups
|
41
|
-
# rubocop: disable Style/GlobalVars
|
42
|
-
Groups.new(EntityLookup.decorate_items($ir.items.select { |item| item.is_a? GroupItem }))
|
43
|
-
# rubocop: enable Style/GlobalVars
|
44
|
-
end
|
45
|
-
|
46
|
-
# Group class that provides access to OpenHAB group object and delegates other methods to
|
47
|
-
# a set of group items
|
48
|
-
class Group < SimpleDelegator
|
49
|
-
extend Forwardable
|
50
|
-
|
51
|
-
java_import org.openhab.core.items.GroupItem
|
52
|
-
|
53
|
-
# @return [org.openhab.core.items.GroupItem] OpenHAB Java Group Item
|
54
|
-
attr_accessor :group
|
55
|
-
|
56
|
-
# @!macro [attach] def_delegators
|
57
|
-
# @!method $2
|
58
|
-
# Forwards to org.openhab.core.items.GroupItem
|
59
|
-
# @see org::openhab::core::items::GroupItem
|
60
|
-
def_delegator :@group, :name
|
61
|
-
def_delegator :@group, :label
|
62
|
-
|
63
|
-
#
|
64
|
-
# Gets members of this group that are themselves a group
|
65
|
-
#
|
66
|
-
# @return [Set] Set of members that are of type group
|
67
|
-
#
|
68
|
-
def groups
|
69
|
-
group.members.grep(org.openhab.core.items.GroupItem)
|
70
|
-
end
|
71
|
-
|
72
|
-
#
|
73
|
-
# Wraps the group in a struct, this method is intended to be called
|
74
|
-
# as an indicator to the rule method that the user wishes to trigger
|
75
|
-
# based on changes to group items
|
76
|
-
#
|
77
|
-
# @return [GroupItems] Indicator struct used by rules engine to trigger based on item changes
|
78
|
-
#
|
79
|
-
def items
|
80
|
-
GroupItems.new(group: group)
|
81
|
-
end
|
82
|
-
|
83
|
-
#
|
84
|
-
# @return [String] List of groups seperated by commas
|
85
|
-
#
|
86
|
-
def to_s
|
87
|
-
"[#{map(&:to_s).join(',')}]"
|
88
|
-
end
|
89
|
-
end
|
90
|
-
end
|
91
|
-
end
|
92
|
-
end
|
93
|
-
end
|
@@ -1,51 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'java'
|
4
|
-
require 'openhab/core/dsl/entities'
|
5
|
-
|
6
|
-
module OpenHAB
|
7
|
-
module Core
|
8
|
-
module DSL
|
9
|
-
#
|
10
|
-
# Manages OpenHAB items
|
11
|
-
#
|
12
|
-
module Items
|
13
|
-
#
|
14
|
-
# Delegates to underlying set of all OpenHAB Items, provides convenience methods
|
15
|
-
#
|
16
|
-
class Items < SimpleDelegator
|
17
|
-
# Fetches the named item from the the ItemRegistry
|
18
|
-
# @param [String] name
|
19
|
-
# @return Item from registry, nil if item missing or requested item is a Group Type
|
20
|
-
def[](name)
|
21
|
-
# rubocop: disable Style/GlobalVars
|
22
|
-
item = $ir.getItem(name)
|
23
|
-
# rubocop: enable Style/GlobalVars
|
24
|
-
item.is_a?(GroupItem) ? nil : item
|
25
|
-
rescue Java::OrgOpenhabCoreItems::ItemNotFoundException
|
26
|
-
nil
|
27
|
-
end
|
28
|
-
|
29
|
-
# Returns true if the given item name exists
|
30
|
-
# @param name [String] Item name to check
|
31
|
-
# @return [Boolean] true if the item exists, false otherwise
|
32
|
-
def include?(name)
|
33
|
-
# rubocop: disable Style/GlobalVars
|
34
|
-
!$ir.getItems(name).empty?
|
35
|
-
# rubocop: enable Style/GlobalVars
|
36
|
-
end
|
37
|
-
alias key? include?
|
38
|
-
end
|
39
|
-
|
40
|
-
java_import org.openhab.core.items.GroupItem
|
41
|
-
# Fetches all non-group items from the item registry
|
42
|
-
# @return [OpenHAB::Core::DSL::Items::Items]
|
43
|
-
def items
|
44
|
-
# rubocop: disable Style/GlobalVars
|
45
|
-
Items.new(EntityLookup.decorate_items($ir.items.reject { |item| item.is_a? GroupItem }))
|
46
|
-
# rubocop: enable Style/GlobalVars
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
@@ -1,318 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'bigdecimal'
|
4
|
-
require 'forwardable'
|
5
|
-
require 'java'
|
6
|
-
require 'openhab/core/dsl/types/quantity'
|
7
|
-
|
8
|
-
module OpenHAB
|
9
|
-
module Core
|
10
|
-
module DSL
|
11
|
-
module Items
|
12
|
-
#
|
13
|
-
# Delegation to OpenHAB Number Item
|
14
|
-
#
|
15
|
-
# rubocop: disable Metrics/ClassLength
|
16
|
-
# Disabled because this class has a single responsibility, there does not appear a logical
|
17
|
-
# way of breaking it up into multiple classes
|
18
|
-
class NumberItem < Numeric
|
19
|
-
extend Forwardable
|
20
|
-
|
21
|
-
def_delegator :@number_item, :to_s
|
22
|
-
|
23
|
-
java_import org.openhab.core.library.types.DecimalType
|
24
|
-
java_import org.openhab.core.library.types.QuantityType
|
25
|
-
java_import 'tec.uom.se.format.SimpleUnitFormat'
|
26
|
-
java_import 'tec.uom.se.AbstractUnit'
|
27
|
-
|
28
|
-
#
|
29
|
-
# Create a new NumberItem
|
30
|
-
#
|
31
|
-
# @param [Java::Org::openhab::core::library::items::NumberItem] number_item OpenHAB number item to delegate to
|
32
|
-
#
|
33
|
-
def initialize(number_item)
|
34
|
-
@number_item = number_item
|
35
|
-
super()
|
36
|
-
end
|
37
|
-
|
38
|
-
#
|
39
|
-
# Check if NumberItem is truthy? as per defined by library
|
40
|
-
#
|
41
|
-
# @return [Boolean] True if item is not in state UNDEF or NULL and value is not zero.
|
42
|
-
#
|
43
|
-
def truthy?
|
44
|
-
@number_item.state? && @number_item.state != DecimalType::ZERO
|
45
|
-
end
|
46
|
-
|
47
|
-
#
|
48
|
-
# Coerce objects into a NumberItem
|
49
|
-
#
|
50
|
-
# @param [Object] other object to coerce to a NumberItem if possible
|
51
|
-
#
|
52
|
-
# @return [Object] NumberItem, QuantityTypes, BigDecimal or nil depending on NumberItem configuration
|
53
|
-
# and/or supplied object
|
54
|
-
#
|
55
|
-
def coerce(other)
|
56
|
-
logger.trace("Coercing #{self} as a request from #{other.class}")
|
57
|
-
case other
|
58
|
-
when Quantity then coerce_from_quantity(other)
|
59
|
-
when Numeric then coerce_from_numeric(other)
|
60
|
-
else
|
61
|
-
logger.trace("#{self} cannot be coereced to #{other.class}")
|
62
|
-
nil
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
#
|
67
|
-
# Compare NumberItem to supplied object
|
68
|
-
#
|
69
|
-
# @param [Object] other object to compare to
|
70
|
-
#
|
71
|
-
# @return [Integer] -1,0,1 or nil depending on value supplied,
|
72
|
-
# nil comparison to supplied object is not possible.
|
73
|
-
#
|
74
|
-
def <=>(other)
|
75
|
-
logger.trace("Comparing #{self} to #{other}")
|
76
|
-
case other
|
77
|
-
when NumberItem then number_item_compare(other)
|
78
|
-
when Numeric then @number_item.state.to_big_decimal.to_d <=> other.to_d
|
79
|
-
when String then @number_item.state <=> QuantityType.new(other) if dimension
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
#
|
84
|
-
# Convert NumberItem to a Quantity
|
85
|
-
#
|
86
|
-
# @param [Object] other String or Unit representing an OpenHAB Unit
|
87
|
-
#
|
88
|
-
# @return [OpenHAB::Core::DSL::Types::Quantity] NumberItem converted to supplied Unit
|
89
|
-
#
|
90
|
-
def |(other)
|
91
|
-
other = SimpleUnitFormat.instance.unitFor(other) if other.is_a? String
|
92
|
-
|
93
|
-
if dimension
|
94
|
-
to_qt | other
|
95
|
-
else
|
96
|
-
Quantity.new(QuantityType.new(to_d.to_java, other))
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
|
-
#
|
101
|
-
# Convert NumberItem to a Quantity
|
102
|
-
#
|
103
|
-
# @return [OpenHAB::Core::DSL::Types::Quantity] NumberItem converted to a QuantityUnit
|
104
|
-
#
|
105
|
-
def to_qt
|
106
|
-
if dimension
|
107
|
-
Quantity.new(@number_item.get_state_as(QuantityType))
|
108
|
-
else
|
109
|
-
Quantity.new(QuantityType.new(to_d.to_java, AbstractUnit::ONE))
|
110
|
-
end
|
111
|
-
end
|
112
|
-
|
113
|
-
#
|
114
|
-
# Converts the NumberItem to an Integer
|
115
|
-
#
|
116
|
-
# @return [Integer] NumberItem as an integer
|
117
|
-
#
|
118
|
-
def to_i
|
119
|
-
to_d&.to_i
|
120
|
-
end
|
121
|
-
|
122
|
-
#
|
123
|
-
# Converts the NumberItem to a float
|
124
|
-
#
|
125
|
-
# @return [Float] NumberItem as a float
|
126
|
-
#
|
127
|
-
def to_f
|
128
|
-
to_d&.to_f
|
129
|
-
end
|
130
|
-
|
131
|
-
#
|
132
|
-
# Converts the NumberItem to a BigDecimal
|
133
|
-
#
|
134
|
-
# @return [BigDecimal] NumberItem as a BigDecimal
|
135
|
-
#
|
136
|
-
def to_d
|
137
|
-
@number_item.state.to_big_decimal.to_d if @number_item.state.respond_to? :to_big_decimal
|
138
|
-
end
|
139
|
-
|
140
|
-
#
|
141
|
-
# Get the Dimension attached to the NumberItem
|
142
|
-
#
|
143
|
-
# @return [Java::org::openhab::core::library::types::QuantityType] dimension
|
144
|
-
#
|
145
|
-
def dimension
|
146
|
-
@number_item.dimension
|
147
|
-
end
|
148
|
-
|
149
|
-
#
|
150
|
-
# Forward missing methods to Openhab Number Item if they are defined
|
151
|
-
#
|
152
|
-
# @param [String] meth method name
|
153
|
-
# @param [Array] args arguments for method
|
154
|
-
# @param [Proc] block <description>
|
155
|
-
#
|
156
|
-
# @return [Object] Value from delegated method in OpenHAB NumberItem
|
157
|
-
#
|
158
|
-
def method_missing(meth, *args, &block)
|
159
|
-
logger.trace("Method missing, performing dynamic lookup for: #{meth}")
|
160
|
-
if @number_item.respond_to?(meth)
|
161
|
-
@number_item.__send__(meth, *args, &block)
|
162
|
-
elsif ::Kernel.method_defined?(meth) || ::Kernel.private_method_defined?(meth)
|
163
|
-
::Kernel.instance_method(meth).bind_call(self, *args, &block)
|
164
|
-
else
|
165
|
-
super(meth, *args, &block)
|
166
|
-
end
|
167
|
-
end
|
168
|
-
|
169
|
-
#
|
170
|
-
# Checks if this method responds to the missing method
|
171
|
-
#
|
172
|
-
# @param [String] method_name Name of the method to check
|
173
|
-
# @param [Boolean] _include_private boolean if private methods should be checked
|
174
|
-
#
|
175
|
-
# @return [Boolean] true if this object will respond to the supplied method, false otherwise
|
176
|
-
#
|
177
|
-
def respond_to_missing?(method_name, _include_private = false)
|
178
|
-
@number_item.respond_to?(method_name) ||
|
179
|
-
::Kernel.method_defined?(method_name) ||
|
180
|
-
::Kernel.private_method_defined?(method_name)
|
181
|
-
end
|
182
|
-
|
183
|
-
%w[+ - * /].each do |operation|
|
184
|
-
define_method(operation) do |other|
|
185
|
-
logger.trace("Execution math operation '#{operation}' on #{inspect} with #{other.inspect}")
|
186
|
-
left_operand, right_operand = operands_for_operation(other)
|
187
|
-
left_operand.public_send(operation, right_operand)
|
188
|
-
end
|
189
|
-
end
|
190
|
-
|
191
|
-
private
|
192
|
-
|
193
|
-
#
|
194
|
-
# Get the operands for any operation
|
195
|
-
#
|
196
|
-
# @param [Object] other object to convert to a compatible operand
|
197
|
-
#
|
198
|
-
# @return [Array[Object,Object]] of operands where the first value is the left operand
|
199
|
-
# and the second value is the right operand
|
200
|
-
#
|
201
|
-
def operands_for_operation(other)
|
202
|
-
case other
|
203
|
-
when NumberItem then number_item_operands(other)
|
204
|
-
when Numeric then [to_d, other.to_d]
|
205
|
-
when String then string_operands(other)
|
206
|
-
else
|
207
|
-
return other.coerce(to_d) if other.respond_to? :coerce
|
208
|
-
|
209
|
-
raise ArgumentError, "#{other.class} can't be coerced into a NumberItem"
|
210
|
-
end
|
211
|
-
end
|
212
|
-
|
213
|
-
#
|
214
|
-
# Get operands for an operation when the right operand is provided as a string
|
215
|
-
#
|
216
|
-
# @param [String] other right operand
|
217
|
-
#
|
218
|
-
# @return [Array[QuantityType,QuantiyType]] of operands where the first value is the left operand
|
219
|
-
# and the second value is the right operand
|
220
|
-
#
|
221
|
-
def string_operands(other)
|
222
|
-
return [to_qt, Quantity.new(other)] if dimension
|
223
|
-
|
224
|
-
raise ArgumentError, 'Strings are only valid operands if NumberItem is dimensions=ed.'
|
225
|
-
end
|
226
|
-
|
227
|
-
#
|
228
|
-
# Get operands for an operation when the right operand is provided is another number item
|
229
|
-
#
|
230
|
-
# @param [NumberItem] other right operand
|
231
|
-
#
|
232
|
-
# @return [Array<QuantityType,QuantityType>,Array<BigDecimal,BigDecimal>] of operands depending on
|
233
|
-
# if the left or right operand has a dimensions
|
234
|
-
#
|
235
|
-
def number_item_operands(other)
|
236
|
-
if dimension || other.dimension
|
237
|
-
dimensioned_operands(other)
|
238
|
-
else
|
239
|
-
logger.trace("Both objects lack dimension, self='#{self}' other='#{other}'")
|
240
|
-
# If nothing has a dimension, just use BigDecimals
|
241
|
-
[to_d, other.to_d]
|
242
|
-
end
|
243
|
-
end
|
244
|
-
|
245
|
-
#
|
246
|
-
# Get operands for an operation when the left or right operand has a dimension
|
247
|
-
#
|
248
|
-
# @param [NumberItem] other right operand
|
249
|
-
#
|
250
|
-
# @return [Array<QuantityType,QuantityType>] of operands
|
251
|
-
#
|
252
|
-
def dimensioned_operands(other)
|
253
|
-
logger.trace("Dimensions self='#{dimension}' other='#{other.dimension}'")
|
254
|
-
if dimension
|
255
|
-
if other.dimension
|
256
|
-
# If both numbers have dimensions, do the math on the quantity types.
|
257
|
-
[to_qt, other.to_qt]
|
258
|
-
else
|
259
|
-
# If this number has dimension and the other does not,
|
260
|
-
# do math with this quantity type and the other as a big decimal
|
261
|
-
[to_qt, other]
|
262
|
-
end
|
263
|
-
else
|
264
|
-
# If this number has no dimension and the other does, convert this into a dimensionless quantity
|
265
|
-
[to_qt, other]
|
266
|
-
end
|
267
|
-
end
|
268
|
-
|
269
|
-
#
|
270
|
-
# Compare two number items, taking into account any dimensions
|
271
|
-
#
|
272
|
-
# @param [NumberItem] other number item
|
273
|
-
#
|
274
|
-
# @return [-1,0,1] depending on if other object is less than, equal to or greater than self
|
275
|
-
#
|
276
|
-
def number_item_compare(other)
|
277
|
-
if other.dimension
|
278
|
-
logger.trace('Other is dimensioned, converting self and other to QuantityTypes to compare')
|
279
|
-
to_qt <=> other.to_qt
|
280
|
-
else
|
281
|
-
@number_item.state <=> other.state
|
282
|
-
end
|
283
|
-
end
|
284
|
-
|
285
|
-
#
|
286
|
-
# Coerce from a numberic object depnding on dimension and state
|
287
|
-
#
|
288
|
-
# @param [Numeric] other numeric object to convert
|
289
|
-
#
|
290
|
-
# @return [Array<QuantityType,QuantityType>,Array<BigDecimal,BigDecimal>,nil] depending on
|
291
|
-
# if this object has a dimension or state
|
292
|
-
#
|
293
|
-
def coerce_from_numeric(other)
|
294
|
-
if dimension
|
295
|
-
[Quantity.new(other), to_qt]
|
296
|
-
elsif @number_item.state?
|
297
|
-
[other.to_d, @number_item.state.to_big_decimal.to_d]
|
298
|
-
end
|
299
|
-
end
|
300
|
-
|
301
|
-
#
|
302
|
-
# Coerce when other is a quantity
|
303
|
-
#
|
304
|
-
# @param [QuantityType] other
|
305
|
-
#
|
306
|
-
# @return [Array<QuanityType,QuantityType] other and self as a quantity type
|
307
|
-
#
|
308
|
-
def coerce_from_quantity(other)
|
309
|
-
as_qt = to_qt
|
310
|
-
logger.trace("Converted #{self} to a Quantity #{as_qt}")
|
311
|
-
[other, as_qt]
|
312
|
-
end
|
313
|
-
end
|
314
|
-
end
|
315
|
-
end
|
316
|
-
end
|
317
|
-
end
|
318
|
-
# rubocop: enable Metrics/ClassLength
|