Module: EnumKit
- Defined in:
- lib/enum_kit/active_record_extensions/schema_dumper.rb,
lib/enum_kit/helpers.rb,
lib/enum_kit/railtie.rb,
lib/enum_kit/constants.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 Classes: Railtie
Constant Summary collapse
- VERSION =
Returns The gem’s semantic version number.
'0.3.1'
Class Method Summary collapse
-
.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
.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 |