Module: Watobo::Conf

Defined in:
lib/watobo/config.rb

Overview

:nodoc: all

Constant Summary collapse

@@settings =
Hash.new
@@modules =
[]

Class Method Summary collapse

Class Method Details

.add(group, settings) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
84
85
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/watobo/config.rb', line 28

def self.add(group, settings)
  #   puts "* create new configuration for #{group}"

  module_eval("module #{group}; @settings = #{settings}; end")
  m = const_get(group)
  m.module_eval do
    def self.to_file
   #   n = self.to_s.gsub(/(Watobo)?::/, "/").gsub(/([A-Z])([A-Z][a-z])/, '\1_\2').gsub(/([a-z\d])([A-Z])/, '\1_\2').tr("-","_").downcase
      n = Watobo::Utils.snakecase self.to_s.gsub(/(Watobo)?::/, "/")
      n << ".yml"
    end

    def self.update(filename=nil, &b)
      n = self.to_file
      file = filename unless filename.nil?
      file = File.join( Watobo::Conf::General.working_directory, n )
      if File.exist? file
        puts " [#{self}] update settings from file #{file}" if $DEBUG
        @settings.update YAML.load_file(file)
      else
        puts "! [#{self}] could not update settings from file #{file}" if $DEBUG
      end
    end
    
    # returns the group name of the module
    # e.g. the group name of Watobo::Conf::Interceptor would be Interceptor
    def self.group_name
      self.to_s.gsub(/.*::/,"")
    end

    def self.set(settings)
      return false unless settings.is_a? Hash
      @settings = YAML.load(YAML.dump(settings))
    end

    def self.save_session( *filter, &b)
      #raise ArgumentError, "Need a valid Watobo::DataStore" unless data_store.respond_to? :save_project_settings
      s = filter_settings filter
      s = yield s if block_given?
     # puts group_name
      Watobo::DataStore.save_session_settings( group_name, s )
    end

    def self.save_project( *filter, &b)
     # raise ArgumentError, "Need a valid Watobo::DataStore" unless data_store.respond_to? :save_project_settings
      s = filter_settings filter
      s = yield s if block_given?
     # puts @settings.to_yaml
     # puts s.to_yaml
      Watobo::DataStore.save_project_settings(group_name, s)
    end
    
    def self.load_session(prefs={}, &b)
      
      p = { :update => true }
      p.update prefs
      
      s = Watobo::DataStore.load_session_settings(group_name)
      return false if s.nil?
      
      if p[:update] == true
        @settings.update s
      else
        @settings = s
      end
    end
    
    def self.load_project(prefs={}, &b)
      p = { :update => true }
      p.update prefs
      
      s = Watobo::DataStore.load_project_settings(group_name)
      return false if s.nil?
      
      if p[:update] == true
        @settings.update s
      else
        @settings = s
      end
    end

    def self.filter_settings(f)
      s = YAML.load(YAML.dump(@settings))
      
      return s unless f.is_a? Array
      return s if f.empty?
      #return s unless s.respond_to? :each_key
      
      s.each_key do |k|
          s.delete k unless f.include? k
      end
      s
    end

    def self.save(path=nil, *filter, &b)

      n = self.to_file
      p = Conf::General.working_directory
      unless path.nil?
        if File.exist? path
        p = path
        end
      end

      file = File.join( p, n )

      s = filter_settings filter
      

      yield s if block_given?

      if File.exist?(File.dirname(file))
        puts "* save config #{self} to: #{file}"
        puts s.to_yaml
       
        File.open(file, "w") { |fh|
          YAML.dump(s, fh)
        }
      else
        puts "Could not save file to #{File.dirname(file)}"
      end
    end

    def self.respond_to?(f)
      #  puts "* respond_to?"
      # puts f
      return true if @settings.has_key? f.to_sym
      #  puts @settings.to_yaml
      super
    end

    def self.dump
      @settings
    end
    
    def self.to_h
      @settings
    end
    

    #@@settings = settings
    def self.method_missing(name, *args, &block)
      #  puts "* instance method missing (#{name})"
      if name =~ /(.*)=$/
      @settings.has_key? $1.to_sym || super
      @settings[$1.to_sym] = args[0]
      else
      # puts @settings[name.to_sym]
      @settings[name.to_sym]

      end
    end

    # TODO: create a class-instance of the module itself, so it can be referenced like @scanner.scope
    # before creating the reference also check if there's another class-instance variable with the same name
    def self.included_UNUSED(clazz)
      puts "* #{self} gets included into #{clazz}"
      @settings.each_key do |k|
        puts "* add method for #{k}"

        clazz.class_eval "
        @@#{k} ||= #{self}.#{k}

        def #{k}=(value)
        @@#{k} = value
        end

        def #{k}
        @@#{k}
        end
        "

      end
    end

  end
  @@modules << m
end

.each(&b) ⇒ Object



9
10
11
12
13
14
# File 'lib/watobo/config.rb', line 9

def self.each(&b)
  @@modules.each do |m|
    yield m if block_given?
  end
  @@modules.length
end

.load_project_settingsObject



16
17
18
19
20
# File 'lib/watobo/config.rb', line 16

def self.load_project_settings()
  @@modules.each do |m|
    m.load_project()
  end
end

.load_session_settingsObject



22
23
24
25
26
# File 'lib/watobo/config.rb', line 22

def self.load_session_settings()
  @@modules.each do |m|
    m.load_session()
  end
end