Class: EnumField::Builder

Inherits:
Object
  • Object
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?].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



68
69
70
71
72
73
74
# File 'lib/enum_field/builder.rb', line 68

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

#membersObject (readonly)

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

#allObject



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



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 then
    all.select { |object| id.include?(object.id) }
  else
    all.detect { |object| object.id == id }
  end
end

#firstObject



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

def first
  key = names.first
  @members[key]
end

#freeze!Object



80
81
82
83
# File 'lib/enum_field/builder.rb', line 80

def freeze!
  @members.freeze
  freeze
end

#idsObject



43
44
45
# File 'lib/enum_field/builder.rb', line 43

def ids
  all.map(&:id)
end

#lastObject



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

#namesObject



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

Returns:

  • (Boolean)


76
77
78
# File 'lib/enum_field/builder.rb', line 76

def respond_to_missing?(method_name, include_private = false)
  @target.respond_to?(method_name) || super
end

#valid_id?(value) ⇒ Boolean

Returns:

  • (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

Returns:

  • (Boolean)


51
52
53
# File 'lib/enum_field/builder.rb', line 51

def valid_name?(value)
  !value.nil? && names.include?(normalize_name(value))
end