Module: EnumKit
- Defined in:
- lib/enum_kit.rb,
lib/enum_kit/helpers.rb,
lib/enum_kit/constants.rb,
lib/enum_kit/active_record_extensions/schema_dumper.rb,
lib/enum_kit/active_record_extensions/migration/command_recorder.rb,
lib/enum_kit/active_record_extensions/connection_adapters/postgresql_adapter.rb,
lib/enum_kit/active_record_extensions/connection_adapters/postgresql/column_dumper.rb,
lib/enum_kit/active_record_extensions/connection_adapters/postgresql/schema_dumper.rb
Overview
:nodoc:
Defined Under Namespace
Modules: ActiveRecordExtensions
Constant Summary collapse
- VERSION =
Returns The gem’s semantic version number.
'0.2.3'
Class Method Summary collapse
-
.load! ⇒ Object
Queue loading of the patches/extensions and database type registration for when ‘ActiveRecord` has loaded.
-
.load_extensions! ⇒ Object
Load the ‘ActiveRecord` extensions.
-
.load_patches! ⇒ Object
Load the ‘ActiveRecord` monkey patches.
-
.register_database_type! ⇒ Object
Register ‘:enum` as a native database type.
-
.sanitize_name!(name) ⇒ String
Sanitize the name of the enum.
-
.sanitize_value!(value) ⇒ Array
Sanitize a single value of an enum.
-
.sanitize_values!(values) ⇒ Array
Sanitize the values of an enum.
-
.sqlize(value) ⇒ String
Convert a value into a String that can be used in SQL.
-
.underscore(value) ⇒ String
Makes an underscored, lowercase form from the expression in the string.
Class Method Details
.load! ⇒ Object
Queue loading of the patches/extensions and database type registration for when ‘ActiveRecord` has loaded.
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/enum_kit.rb', line 11 def self.load! require 'active_record' require 'active_record/connection_adapters/postgresql_adapter' require 'active_support/lazy_load_hooks' ActiveSupport.on_load(:active_record) do EnumKit.load_patches! EnumKit.load_extensions! EnumKit.register_database_type! end end |
.load_extensions! ⇒ Object
Load the ‘ActiveRecord` extensions.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/enum_kit.rb', line 36 def self.load_extensions! %w[ ConnectionAdapters::PostgreSQL::ColumnDumper ConnectionAdapters::PostgreSQL::SchemaDumper ConnectionAdapters::PostgreSQLAdapter Migration::CommandRecorder SchemaDumper ].each do |extension| next unless Object.const_defined?("ActiveRecord::#{extension}") require File.join('enum_kit', 'active_record_extensions', EnumKit.underscore(extension)) target_constant = Object.const_get("ActiveRecord::#{extension}") extension_constant = Object.const_get("EnumKit::ActiveRecordExtensions::#{extension}") target_constant.prepend(extension_constant) end end |
.load_patches! ⇒ Object
Load the ‘ActiveRecord` monkey patches.
25 26 27 28 29 30 31 32 |
# File 'lib/enum_kit.rb', line 25 def self.load_patches! require 'enum_kit/active_record_patches/connection_adapters/postgresql/column_methods' require 'enum_kit/active_record_patches/connection_adapters/postgresql/oid/enum' require 'enum_kit/active_record_patches/connection_adapters/postgresql/oid/type_map_initializer' require 'enum_kit/active_record_patches/enum' require 'enum_kit/active_record_patches/enum/enum_type' require 'enum_kit/active_record_patches/validations/pg_enum_validator' end |
.register_database_type! ⇒ Object
Register ‘:enum` as a native database type.
57 58 59 |
# File 'lib/enum_kit.rb', line 57 def self.register_database_type! ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::NATIVE_DATABASE_TYPES[:enum] = { name: 'enum' } end |
.sanitize_name!(name) ⇒ String
Sanitize the name of the enum.
47 48 49 50 51 52 53 54 55 |
# File 'lib/enum_kit/helpers.rb', line 47 def self.sanitize_name!(name) raise ArgumentError, 'Enum names must be a String or a Symbol.' unless name.is_a?(String) || name.is_a?(Symbol) name = name.to_s return name if name =~ /^[a-z0-9_]+$/ raise ArgumentError, 'Enum names may contain only lowercase letters, numbers and underscores.' end |
.sanitize_value!(value) ⇒ Array
Sanitize a single value of an enum.
62 63 64 65 66 67 68 69 70 |
# File 'lib/enum_kit/helpers.rb', line 62 def self.sanitize_value!(value) raise ArgumentError, 'Enum values must be a String or a Symbol.' unless value.is_a?(String) || value.is_a?(Symbol) value = value.to_s return value if value =~ /^[a-z0-9_ ]+$/ raise ArgumentError, 'Enum values may contain only lowercase letters, numbers, underscores and spaces.' end |
.sanitize_values!(values) ⇒ Array
Sanitize the values of an enum.
77 78 79 80 81 82 83 |
# File 'lib/enum_kit/helpers.rb', line 77 def self.sanitize_values!(values) return nil if values.nil? raise ArgumentError, 'Enum values must be an Array of String and/or Symbol objects.' unless values.is_a?(Array) values.map { |value| sanitize_value!(value) } end |
.sqlize(value) ⇒ String
Convert a value into a String that can be used in SQL.
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/enum_kit/helpers.rb', line 29 def self.sqlize(value) case value when Array '(' + value.map { |v| sqlize(v) }.join(', ') + ')' when String ActiveRecord::Base.connection.quote(value) when Symbol sqlize(value.to_s) else raise ArgumentError, "Unable to convert value of type #{value.class} into SQL format." end end |
.underscore(value) ⇒ String
Makes an underscored, lowercase form from the expression in the string.
Changes ‘::’ to ‘/’ to convert namespaces to paths. This method is based on the ‘ActiveSupport::Inflector.underscore` method.
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/enum_kit/helpers.rb', line 12 def self.underscore(value) return value unless /[A-Z-]|::/.match?(value) value = value.to_s.gsub('::', '/') value.gsub!('PostgreSQL', 'Postgresql') value.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2') value.gsub!(/([a-z\d])([A-Z])/, '\1_\2') value.tr!('-', '_') value.downcase! value end |