Class: Enumeration
- Inherits:
-
Object
- Object
- Enumeration
- Extended by:
- Enumerable
- Includes:
- Comparable
- Defined in:
- lib/enumeration.rb
Class Attribute Summary collapse
-
.abbrs ⇒ Object
readonly
Returns the value of attribute abbrs.
-
.names ⇒ Object
readonly
Returns the value of attribute names.
-
.offset ⇒ Object
Returns the value of attribute offset.
-
.size ⇒ Object
readonly
Returns the value of attribute size.
-
.units ⇒ Object
readonly
Returns the value of attribute units.
Class Method Summary collapse
Instance Method Summary collapse
- #+(o) ⇒ Object
- #-(o) ⇒ Object
- #<=>(o) ⇒ Object
- #between?(a, b) ⇒ Boolean
- #coerce(o) ⇒ Object
-
#initialize(id) ⇒ Enumeration
constructor
A new instance of Enumeration.
- #inspect ⇒ Object
- #succ ⇒ Object
- #to_abbr ⇒ Object
- #to_i ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(id) ⇒ Enumeration
Returns a new instance of Enumeration.
4 5 6 |
# File 'lib/enumeration.rb', line 4 def initialize(id) @unit_id = id % self.class.size end |
Class Attribute Details
.abbrs ⇒ Object (readonly)
Returns the value of attribute abbrs.
68 69 70 |
# File 'lib/enumeration.rb', line 68 def abbrs @abbrs end |
.names ⇒ Object (readonly)
Returns the value of attribute names.
68 69 70 |
# File 'lib/enumeration.rb', line 68 def names @names end |
.offset ⇒ Object
Returns the value of attribute offset.
67 68 69 |
# File 'lib/enumeration.rb', line 67 def offset @offset end |
.size ⇒ Object (readonly)
Returns the value of attribute size.
68 69 70 |
# File 'lib/enumeration.rb', line 68 def size @size end |
.units ⇒ Object (readonly)
Returns the value of attribute units.
68 69 70 |
# File 'lib/enumeration.rb', line 68 def units @units end |
Class Method Details
.build_from(names) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/enumeration.rb', line 69 def build_from(names) @names = names.dup.freeze @abbrs = names.map { |n| n[0,3] }.freeze @size = @names.size @units = (0...@size).map { |n| new(n) }.freeze @names.each_with_index do |c,i| const = c.upcase const_get(const) rescue const_set(const, @units[i]) const_get(const[0,3]) rescue const_set(const[0,3], @units[i]) end end |
.each ⇒ Object
63 64 65 |
# File 'lib/enumeration.rb', line 63 def each units.each { |u| yield u } end |
.for(var) ⇒ Object
9 10 11 |
# File 'lib/enumeration.rb', line 9 def self.for(var) units[var.to_s =~ /^\d+$/ ? var.to_i - offset : abbrs.index(var[0,3].downcase.capitalize)] end |
.generate(names, offset = 0) ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/enumeration.rb', line 53 def self.generate(names, offset=0) klass = Class.new(self) klass.send(:build_from, names) klass.send(:offset=, offset) klass.instance_eval { undef generate } klass end |
Instance Method Details
#+(o) ⇒ Object
41 42 43 |
# File 'lib/enumeration.rb', line 41 def +(o) self.class.for((to_i + o.to_i) % self.class.size) end |
#-(o) ⇒ Object
45 46 47 |
# File 'lib/enumeration.rb', line 45 def -(o) self.class.for((to_i - o.to_i) % self.class.size) end |
#<=>(o) ⇒ Object
21 22 23 |
# File 'lib/enumeration.rb', line 21 def <=>(o) @unit_id <=> o.instance_variable_get("@unit_id") end |
#between?(a, b) ⇒ Boolean
17 18 19 |
# File 'lib/enumeration.rb', line 17 def between?(a,b) true # always in rings, change for non rings end |
#coerce(o) ⇒ Object
37 38 39 |
# File 'lib/enumeration.rb', line 37 def coerce(o) [self.class.for(o % self.class.size), self] end |
#inspect ⇒ Object
49 50 51 |
# File 'lib/enumeration.rb', line 49 def inspect "#<#{self.class.name.split("::").last}:#{to_s}>" end |
#succ ⇒ Object
13 14 15 |
# File 'lib/enumeration.rb', line 13 def succ self.class.units[(@unit_id + 1) % self.class.size] end |
#to_abbr ⇒ Object
33 34 35 |
# File 'lib/enumeration.rb', line 33 def to_abbr to_s[0,3] end |
#to_i ⇒ Object
29 30 31 |
# File 'lib/enumeration.rb', line 29 def to_i @unit_id + self.class.offset end |
#to_s ⇒ Object
25 26 27 |
# File 'lib/enumeration.rb', line 25 def to_s self.class.names[@unit_id] end |