Sha256: 0ce9a4cd2e880eade69bbbbb61fdad2e11b353f8d8435217e457f206cffe55fd

Contents?: true

Size: 1.7 KB

Versions: 26

Compression:

Stored size: 1.7 KB

Contents

module Foobara
  module Enumerated
    module Accessors
      class ValueNotAllowed < StandardError; end
      class CannotDetermineModuleAutomatically; end

      include Concern

      module ClassMethods
        def enumerated(attribute_name, values_source = nil)
          original_values_source = values_source

          if values_source.nil?
            module_name = Util.classify(attribute_name)

            values_source = begin
              Util.const_get_up_hierarchy(self, module_name)
            rescue NameError
              # :nocov:
              raise CannotDetermineModuleAutomatically,
                    "could not find a module for #{module_name}. Maybe consider passing it in explicitly."
              # :nocov:
            end
          end

          values = Values.new(values_source)

          attribute_name = attribute_name.to_sym

          # :nocov:
          unless respond_to?(:enumerated_type_metadata)
            # :nocov:
            class << self
              attr_accessor :enumerated_type_metadata
            end

            self.enumerated_type_metadata = {}
          end

          attr_reader attribute_name

          define_method "#{attribute_name}=" do |value|
            value = Values.normalize_value(value)

            if !value.nil? && !values.all_values.include?(value)
              raise ValueNotAllowed, "Received #{value} for #{attribute_name} but expected one of #{values.all_values}"
            end

            instance_variable_set("@#{attribute_name}", value)
          end

          enumerated_type_metadata[attribute_name] = {
            original_values_source:,
            values_source:,
            values:
          }
        end
      end
    end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
foobara-0.0.26 projects/enumerated/src/accessors.rb
foobara-0.0.25 projects/enumerated/src/accessors.rb
foobara-0.0.24 projects/enumerated/src/accessors.rb
foobara-0.0.23 projects/enumerated/src/accessors.rb
foobara-0.0.22 projects/enumerated/src/accessors.rb
foobara-0.0.21 projects/enumerated/src/accessors.rb
foobara-0.0.20 projects/enumerated/src/accessors.rb
foobara-0.0.19 projects/enumerated/src/accessors.rb
foobara-0.0.18 projects/enumerated/src/accessors.rb
foobara-0.0.17 projects/enumerated/src/accessors.rb
foobara-0.0.16 projects/enumerated/src/accessors.rb
foobara-0.0.15 projects/enumerated/src/accessors.rb
foobara-0.0.14 projects/enumerated/src/accessors.rb
foobara-0.0.13 projects/enumerated/src/accessors.rb
foobara-0.0.12 projects/enumerated/src/accessors.rb
foobara-0.0.11 projects/enumerated/src/accessors.rb
foobara-0.0.10 projects/enumerated/src/accessors.rb
foobara-0.0.9 projects/enumerated/src/accessors.rb
foobara-0.0.8 projects/enumerated/src/accessors.rb
foobara-0.0.7 projects/enumerated/src/accessors.rb