Sha256: f20529eab53879aad13e5cd52e13ff42b0d66add47ae7a2012d8a51d7e76cc9c

Contents?: true

Size: 1.39 KB

Versions: 5

Compression:

Stored size: 1.39 KB

Contents

module Unitwise
  # The base class that Atom and Prefix are extended from. This class provides
  # shared functionality for said classes.
  class Base
    liner :names, :primary_code, :secondary_code, :symbol, :scale
    include Memoizable

    # The list of tracked items.
    # @return [Array] An array of memoized instances.
    # @api public
    def self.all
      @all ||= data.map { |d| new d }
    end

    # Find a matching instance by a specified attribute.
    # @param string [String] The search term
    # @param method [Symbol] The attribute to search by
    # @return The first matching instance
    # @example
    #   Unitwise::Atom.find('m')
    # @api public
    def self.find(string, method = :primary_code)
      all.find do |i|
        key = i.send(method)
        if key.is_a? Array
          key.include?(string)
        else
          key == string
        end
      end
    end

    # Setter for the names attribute. Will always set as an array.
    # @api semipublic
    def names=(names)
      @names = Array(names)
    end

    # A set of method friendly names.
    # @return [Array] An array of strings
    # @api semipublic
    def slugs
      names.map do |n|
        n.downcase.strip.gsub(/\s/, '_').gsub(/\W/, '')
      end
    end
    memoize :slugs

    # String representation for the instance.
    # @return [String]
    # @api public
    def to_s
      primary_code
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
unitwise-0.8.1 lib/unitwise/base.rb
unitwise-0.8.0 lib/unitwise/base.rb
unitwise-0.7.1 lib/unitwise/base.rb
unitwise-0.7.0 lib/unitwise/base.rb
unitwise-0.6.2 lib/unitwise/base.rb