Sha256: ab23948c5e3a6068662365653a009d548515a7b338124a11990b31fcb772ce33
Contents?: true
Size: 1.55 KB
Versions: 5
Compression:
Stored size: 1.55 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) || primary_code res.respond_to?(:each) ? res.first.to_s : res.to_s end end end
Version data entries
5 entries across 5 versions & 1 rubygems
Version | Path |
---|---|
unitwise-2.3.0 | lib/unitwise/base.rb |
unitwise-2.2.0 | lib/unitwise/base.rb |
unitwise-2.1.0 | lib/unitwise/base.rb |
unitwise-2.0.0 | lib/unitwise/base.rb |
unitwise-1.1.0 | lib/unitwise/base.rb |