Module: Attributor

Defined in:
lib/attributor/attribute.rb,
lib/attributor.rb,
lib/attributor/type.rb,
lib/attributor/version.rb,
lib/attributor/dumpable.rb,
lib/attributor/types/csv.rb,
lib/attributor/types/ids.rb,
lib/attributor/types/uri.rb,
lib/attributor/exceptions.rb,
lib/attributor/types/date.rb,
lib/attributor/types/hash.rb,
lib/attributor/types/time.rb,
lib/attributor/types/class.rb,
lib/attributor/types/float.rb,
lib/attributor/types/model.rb,
lib/attributor/dsl_compiler.rb,
lib/attributor/types/object.rb,
lib/attributor/types/regexp.rb,
lib/attributor/types/string.rb,
lib/attributor/types/struct.rb,
lib/attributor/types/symbol.rb,
lib/attributor/example_mixin.rb,
lib/attributor/types/boolean.rb,
lib/attributor/types/integer.rb,
lib/attributor/types/tempfile.rb,
lib/attributor/types/container.rb,
lib/attributor/types/date_time.rb,
lib/attributor/families/numeric.rb,
lib/attributor/types/bigdecimal.rb,
lib/attributor/types/collection.rb,
lib/attributor/families/temporal.rb,
lib/attributor/hash_dsl_compiler.rb,
lib/attributor/types/file_upload.rb,
lib/attributor/types/polymorphic.rb,
lib/attributor/attribute_resolver.rb,
lib/attributor/extras/field_selector.rb,
lib/attributor/smart_attribute_selector.rb,
lib/attributor/extras/field_selector/parser.rb,
lib/attributor/extras/field_selector/transformer.rb

Overview

Abstract type for the ‘temporal’ family

Defined Under Namespace

Modules: Container, Dumpable, ExampleMixin, Type Classes: Attribute, AttributeResolver, AttributorException, BigDecimal, Boolean, CSV, Class, CoercionError, Collection, DSLCompiler, Date, DateTime, DeserializationError, DumpError, FakeParent, FieldSelector, FileUpload, Float, Hash, HashDSLCompiler, Ids, IncompatibleTypeError, Integer, InvalidDefinition, LoadError, Model, Numeric, Object, Polymorphic, Regexp, SmartAttributeSelector, String, Struct, Symbol, Tempfile, Temporal, Time, URI, UnfeasibleRequirementsError

Constant Summary collapse

SEPARATOR =

hierarchical separator string for composing human readable attributes

'.'.freeze
DEFAULT_ROOT_CONTEXT =
['$'].freeze
MODULE_PREFIX =
'Attributor::'.freeze
MODULE_PREFIX_REGEX =
::Regexp.new(MODULE_PREFIX)
VERSION =
'5.2.1'.freeze

Class Method Summary collapse

Class Method Details

.errorize_value(value) ⇒ Object



70
71
72
73
74
# File 'lib/attributor.rb', line 70

def self.errorize_value(value)
  inspection = value.inspect
  inspection = inspection[0..500] + '...[truncated]' if inspection.size > 500
  inspection
end

.find_type(attr_type) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/attributor.rb', line 38

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) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/attributor.rb', line 54

def self.humanize_context(context)
  return '' unless context

  context = Array(context) if context.is_a? ::String

  unless context.is_a? Enumerable
    raise "INVALID CONTEXT!!! (got: #{context.inspect})"
  end

  begin
    return context.join('.')
  rescue e
    raise "Error creating context string: #{e.message}"
  end
end

.resolve_type(attr_type, options = {}, constructor_block = nil) ⇒ Object

Parameters:

  • type (Class)

    The class of the type to resolve

Raises:



29
30
31
32
33
34
35
36
# File 'lib/attributor.rb', line 29

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) ⇒ Object



48
49
50
51
52
# File 'lib/attributor.rb', line 48

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