Class: EnumField::Builder
- Inherits:
-
Object
- Object
- EnumField::Builder
show all
- Defined in:
- lib/enum_field/builder.rb
Constant Summary
collapse
- METHODS =
%w[all names find_by_id find first last ids valid_id? valid_name? safe_find].freeze
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(target, options = {}) ⇒ Builder
Returns a new instance of Builder.
9
10
11
12
13
|
# File 'lib/enum_field/builder.rb', line 9
def initialize(target, options = {})
@target = target
@options = options
@members = {}
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
69
70
71
72
73
74
75
|
# File 'lib/enum_field/builder.rb', line 69
def method_missing(method_name, *args, &block)
if @target.respond_to?(method_name)
@target.send(method_name, *args, &block)
else
super
end
end
|
Instance Attribute Details
#members ⇒ Object
Returns the value of attribute members.
7
8
9
|
# File 'lib/enum_field/builder.rb', line 7
def members
@members
end
|
Instance Method Details
#[](value) ⇒ Object
24
25
26
27
|
# File 'lib/enum_field/builder.rb', line 24
def [](value)
unique_name = normalize_name(value)
@members[unique_name]
end
|
#all ⇒ Object
20
21
22
|
# File 'lib/enum_field/builder.rb', line 20
def all
@members.values
end
|
#find(id) ⇒ Object
55
56
57
|
# File 'lib/enum_field/builder.rb', line 55
def find(id)
find_by_id(id) || raise(EnumField::ObjectNotFound)
end
|
#find_by_id(id) ⇒ Object
Also known as:
safe_find
59
60
61
62
63
64
65
66
|
# File 'lib/enum_field/builder.rb', line 59
def find_by_id(id)
case id
when Array
all.select { |object| id.include?(object.id) }
else
all.detect { |object| object.id == id }
end
end
|
#first ⇒ Object
33
34
35
36
|
# File 'lib/enum_field/builder.rb', line 33
def first
key = names.first
@members[key]
end
|
#freeze! ⇒ Object
81
82
83
84
|
# File 'lib/enum_field/builder.rb', line 81
def freeze!
@members.freeze
freeze
end
|
#ids ⇒ Object
43
44
45
|
# File 'lib/enum_field/builder.rb', line 43
def ids
all.map(&:id)
end
|
#last ⇒ Object
38
39
40
41
|
# File 'lib/enum_field/builder.rb', line 38
def last
key = names.last
@members[key]
end
|
#member(name, options = {}) ⇒ Object
15
16
17
18
|
# File 'lib/enum_field/builder.rb', line 15
def member(name, options = {})
unique_name = normalize_name(name)
@members[unique_name] ||= create_new_object(unique_name, options)
end
|
#names ⇒ Object
29
30
31
|
# File 'lib/enum_field/builder.rb', line 29
def names
@members.keys
end
|
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
77
78
79
|
# File 'lib/enum_field/builder.rb', line 77
def respond_to_missing?(method_name, include_private = false)
@target.respond_to?(method_name) || super
end
|
#valid_id?(value) ⇒ Boolean
47
48
49
|
# File 'lib/enum_field/builder.rb', line 47
def valid_id?(value)
!value.nil? && ids.include?(value)
end
|
#valid_name?(value) ⇒ Boolean
51
52
53
|
# File 'lib/enum_field/builder.rb', line 51
def valid_name?(value)
!value.nil? && names.include?(normalize_name(value))
end
|