Class: Unitwise::Base

Inherits:
Object
  • Object
show all
Includes:
Memoizable
Defined in:
lib/unitwise/base.rb

Overview

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

Direct Known Subclasses

Atom, Prefix

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.allArray

The list of tracked items.



11
12
13
# File 'lib/unitwise/base.rb', line 11

def self.all
  @all ||= data.map { |d| new d }
end

.find(string, method = :primary_code) ⇒ Object

Find a matching instance by a specified attribute.

Examples:

Unitwise::Atom.find('m')


22
23
24
25
26
27
28
29
30
31
# 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

Instance Method Details

#names=(names) ⇒ Object

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



35
36
37
# File 'lib/unitwise/base.rb', line 35

def names=(names)
  @names = Array(names)
end

#slugsArray

A set of method friendly names.



42
43
44
45
46
# 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) ⇒ String

String representation for the instance.



53
54
55
56
# 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