module Attributor
TODO: profile keys for attributes, test as frozen strings
Container
of options and structure definition
poorly-optimized, but handy, mixin for Hash
and Model
examples. primarily enables support for lazy values.
Abstract type for the ‘numeric’ family
Abstract type for the ‘temporal’ family
Represents an unordered collection of attributes
Float
objects represent inexact real numbers using the native architecture’s double-precision floating point representation. See: ruby-doc.org/core-2.1.0/Float.html
Constants
- DEFAULT_ROOT_CONTEXT
- MODULE_PREFIX
- MODULE_PREFIX_REGEX
- ROOT_PREFIX
- SEPARATOR
hierarchical separator string for composing human readable attributes
- VERSION
Public Class Methods
errorize_value(value)
click to toggle source
# File lib/attributor.rb, line 65 def self.errorize_value(value) inspection = value.inspect inspection = inspection[0..500] + '...[truncated]' if inspection.size > 500 inspection end
find_type(attr_type)
click to toggle source
# File lib/attributor.rb, line 35 def self.find_type(attr_type) return attr_type if attr_type < Attributor::Type name = attr_type.name.split('::').last # TOO EXPENSIVE? klass = const_get(name) if const_defined?(name) raise AttributorException, "Could not find class with name #{name}" unless klass raise AttributorException, "Could not find attribute type for: #{name} [klass: #{klass.name}]" unless klass < Attributor::Type klass end
humanize_context(context)
click to toggle source
# File lib/attributor.rb, line 53 def self.humanize_context(context) return '' unless context context = Array(context) if context.is_a? ::String begin context.join('.') rescue e raise "Error creating context string: #{e.message}" end end
recursive_to_h(val)
click to toggle source
# File lib/attributor.rb, line 71 def self.recursive_to_h(val) if val.is_a? Array val.map { |v| recursive_to_h(v) } elsif val.nil? nil elsif val.respond_to?(:to_h) val.to_h.each_with_object({}) do |(name, inner_val), hash| hash[name] = recursive_to_h(inner_val) end else val end end
resolve_type(attr_type, options = {}, constructor_block = nil)
click to toggle source
@param type [Class] The class of the type to resolve
# File lib/attributor.rb, line 26 def self.resolve_type(attr_type, options = {}, constructor_block = nil) klass = self.find_type(attr_type) return klass.construct(constructor_block, **options) if klass.constructable? raise AttributorException, "Type: #{attr_type} does not support anonymous generation" if constructor_block klass end
type_name(type)
click to toggle source
# File lib/attributor.rb, line 47 def self.type_name(type) return type_name(type.class) unless type.is_a?(::Class) type.ancestors.find { |k| k.name && !k.name.empty? }.name end
Public Instance Methods
_get_attr(k)
click to toggle source
Override the generic way to get a value from an instance (models need to call the method)
# File lib/attributor/types/model.rb, line 191 def _get_attr(k) __send__(k) end