Sha256: 26c879d7b95dbb2239cd05c4633ff80701d165208f7768c31fa222755047462b

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 KB

Contents

module SwitchUser
  class DataSources
    attr_reader :sources

    def initialize(sources)
      @sources = sources
    end

    def all
      sources.flat_map { |source| source.all }
    end

    def find_scope_id(scope_id)
      sources.map { |source| source.find_scope_id(scope_id) }.compact.first
    end
  end

  class DataSource
    attr_reader :loader, :scope, :identifier, :name

    def initialize(loader, scope, identifier, name)
      @loader = loader
      @scope = scope
      @identifier = identifier
      @name = name
    end

    def all
      loader.call.map { |user| Record.new(user, self) }
    end

    def find_scope_id(scope_id)
      scope_regexp = /\A#{scope}_/
      return unless scope_id =~ scope_regexp

      user = loader.call.where(identifier => scope_id.sub(scope_regexp, '')).first
      Record.new(user, self)
    end
  end

  class GuestDataSource
    def all
      [GuestRecord.new]
    end

    def find_scope_id(scope_id)
    end
  end

  class Record
    attr_reader :user, :source

    def initialize(user, source)
      @user = user
      @source = source
    end

    def label
      user.send(source.name)
    end

    def scope
      source.scope
    end

    def scope_id
      "#{source.scope}_#{user.send(source.identifier)}"
    end
  end

  class GuestRecord
    def label
      "Guest"
    end

    def scope_id
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
switch_user-1.5.0 lib/switch_user/data_source.rb