Class: EnumField::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/enum_field/builder.rb

Instance Method Summary collapse

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

#allObject



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

#firstObject



35
# File 'lib/enum_field/builder.rb', line 35

def first; @sorted.first; end

#lastObject



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, options = {})
  obj, candidate_id = process_options(options)
  assign_id(obj, candidate_id)
  define_in_meta(name) { obj }
  save(name, obj)
  obj.freeze
end

#namesObject



23
24
25
# File 'lib/enum_field/builder.rb', line 23

def names
  @name2obj.keys
end