Sha256: 51d1f3c8750056c4458466d02c150ca792c42d72cea50dbbfd94741d98f5bde4

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.find_by identifier => scope_id.sub(scope_regexp, '')
      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.0.2 lib/switch_user/data_source.rb