Sha256: 4547607a4f481397e067d45bf6919a146fb3befe259cf48c93722bf65b35b6a0
Contents?: true
Size: 1.53 KB
Versions: 6
Compression:
Stored size: 1.53 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. # @param mode [symbol] The attribute to for stringification # @return [String] # @api public def to_s(mode = :primary_code) res = send(mode) res.respond_to?(:each) ? res.first.to_s : res.to_s end end end
Version data entries
6 entries across 6 versions & 2 rubygems