Sha256: ba7d0704d0923d3fddc09ea78e059abe986f6eeb7c6dac50eac679a5683c749d

Contents?: true

Size: 1.62 KB

Versions: 1

Compression:

Stored size: 1.62 KB

Contents

module Ubiquitously
  module Storage
    class Base
      def initialize(*)
        
      end
      
      def save(cookies, credentials)
        
      end
      
      def load
        
      end
    end
    
    class FileSystem < Base
      attr_accessor :path
      
      def initialize(path)
        self.path = path
      end
      
      def save(cookies, credentials)
        write("#{path}/cookies.yml", cookies)
        write("#{path}/credentials.yml", credentials)
      end
      
      def load
        {
          :cookies => read("#{path}/cookies.yml"),
          :credentials => read("#{path}/credentials.yml")
        }
      end
      
      def read(path)
        result = File.exists?(path) ? YAML.load_file(path) : {}
        result = {} unless result.is_a?(Hash)
        result
      end
      
      def write(path, content)
        File.open(path, "w+") { |file| file.puts YAML.dump(content) }
      end
    end
    
    class ActiveRecord < Base
      attr_accessor :record, :cookies_attribute, :credentials_attribute
      
      def initialize(record, cookies_attribute, credentials_attribute)
        self.record = record
        self.cookies_attribute = cookies_attribute
        self.credentials_attribute = credentials_attribute
      end
      
      def save(cookies, credentials)
        record.update_attributes(
          cookies_attribute => cookies,
          credentials_attribute => credentials
        )
      end
      
      def load
        {
          :cookies => record.read_attribute(cookies_attribute),
          :credentials => record.read_attribute(credentials_attribute)
        }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ubiquitously-0.1.0 lib/ubiquitously/support/storage.rb