Sha256: 91f4867fcc35884512a0959f242c02b7db822501d943917c604e221adca8f4d2

Contents?: true

Size: 1.39 KB

Versions: 5

Compression:

Stored size: 1.39 KB

Contents

# encoding: utf-8
require 'ostruct'

module Freeberry
  class SystemSettings < 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

5 entries across 5 versions & 1 rubygems

Version Path
freeberry-0.3.0 lib/freeberry/system_settings.rb
freeberry-0.2.9 lib/freeberry/system_settings.rb
freeberry-0.2.7 lib/freeberry/system_settings.rb
freeberry-0.2.6 lib/freeberry/system_settings.rb
freeberry-0.2.5 lib/freeberry/system_settings.rb