Class: Unitwise::Base
- Inherits:
-
Object
- Object
- Unitwise::Base
- 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.
Class Method Summary collapse
-
.all ⇒ Array
The list of tracked items.
-
.find(string, method = :primary_code) ⇒ Object
Find a matching instance by a specified attribute.
Instance Method Summary collapse
-
#initialize(*args) ⇒ Base
constructor
Setup a new instance.
-
#names=(names) ⇒ Object
Setter for the names attribute.
-
#slugs ⇒ Array
A set of method friendly names.
-
#to_s ⇒ String
String representation for the instance.
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.
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.
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.
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.
59 60 61 |
# File 'lib/unitwise/base.rb', line 59 def to_s primary_code end |