Class: OpenHAB::DSL::ConfigDescription::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/openhab/dsl/config_description/builder.rb

Overview

A ConfigDescriptionBuilder is used to create a orgorg.openhaborg.openhab.coreorg.openhab.core.configorg.openhab.core.config.coreorg.openhab.core.config.core.ConfigDescription instance.

See Also:

Instance Method Summary collapse

Constructor Details

#initializeBuilder



16
17
18
19
20
# File 'lib/openhab/dsl/config_description/builder.rb', line 16

def initialize
  @parameters = []
  @parameter_groups = []
  @current_group = nil
end

Instance Method Details

#build(uri = nil) { ... } ⇒ org.openhab.core.config.core.ConfigDescription

Build the config description

Yields:

  • Block executed in the context of this builder. Inside the block, you can call #parameter and #group.

Raises:

  • (ArgumentError)


142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/openhab/dsl/config_description/builder.rb', line 142

def build(uri = nil, &block)
  instance_eval(&block) if block
  raise ArgumentError, "No parameters defined" if @parameters.empty?

  uri ||= "dummy:uri"
  uri = java.net.URI.new(uri.to_s) unless uri.is_a?(java.net.URI)
  org.openhab.core.config.core.ConfigDescriptionBuilder
     .create(uri)
     .with_parameters(@parameters)
     .with_parameter_groups(@parameter_groups)
     .build
end

#group(name, label: nil, description: nil, advanced: false, context: nil) { ... } ⇒ void

This method returns an undefined value.

Create a parameter group.

Yields:

  • Block executed in the context of this group. Any #parameter calls within the block will automatically be added to this group, unless it specifies a different group name.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/openhab/dsl/config_description/builder.rb', line 36

def group(name, label: nil, description: nil, advanced: false, context: nil, &block)
  raise ArgumentError, "Groups cannot be nested" if @current_group

  name = name.to_s
  @parameter_groups << org.openhab.core.config.core.ConfigDescriptionParameterGroupBuilder
                          .create(name)
                          .with_label(label)
                          .with_description(description)
                          .with_advanced(advanced)
                          .with_context(context)
                          .build

  @current_group = name
  instance_eval(&block) if block
ensure
  @current_group = nil
end

#parameter(name, type, label: nil, description: nil, min: nil, max: nil, step: nil, pattern: nil, required: false, read_only: false, multiple: false, context: nil, default: nil, options: {}, filter_criteria: {}, group_name: nil, advanced: false, limit_to_options: false, multiple_limit: nil, unit: nil, unit_label: nil, verify: false) ⇒ void

This method returns an undefined value.

Adds a parameter to the config description.

See Also:

  • OpenHAB::DSL::ConfigDescription::Builder.orgorg.openhaborg.openhab.coreorg.openhab.core.configorg.openhab.core.config.coreorg.openhab.core.config.core.ConfigDescriptionParameter


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/openhab/dsl/config_description/builder.rb', line 86

def parameter(name,
              type,
              label: nil,
              description: nil,
              min: nil,
              max: nil,
              step: nil,
              pattern: nil,
              required: false,
              read_only: false,
              multiple: false,
              context: nil,
              default: nil,
              options: {},
              filter_criteria: {},
              group_name: nil,
              advanced: false,
              limit_to_options: false,
              multiple_limit: nil,
              unit: nil,
              unit_label: nil,
              verify: false)
  # Extract the named arguments into a hash
  @parameters << method(__method__).parameters
                                   .select { |param_type, _| param_type == :key }
                                   .to_h { |_, key| [key, binding.local_variable_get(key)] }
                                   .then do |p|
    p[:options] = p[:options].map do |opt_value, opt_label|
      org.openhab.core.config.core.ParameterOption.new(opt_value, opt_label)
    end
    p[:filter_criteria] = p[:filter_criteria].map do |filter_name, filter_value|
      org.openhab.core.config.core.FilterCriteria.new(filter_name, filter_value)
    end
    p[:minimum] = p.delete(:min)&.to_d&.to_java
    p[:maximum] = p.delete(:max)&.to_d&.to_java
    p[:step] = p.delete(:step)&.to_d&.to_java
    p[:group_name] ||= @current_group
    type = org.openhab.core.config.core.ConfigDescriptionParameter::Type.value_of(type.to_s.upcase)

    parameter = org.openhab.core.config.core.ConfigDescriptionParameterBuilder.create(name.to_s, type)

    p.each do |key, value|
      parameter.send(:"with_#{key}", value) unless value.nil?
    end
    parameter.build
  end
end