Class: EnumField::Builder
- Inherits:
-
Object
- Object
- EnumField::Builder
- Defined in:
- lib/enum_field/builder.rb
Instance Method Summary collapse
- #all ⇒ Object
- #find(id) ⇒ Object
- #find_by_id(id) ⇒ Object
- #first ⇒ Object
-
#initialize(target) ⇒ Builder
constructor
A new instance of Builder.
- #last ⇒ Object
- #member(name, options = {}) ⇒ Object
- #names ⇒ Object
Constructor Details
#initialize(target) ⇒ Builder
Returns a new instance of Builder.
3 4 5 6 7 8 9 |
# File 'lib/enum_field/builder.rb', line 3 def initialize(target) @target = target @next_id = 0 @id2obj = {} @name2obj = {} @sorted = [] end |
Instance Method Details
#all ⇒ Object
19 20 21 |
# File 'lib/enum_field/builder.rb', line 19 def all @sorted.dup end |
#find(id) ⇒ Object
27 28 29 |
# File 'lib/enum_field/builder.rb', line 27 def find(id) find_by_id(id) or raise EnumField::ObjectNotFound end |
#find_by_id(id) ⇒ Object
31 32 33 |
# File 'lib/enum_field/builder.rb', line 31 def find_by_id(id) @id2obj[id.to_i] end |
#first ⇒ Object
35 |
# File 'lib/enum_field/builder.rb', line 35 def first; @sorted.first; end |
#last ⇒ Object
36 |
# File 'lib/enum_field/builder.rb', line 36 def last; @sorted.last; end |
#member(name, options = {}) ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/enum_field/builder.rb', line 11 def member(name, = {}) obj, candidate_id = () assign_id(obj, candidate_id) (name) { obj } save(name, obj) obj.freeze end |
#names ⇒ Object
23 24 25 |
# File 'lib/enum_field/builder.rb', line 23 def names @name2obj.keys end |