Sha256: b1b14cf1b33921a4e2ce9107d3056cd422d4eae9e59b3e5b12154d96d1961afe

Contents?: true

Size: 848 Bytes

Versions: 4

Compression:

Stored size: 848 Bytes

Contents

# frozen_string_literal: true

module Solid::Model
  # Implementation based on ActiveModel::Access
  # https://github.com/rails/rails/blob/7-1-stable/activemodel/lib/active_model/access.rb
  module Access
    # Returns a hash of the given methods with their names as keys and returned
    # values as values.
    #
    #   person = Person.new(id: 1, name: "bob")
    #   person.slice(:id, :name)
    #   => { "id" => 1, "name" => "bob" }
    def slice(*methods)
      methods.flatten.index_with { |method| public_send(method) }.with_indifferent_access
    end

    # Returns an array of the values returned by the given methods.
    #
    #   person = Person.new(id: 1, name: "bob")
    #   person.values_at(:id, :name)
    #   => [1, "bob"]
    def values_at(*methods)
      methods.flatten.map! { |method| public_send(method) }
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
solid-process-0.4.0 lib/solid/model/access.rb
solid-process-0.3.0 lib/solid/model/access.rb
solid-process-0.2.0 lib/solid/model/access.rb
solid-process-0.1.0 lib/solid/model/access.rb