Class: TT::Settings

Inherits:
Object
  • Object
show all
Defined in:
TT_Lib2/settings.rb

Overview

Wrapper for Sketchup.read_default and Sketchup.write_default.

To initialize defaults:

@settings = TT::Settings.new('FooBar')
@settings.set_default( :foo, false )
@settings.set_default( :bar, true )
# Read
read_value = @settings[ :foo ]
# Write
@settings[:bar] = false

Since:

  • 2.0.0

Instance Method Summary collapse

Constructor Details

#initialize(section) ⇒ Settings

Creates a new Settings instance that read and writes values to section in SketchUp's preferences.

Parameters:

  • section (String)

Since:

  • 2.0.0



30
31
32
33
# File 'TT_Lib2/settings.rb', line 30

def initialize(section)
  @section = section
  @cache = {}
end

Instance Method Details

#[](key, default = nil) ⇒ mixed

Type casts read values based on the default value given where Sketchup.read_default would otherwise not do so.

Custom type casts: Length, Symbol

Parameters:

  • key (String, Symbol)
  • default (mixed) (defaults to: nil)

Returns:

  • (mixed)

Since:

  • 2.0.0



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'TT_Lib2/settings.rb', line 46

def [](key, default = nil)
  if @cache.key?(key)
    x = @cache[key]
  else
    begin
      x = Sketchup.read_default(@section, key.to_s, default)
    rescue SyntaxError
      puts "#<TT::Setting> Error reading setting! - Returning default value."
      puts "> #{@section.inspect} - #{key.to_s.inspect} (#{default.inspect})"
      x = default
    end
    x = x.to_l if default.is_a?(Length)
    x = x.intern if x.is_a?(String) && default.is_a?(Symbol)
    @cache[key] = x
    x
  end
end

#[]=(key, value) ⇒ mixed

Converts Length to Float.

Converts Symbol to String.

Parameters:

  • key (String, Symbol)
  • value (mixed)

Returns:

  • (mixed)

Since:

  • 2.0.0



74
75
76
77
78
79
80
# File 'TT_Lib2/settings.rb', line 74

def []=(key, value)
  @cache[key] = value
  value = value.to_f if value.is_a?(Length)
  value = value.to_s if value.is_a?(Symbol)
  Sketchup.write_default(@section, key.to_s, value)
  value
end

#set_default(key, default = nil) ⇒ mixed

Type casts read values based on the default value given where Sketchup.read_default would otherwise not do so.

Preferred over settings[key, default] for setting defaults.

Custom type casts: Length, Symbol

Parameters:

  • key (String, Symbol)
  • default (mixed) (defaults to: nil)

Returns:

  • (mixed)

Since:

  • 2.5.0



95
96
97
# File 'TT_Lib2/settings.rb', line 95

def set_default(key, default = nil)
  self.[](key, default)
end

#sub_section(sub_section) ⇒ TT::Settings

Deprecated.

Untested on OSX. Creates sub-keys under Windows.

Creates a sub-section.

Parameters:

  • sub_section (String)

Returns:

Since:

  • 2.5.0



108
109
110
# File 'TT_Lib2/settings.rb', line 108

def sub_section( sub_section )
  self.new( "#{@section}\\#{sub_section}" )
end