Sha256: ce4295ef95af8d51f8f23dc76f7e132118e267b76ca974de0a98b73c8f72b7de

Contents?: true

Size: 1.12 KB

Versions: 2

Compression:

Stored size: 1.12 KB

Contents

require "active_worksheet/adapters/file_adapter"
require "active_worksheet/adapters/google_sheets_adapter"
require "active_resource/threadsafe_attributes"
require "ostruct"

module ActiveWorksheet
  class Base < OpenStruct
    class << self
      include ThreadsafeAttributes

      threadsafe_attribute :source, :authorization

      def all
        adapter.all.map do |row|
          new(row)
        end
      end

      def find(index)
        new(adapter.find(index))
      end

      def first
        new(adapter.first)
      end

      def last
        new(adapter.last)
      end

      def count
        adapter.count
      end

      def adapter
        if is_source_local?
          ActiveWorksheet::Adapters::FileAdapter.new(source: File.expand_path(source))
        else
          ActiveWorksheet::Adapters::GoogleSheetsAdapter.new(
            source: source,
            authorization: authorization
          )
        end
      end

      def is_source_local?
        self.source.is_a?(File) || File.exists?(File.expand_path(self.source))
      end

      def is_source_remote?
        !is_source_local?
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
active_worksheet-0.1.3 lib/active_worksheet/base.rb
active_worksheet-0.1.2 lib/active_worksheet/base.rb