Class: Attributor::Class
- Inherits:
-
Object
- Object
- Attributor::Class
- Includes:
- Type
- Defined in:
- lib/attributor/types/class.rb
Class Method Summary collapse
- .example(_context = nil, options: {}) ⇒ Object
- .family ⇒ Object
- .load(value, context = Attributor::DEFAULT_ROOT_CONTEXT, **_options) ⇒ Object
- .native_type ⇒ Object
-
.of(klass) ⇒ Object
Create a Class attribute type of a specific Class.
Methods included from Type
Class Method Details
.example(_context = nil, options: {}) ⇒ Object
34 35 36 |
# File 'lib/attributor/types/class.rb', line 34 def self.example(_context = nil, options: {}) @klass.nil? ? 'MyClass' : @klass.name end |
.family ⇒ Object
52 53 54 |
# File 'lib/attributor/types/class.rb', line 52 def self.family 'string' end |
.load(value, context = Attributor::DEFAULT_ROOT_CONTEXT, **_options) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/attributor/types/class.rb', line 13 def self.load(value, context = Attributor::DEFAULT_ROOT_CONTEXT, **) return value if value.is_a?(native_type) return @klass || nil if value.nil? # Must be given a String object or nil unless value.is_a?(::String) || value.nil? raise IncompatibleTypeError, context: context, value_type: value.class, type: self end value = '::' + value if value[0..1] != '::' result = value.constantize # Class given must match class specified when type created using .of() method unless @klass.nil? || result == @klass raise LoadError, "Error loading class #{value} for attribute with " \ "defined class #{@klass} while loading #{Attributor.humanize_context(context)}." end result end |
.native_type ⇒ Object
9 10 11 |
# File 'lib/attributor/types/class.rb', line 9 def self.native_type ::Class end |
.of(klass) ⇒ Object
Create a Class attribute type of a specific Class.
46 47 48 49 50 |
# File 'lib/attributor/types/class.rb', line 46 def self.of(klass) ::Class.new(self) do @klass = klass end end |