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 org.openhab.core.config.core.ConfigDescription instance.

See Also:

  • config_description

Instance Method Summary collapse

Constructor Details

#initializeBuilder

Returns a new instance of Builder.



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

Parameters:

  • uri (String, java.net.URI) (defaults to: nil)

    The URI for the config description. When nil, it will default to dummy:uri

Yields:

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

Returns:

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.

Parameters:

  • name (String, Symbol)

    The group name. This name will be referred to by #parameter.

  • label (String, nil) (defaults to: nil)

    The group label

  • description (String, nil) (defaults to: nil)

    The group description

  • advanced (Boolean) (defaults to: false)

    Whether the group is advanced

  • context (<Type>) (defaults to: nil)

    Context for the 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.

Parameters:

  • name (String, Symbol)

    Parameter name

  • type (:text, :integer, :decimal, :boolean)
  • label (String, nil) (defaults to: nil)

    Parameter label

  • description (String, nil) (defaults to: nil)

    Parameter description

  • min (Numeric, nil) (defaults to: nil)

    Minimum value for numeric types

  • max (Numeric, nil) (defaults to: nil)

    Maximum value for numeric types

  • step (Numeric, nil) (defaults to: nil)

    Step size for numeric types

  • pattern (String, nil) (defaults to: nil)

    Regular expression pattern for string types

  • required (true, false) (defaults to: false)

    Whether the parameter is required

  • read_only (true, false) (defaults to: false)

    Whether the parameter is read only

  • multiple (true, false) (defaults to: false)

    Whether the parameter is a list of values

  • context (String, nil) (defaults to: nil)

    Context for the parameter

  • default (Object, nil) (defaults to: nil)

    Default value for the parameter

  • options (Hash) (defaults to: {})

    Options for the parameter

  • filter_criteria (Hash) (defaults to: {})

    Filter criteria for the parameter

  • group_name (String, nil) (defaults to: nil)

    Parameter group name. When nil, it will be inferred when this method is called inside a #group block.

  • advanced (true, false) (defaults to: false)

    Whether the parameter is advanced

  • limit_to_options (true, false) (defaults to: false)

    Whether the parameter is limited to the given options

  • multiple_limit (Integer, nil) (defaults to: nil)

    Maximum number of values for a multiple parameter

  • unit (String, nil) (defaults to: nil)

    Parameter unit

  • unit_label (String, nil) (defaults to: nil)

    Parameter unit label

  • verify (true, false) (defaults to: false)

    Whether the parameter value should be verified

See Also:



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