Class: Sunrise::SystemSettings

Inherits:
Utils::Settingslogic show all
Defined in:
lib/sunrise/system_settings.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Utils::Settingslogic

[], #[], []=, #[]=, #create_accessor_for, #create_accessors!, get, #initialize, load!, #method_missing, name, namespace, reload!, source

Constructor Details

This class inherits a constructor from Sunrise::Utils::Settingslogic

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Sunrise::Utils::Settingslogic

Class Method Details

.hashes_to_openstructs(obj, memo = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/sunrise/system_settings.rb', line 29

def hashes_to_openstructs(obj, memo={})
  return obj unless Hash === obj
  
  memo[obj.object_id] ||= OpenStruct.new( Hash[
      *obj.inject( [] ) { |a, (k, v)|
        a.push k, hashes_to_openstructs( v, memo )
      }
    ])
end

.ostructObject



10
11
12
# File 'lib/sunrise/system_settings.rb', line 10

def ostruct
  instance.serialize
end

.update_settings(attributes) ⇒ Object



14
15
16
17
# File 'lib/sunrise/system_settings.rb', line 14

def update_settings(attributes)
  instance.update attributes
  instance.save
end

.write(hash) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/sunrise/system_settings.rb', line 19

def write(hash)
  begin
    File.open(source, 'w') do |file|
      file.write hash.to_yaml
    end
  rescue Exception => e
    return false
  end
end

Instance Method Details

#reloadObject



53
54
55
56
57
58
# File 'lib/sunrise/system_settings.rb', line 53

def reload
  @instance = nil
  @struct = nil
  
  return true
end

#saveObject



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/sunrise/system_settings.rb', line 40

def save
  if self.class.namespace
    hash = YAML.load(ERB.new(File.read(self.class.source)).result).to_hash
    hash[self.class.namespace] ||= {}
    hash[self.class.namespace].update self
  else
    hash = self
  end
    
  self.class.write(hash)
  reload
end

#serializeObject



60
61
62
63
# File 'lib/sunrise/system_settings.rb', line 60

def serialize
  @struct ||= self.class.hashes_to_openstructs(self)   
  @struct
end