Module: EnumerizeSchema

Defined in:
lib/enumerize_schema/errors.rb,
lib/enumerize_schema.rb,
lib/enumerize_schema/version.rb,
lib/enumerize_schema/configuration.rb

Defined Under Namespace

Classes: Configuration, MissingValuesError, SchemaFileNotFoundError, SchemaFileNotReadableError

Constant Summary collapse

VERSION =
"0.1.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configConfiguration (readonly)

Returns the EnumerizeSchema configuration object.

Returns:



16
17
18
# File 'lib/enumerize_schema.rb', line 16

def self.config
  @config ||= Configuration.new
end

Class Method Details

.attr_enum(attribute_name, **enumerize_options) ⇒ void

Note:

This method is marked as protected when added to the class, so it can’t be used from outside that class.

Note:

If the :in option is passed then this method will not check the schema file and forward all options to enumerize

This method returns an undefined value.

Defines an enumerized attribute.

Parameters:

  • attribute_name (Symbol)

    the name of the enumerized attribute

  • enumerize_options (Hash)

    options forwarded to enumerize

Raises:

See Also:



186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/enumerize_schema.rb', line 186

def attr_enum(attribute_name, **enumerize_options)
  if enumerize_options.include?(:in)
    enumerize(attribute_name, enumerize_options)
  else
    enum_values = Array(enumerize_schema.dig(*enumerize_schema_attributes_scope, attribute_name.to_s))

    if enum_values.empty?
      raise ::EnumerizeSchema::MissingValuesError.new(class_name: name, attribute_name: attribute_name)
    end

    enumerize(attribute_name, enumerize_options.merge(in: enum_values))
  end
end

.configure {|config| ... } ⇒ Object

Configure EnumerizeSchema.

Examples:

EnumerizeSchema.configure do |config|
  config.schema_file = Rails.root.join("config", "enumerize", "schema.yml")
end

Yield Parameters:

  • config (Configuration)

    the EnumerizeSchema configuration object



28
29
30
# File 'lib/enumerize_schema.rb', line 28

def self.configure
  yield(config)
end

.enumerize_schemaObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



162
163
164
165
166
167
168
169
# File 'lib/enumerize_schema.rb', line 162

def enumerize_schema
  @__enumerize_schema ||=
    if enumerize_schema_file.to_s.empty?
      ::EnumerizeSchema.schema
    else
      YAML.load_file(enumerize_schema_file) || {}
    end
end

.enumerize_schema_attributes_scopeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



172
173
174
# File 'lib/enumerize_schema.rb', line 172

def enumerize_schema_attributes_scope
  @__enumerize_schema_attributes_scope ||= ActiveSupport::Inflector.underscore(name.to_s).split("/")
end

.enumerize_schema_fileObject

The path of the schema file for the specific class extended by EnumerizeSchema. This method will return nil if a custom schema file was not specified.

Examples:

A class with a custom schema file

class User
  extend EnumerizeSchema

  self.enumerize_schema_file = Rails.root.join("config", "enumerize", "user.yml")
end

User.enumerize_schema_file
# => #<Pathname:config/enumerize/user.yml>

Subsclasses inherit their parent’s schema file

class User
  extend EnumerizeSchema

  self.enumerize_schema_file = Rails.root.join("config", "enumerize", "user.yml")
end

class Member < User
end

User.enumerize_schema_file
# => #<Pathname:config/enumerize/user.yml>
Member.enumerize_schema_file
# => #<Pathname:config/enumerize/user.yml>

Subsclasses can also use their own schema file instead of their parent’s

class User
  extend EnumerizeSchema

  self.enumerize_schema_file = Rails.root.join("config", "enumerize", "user.yml")
end

class Member < User
  self.enumerize_schema_file = Rails.root.join("config", "enumerize", "member.yml")
end

User.enumerize_schema_file
# => #<Pathname:config/enumerize/user.yml>
Member.enumerize_schema_file
# => #<Pathname:config/enumerize/member.yml>

Subsclasses can also use the default schema file instead of their parent’s

class User
  extend EnumerizeSchema

  self.enumerize_schema_file = Rails.root.join("config", "enumerize", "user.yml")
end

class Member < User
  self.enumerize_schema_file = nil
end

User.enumerize_schema_file
# => #<Pathname:config/enumerize/user.yml>
Member.enumerize_schema_file
# => nil

A class without a custom schema file

class User
  extend EnumerizeSchema
end

User.enumerize_schema_file
# => nil


130
131
132
133
134
# File 'lib/enumerize_schema.rb', line 130

def enumerize_schema_file
  return @__enumerize_schema_file if instance_variable_defined?(:@__enumerize_schema_file)

  @__enumerize_schema_file = superclass.instance_variable_get(:@__enumerize_schema_file)
end

.enumerize_schema_file=(value) ⇒ Pathname

Override the path to the enumerized attribute schema file for the current class.

Examples:

class User < ApplicationRecord
  include EnumerizeSchema

  self.enumerize_schema_file = Rails.root.join("config", "enumerize", "user.yml")
end

Parameters:

  • value (String, File, Pathname)

    the path to the schema file containing all the enum values for the current class

Returns:

  • (Pathname)

    the path to the schema file containing all the enum values for the current class

Raises:



149
150
151
152
153
154
155
156
157
158
159
# File 'lib/enumerize_schema.rb', line 149

def enumerize_schema_file=(value)
  if value.to_s.empty?
    @__enumerize_schema_file = nil
  elsif !File.file?(value)
    raise SchemaFileNotFoundError.new(schema_file: value)
  elsif !File.readable?(value)
    raise SchemaFileNotReadableError.new(schema_file: value)
  else
    @__enumerize_schema_file = Pathname.new(value)
  end
end