Sha256: ddc2506c7350e4451a22373e5ec3b85c0a0f14a76227664dd2ea377448bd3e51

Contents?: true

Size: 1006 Bytes

Versions: 3

Compression:

Stored size: 1006 Bytes

Contents

module ConfigureMe
  class Base
    class << self
      def persist_me
        @persisting = true
      end

      def persisting?
        @persisting ||= false
        @persisting && !ConfigureMe.persistence_klass.nil?
      end
    end
  end

  module Persisting

    def read_persist(name)
      if self.class.persisting?
        setting = ConfigureMe.persistence_klass.find_by_key(self.storage_key(name))

        unless setting.nil?
          YAML.load(setting.value)
        end
      else
        nil
      end
    end

    def write_persist(name, value)
      if self.class.persisting?
        setting = ConfigureMe.persistence_klass.find_or_create_by_key(self.storage_key(name))
        setting.value = value.to_yaml
        setting.save!
      else
        true
      end
    end

    def persist_guard(&block)
      if ConfigureMe.persistence_klass.respond_to?(:transaction)
        ConfigureMe.persistence_klass.transaction({}, &block)
      else
        block.call
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
configure_me-0.5.0 lib/configure_me/persisting.rb
configure_me-0.4.1 lib/configure_me/persisting.rb
configure_me-0.4.0 lib/configure_me/persisting.rb