class Unitwise::Base

The base class that Atom and Prefix are extended from. This class provides shared functionality for said classes.

Public Class Methods

all() click to toggle source

The list of tracked items. @return [Array] An array of memoized instances. @api public

# File lib/unitwise/base.rb, line 11
def self.all
  @all ||= data.map { |d| new d }
end
find(string, method = :primary_code) click to toggle source

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

# File lib/unitwise/base.rb, line 22
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

Public Instance Methods

names=(names) click to toggle source

Setter for the names attribute. Will always set as an array. @api semipublic

# File lib/unitwise/base.rb, line 35
def names=(names)
  @names = Array(names)
end
slugs() click to toggle source

A set of method friendly names. @return [Array] An array of strings @api semipublic

# File lib/unitwise/base.rb, line 42
def slugs
  names.map do |n|
    n.downcase.strip.gsub(/\s/, '_').gsub(/\W/, '')
  end
end
to_s(mode = :primary_code) click to toggle source

String representation for the instance. @param mode [symbol] The attribute to for stringification @return [String] @api public

# File lib/unitwise/base.rb, line 53
def to_s(mode = :primary_code)
  res = send(mode) || primary_code
  res.respond_to?(:each) ? res.first.to_s : res.to_s
end