Module: Attributor

Included in:
DSLCompiler
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/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, Numeric, Temporal, Type Classes: Attribute, AttributorException, BigDecimal, Boolean, CSV, Class, CoercionError, Collection, DSLCompiler, Date, DateTime, DeserializationError, DumpError, FakeParent, FieldSelector, FileUpload, Float, Hash, HashDSLCompiler, Ids, IncompatibleTypeError, Integer, InvalidDefinition, LoadError, Model, Object, Polymorphic, Regexp, SmartAttributeSelector, String, Struct, Symbol, Tempfile, Time, URI, UnfeasibleRequirementsError

Constant Summary collapse

SEPARATOR =

hierarchical separator string for composing human readable attributes

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.errorize_value(value) ⇒ Object



65
66
67
68
69
# 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) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# 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) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# 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) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
# 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) ⇒ Object

Parameters:

  • type (Class)

    The class of the type to resolve

Raises:



26
27
28
29
30
31
32
33
# 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) ⇒ Object



47
48
49
50
51
# 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

Instance Method Details

#_get_attr(k) ⇒ Object

Override the generic way to get a value from an instance (models need to call the method)



191
192
193
# File 'lib/attributor/types/model.rb', line 191

def _get_attr(k)
  __send__(k)
end