Sha256: 422c5cebc082f47e608b4468950f1bd7281c59b289f0603c26aca1413d8c6bad

Contents?: true

Size: 1.4 KB

Versions: 9

Compression:

Stored size: 1.4 KB

Contents

# encoding: utf-8
require 'ostruct'

module Sunrise
  class SystemSettings < Utils::Settingslogic
    source Rails.root.join("config", "application.yml")
    #namespace (Rails.env.to_s || 'development')
    
    class << self
      def ostruct
        instance.serialize
      end
      
      def update_settings(attributes)
        instance.update attributes
        instance.save
      end
      
      def write(hash)
        begin
          File.open(source, 'w') do |file|
            file.write hash.to_yaml
          end
        rescue Exception => e
          return false
        end
      end
      
      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
    end
    
    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
    
    def reload
      @instance = nil
      @struct = nil
      
      return true
    end
    
    def serialize
      @struct ||= self.class.hashes_to_openstructs(self)   
      @struct
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
sunrise-core-0.2.2 lib/sunrise/system_settings.rb
sunrise-core-0.2.1 lib/sunrise/system_settings.rb
sunrise-core-0.2.0 lib/sunrise/system_settings.rb
sunrise-core-0.1.5 lib/sunrise/system_settings.rb
sunrise-core-0.1.4 lib/sunrise/system_settings.rb
sunrise-core-0.1.3 lib/sunrise/system_settings.rb
sunrise-core-0.1.2 lib/sunrise/system_settings.rb
sunrise-core-0.1.1 lib/sunrise/system_settings.rb
sunrise-core-0.1.0 lib/sunrise/system_settings.rb