Class: Unitwise::Base

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(*args) ⇒ Base

Setup a new instance. Takes a hash of attributes, or an array of attribute values.



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

def initialize(*args)
  super(*args)
  freeze
end

Class Method Details

.all ⇒ Array

The list of tracked items.

Returns:

  • (Array) —

    An array of memoized instances.



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')

Parameters:

  • string (String) —

    The search term

  • method (Symbol) (defaults to: :primary_code) —

    The attribute to search by

Returns:

  • The first matching instance



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.respond_to?(:each)
      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.



43
44
45
# File 'lib/unitwise/base.rb', line 43

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

#slugs ⇒ Array

A set of method friendly names.

Returns:

  • (Array) —

    An array of strings



50
51
52
53
54
# File 'lib/unitwise/base.rb', line 50

def slugs
  names.map do |n|
    n.downcase.strip.gsub(/\s/, '_').gsub(/\W/, '')
  end
end

#to_s ⇒ String

String representation for the instance.

Returns:

  • (String)


59
60
61
# File 'lib/unitwise/base.rb', line 59

def to_s
  primary_code
end