Class: EnumFields::Registry

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/enum_fields/registry.rb

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry.



5
6
7
8
# File 'lib/enum_fields/registry.rb', line 5

def initialize
  @store = {}.with_indifferent_access
  super(@store)
end

Instance Method Details

#catalogObject



19
20
21
22
23
# File 'lib/enum_fields/registry.rb', line 19

def catalog
  sort_by { |key, _| key.to_s }.to_h.transform_values do |fields|
    fields.transform_values(&:values)
  end
end

#register(args = {}) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/enum_fields/registry.rb', line 10

def register(args = {})
  namespace = args.fetch(:namespace) { raise ArgumentError, "namespace is required" }
  accessor = args.fetch(:accessor) { raise ArgumentError, "accessor is required" }
  definition = args.fetch(:definition, {})

  @store[namespace] ||= {}.with_indifferent_access
  @store[namespace][accessor] = definition
end