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
-
.config ⇒ Configuration
readonly
The EnumerizeSchema configuration object.
Class Method Summary collapse
-
.attr_enum(attribute_name, **enumerize_options) ⇒ void
Defines an enumerized attribute.
-
.configure {|config| ... } ⇒ Object
Configure EnumerizeSchema.
- .enumerize_schema ⇒ Object private
- .enumerize_schema_attributes_scope ⇒ Object private
-
.enumerize_schema_file ⇒ Object
The path of the schema file for the specific class extended by EnumerizeSchema.
-
.enumerize_schema_file=(value) ⇒ Pathname
Override the path to the enumerized attribute schema file for the current class.
Class Attribute Details
.config ⇒ Configuration (readonly)
Returns the EnumerizeSchema configuration object.
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
This method is marked as protected
when added to the class, so it can’t be used from outside that class.
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.
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, **) if .include?(:in) enumerize(attribute_name, ) 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, .merge(in: enum_values)) end end |
.configure {|config| ... } ⇒ Object
Configure EnumerizeSchema.
28 29 30 |
# File 'lib/enumerize_schema.rb', line 28 def self.configure yield(config) end |
.enumerize_schema ⇒ Object
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_scope ⇒ Object
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_file ⇒ Object
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.
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.
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 |