Class: Gooby::Configuration

Inherits:
GoobyObject show all
Defined in:
lib/gooby_configuration.rb

Overview

This is a singleton class whose values are loaded from a YAML file when your GoobyCommand class is created. The default filename is ‘gooby_config.yaml“’.

The YAML file contains configuration parameters, such as your Google Map key, map HTML options, points of interest, and courses.

Constant Summary collapse

@@singleton_instance =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from GoobyKernel

#character_align, #default_delimiter, #invalid_altitude, #invalid_heartbeat, #invalid_latitude, #invalid_longitude, #invalid_time, #project_author, #project_copyright, #project_date, #project_embedded_comment, #project_license, #project_name, #project_version_number, #project_year, #read_as_ascii_lines, #read_lines, #strip_lines, #tokenize

Constructor Details

#initialize(yaml_filename) ⇒ Configuration

Returns a new instance of Configuration.



37
38
39
40
# File 'lib/gooby_configuration.rb', line 37

def initialize(yaml_filename) 
  @yaml_filename = yaml_filename
  File.open("#{@yaml_filename}") { |fn| @configuration = YAML::load(fn) }
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



24
25
26
# File 'lib/gooby_configuration.rb', line 24

def configuration
  @configuration
end

#yaml_filenameObject (readonly)

Returns the value of attribute yaml_filename.



24
25
26
# File 'lib/gooby_configuration.rb', line 24

def yaml_filename
  @yaml_filename
end

Class Method Details

.get_configObject



33
34
35
# File 'lib/gooby_configuration.rb', line 33

def self.get_config
  @@singleton_instance
end

.init(yaml_filename = 'gooby_config.yaml') ⇒ Object



28
29
30
31
# File 'lib/gooby_configuration.rb', line 28

def Configuration.init(yaml_filename='gooby_config.yaml')
  return @@singleton_instance if  @@singleton_instance
   @@singleton_instance = new(yaml_filename)      
end

Instance Method Details

#get(name) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/gooby_configuration.rb', line 42

def get(name)    
  if name == nil
    return ''
  end
  s = @configuration["#{name}"]

  # Provide "sensible defaults".
  if s == nil
    if (name == '') 
      return ''
    elsif (name == 'english_metric')  
      return 'english'
    elsif (name == 'gmap_first_tkpt_number')  
      return 1
    elsif (name == 'gmap_last_tkpt_number')   
      return 5000
    elsif (name == 'gmap_map_element_id')     
      return 'map'  
    elsif (name == 'gmap_height')             
      return '600'
    elsif (name == 'gmap_icon_url_base')             
      return 'http://www.your-web-site.com/gicons/'          
    elsif (name == 'gmap_key')                
      return 'enter your Google Map Key here'       
    elsif (name == 'gmap_type_control')       
      return true
    elsif (name == 'gmap_approx_max_points')  
      return '200'
    elsif (name == 'gmap_gen_comments')       
      return true
    elsif (name == 'gmap_size_control')       
      return nil
    elsif (name == 'gmap_type')               
      return 'G_NORMAL_MAP'  
    elsif (name == 'gmap_zoom_level')         
      return 5 
    else 
      return ''
    end
  end
  s
end

#get_course(number) ⇒ Object



107
108
109
110
# File 'lib/gooby_configuration.rb', line 107

def get_course(number)
  val = @configuration["course.#{number}"]
  (val) ? Course.new(val) : nil
end

#get_poi(number) ⇒ Object



102
103
104
105
# File 'lib/gooby_configuration.rb', line 102

def get_poi(number)
  val = @configuration["poi.#{number}"]
  (val) ? Point.new(val) : nil
end


85
86
87
88
89
90
# File 'lib/gooby_configuration.rb', line 85

def print_all
  @configuration.keys.sort.each { |key| 
    value = get(key)
    puts "#{key}: #{value}" 
  }
end


92
93
94
95
96
97
98
99
100
# File 'lib/gooby_configuration.rb', line 92

def print_all_poi
  @configuration.keys.sort.each { |key|
    if (key.match(/poi[\.]/))
      val = @configuration["#{key}"]
      poi = Point.new(val)
      puts poi.to_s
    end
  }
end

#sizeObject



112
113
114
# File 'lib/gooby_configuration.rb', line 112

def size
  @configuration.size
end

#to_sObject

Return a String containing yaml filename and entry count.



117
118
119
# File 'lib/gooby_configuration.rb', line 117

def to_s
  return "# Configuration: filename: #{@yaml_filename} entries: #{@configuration.size}"
end