Sha256: 085a555d045615379f116da88a71bca0e2e0a70a2dbd7494d090df7c8ae070f8

Contents?: true

Size: 1.35 KB

Versions: 6

Compression:

Stored size: 1.35 KB

Contents

module Enumerations
  module FinderMethods
    # Finds an enumeration by symbol or name
    #
    # Example:
    #
    #   Role.find(:admin)  => #<Enumerations::Value: @base=Role, @symbol=:admin...>
    #   Role.find('staff') => #<Enumerations::Value: @base=Role, @symbol=:staff...>
    #
    def find(key)
      case key
      when Symbol, String, Enumerations::Base then find_by_key(key.to_sym)
      end || find_by_primary_key(key)
    end

    # Finds all enumerations which meets given attributes.
    # Similar to ActiveRecord::QueryMethods#where.
    #
    # Example:
    #
    #   Role.find_by(name: 'Admin') => #<Enumerations::Value: @base=Role, @symbol=:admin...>
    #
    def where(**args)
      _values.values.select { |value| args.map { |k, v| value.attributes[k] == v }.all? }
    end

    # Finds an enumeration by defined attribute. Similar to ActiveRecord::FinderMethods#find_by
    #
    # Example:
    #
    #   Role.find_by(name: 'Admin') => #<Enumerations::Value: @base=Role, @symbol=:admin...>
    #
    def find_by(**args)
      where(args).first
    end

    def find_by_key(key)
      _values[key]
    end

    def find_by_primary_key(primary_key)
      value_from_symbol_index(primary_key) || value_from_symbol_index(primary_key.to_s.to_i)
    end

    private

    def value_from_symbol_index(key)
      _values[_symbol_index.key(key)]
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
enumerations-2.3.2 lib/enumerations/finder_methods.rb
enumerations-2.3.1 lib/enumerations/finder_methods.rb
enumerations-2.2.3 lib/enumerations/finder_methods.rb
enumerations-2.2.2 lib/enumerations/finder_methods.rb
enumerations-2.2.1 lib/enumerations/finder_methods.rb
enumerations-2.2.0 lib/enumerations/finder_methods.rb