Class: TT::Settings
- Inherits:
-
Object
- Object
- TT::Settings
- Defined in:
- TT_Lib2/settings.rb
Overview
Instance Method Summary collapse
-
#[](key, default = nil) ⇒ mixed
Type casts read values based on the default value given where
Sketchup.read_default
would otherwise not do so. -
#[]=(key, value) ⇒ mixed
Converts
Length
toFloat
. -
#initialize(section) ⇒ Settings
constructor
Creates a new Settings instance that read and writes values to
section
in SketchUp's preferences. -
#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. -
#sub_section(sub_section) ⇒ TT::Settings
deprecated
Deprecated.
Untested on OSX. Creates sub-keys under Windows.
Constructor Details
#initialize(section) ⇒ Settings
Creates a new Settings instance that read and writes values to
section
in SketchUp's preferences.
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
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
.
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
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
Untested on OSX. Creates sub-keys under Windows.
Creates a sub-section.
108 109 110 |
# File 'TT_Lib2/settings.rb', line 108 def sub_section( sub_section ) self.new( "#{@section}\\#{sub_section}" ) end |