Module: Attributor

Defined in:
lib/attributor/attribute.rb,
lib/attributor.rb,
lib/attributor/type.rb,
lib/attributor/version.rb,
lib/attributor/types/uri.rb,
lib/attributor/types/csv.rb,
lib/attributor/types/ids.rb,
lib/attributor/types/date.rb,
lib/attributor/exceptions.rb,
lib/attributor/types/hash.rb,
lib/attributor/types/time.rb,
lib/attributor/types/model.rb,
lib/attributor/types/float.rb,
lib/attributor/types/class.rb,
lib/attributor/types/struct.rb,
lib/attributor/dsl_compiler.rb,
lib/attributor/types/object.rb,
lib/attributor/types/string.rb,
lib/attributor/types/regexp.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/types/collection.rb,
lib/attributor/families/numeric.rb,
lib/attributor/types/bigdecimal.rb,
lib/attributor/types/file_upload.rb,
lib/attributor/families/temporal.rb,
lib/attributor/attribute_resolver.rb,
lib/attributor/extras/field_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, ExampleMixin, Type Classes: Attribute, AttributeResolver, AttributorException, BigDecimal, Boolean, CSV, Class, CoercionError, Collection, DSLCompiler, Date, DateTime, DeserializationError, DumpError, FakeParent, FieldSelector, FileUpload, Float, Hash, Ids, IncompatibleTypeError, Integer, InvalidDefinition, LoadError, Model, Numeric, Object, Regexp, String, Struct, Symbol, Tempfile, Temporal, Time, URI

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 =
"4.1.0"

Class Method Summary collapse

Class Method Details

.errorize_value(value) ⇒ Object


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

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

.humanize_context(context) ⇒ Object


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

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

  if context.kind_of? ::String
    context = Array(context)
  end

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

  begin
    return context.join('.')
  rescue Exception => 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:


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/attributor.rb', line 27

def self.resolve_type(attr_type, options={}, constructor_block=nil)
  if attr_type < Attributor::Type
    klass = attr_type
  else
    name = attr_type.name.split("::").last # TOO EXPENSIVE?

    klass = const_get(name) if const_defined?(name)
    raise AttributorException.new("Could not find class with name #{name}") unless klass
    raise AttributorException.new("Could not find attribute type for: #{name} [klass: #{klass.name}]")  unless  klass < Attributor::Type
  end

  if klass.constructable?
    return klass.construct(constructor_block, options)
  end

  raise AttributorException.new("Type: #{attr_type} does not support anonymous generation") if constructor_block

  klass
end

.type_name(type) ⇒ Object


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

def self.type_name(type)
  return self.type_name(type.class) unless type.kind_of?(::Class)

  type.ancestors.find { |k| k.name && !k.name.empty? }.name
end