Module: Attributor::Type::ClassMethods

Defined in:
lib/attributor/type.rb

Instance Method Summary collapse

Instance Method Details

#anonymous?Boolean

Returns:



23
24
25
26
27
28
29
# File 'lib/attributor/type.rb', line 23

def anonymous?
  if @_anonymous == nil
    self.name == nil # if nothing is set, consider it anonymous if the class does not have a name
  else
    @_anonymous
  end
end

#anonymous_type(val = true) ⇒ Object

Allow a type to be marked as if it was anonymous (i.e. not referenceable by name)



19
20
21
# File 'lib/attributor/type.rb', line 19

def anonymous_type(val=true)
  @_anonymous = val
end

#check_option!(name, definition) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/attributor/type.rb', line 80

def check_option!(name, definition)
  case name
  when :min
    raise AttributorException.new("Value for option :min does not implement '<='. Got: (#{definition.inspect})") unless definition.respond_to?(:<=)
  when :max
    raise AttributorException.new("Value for option :max does not implement '>='. Got(#{definition.inspect})") unless definition.respond_to?(:>=)
  when :regexp
    # could go for a respoind_to? :=~ here, but that seems overly... cute... and not useful.
    raise AttributorException.new("Value for option :regexp is not a Regexp object. Got (#{definition.inspect})") unless definition.is_a? ::Regexp
  else
    return :unknown
  end

  return :ok
end

#compile_dsl(options, block) ⇒ Object

By default, non complex types will not have a DSL subdefinition this handles such case



106
107
108
109
110
111
# File 'lib/attributor/type.rb', line 106

def compile_dsl( options, block )
  raise AttributorException.new("Basic structures cannot take extra block definitions") if block
  # Simply create a DSL compiler to store the options, and not to parse any DSL
  sub_definition=dsl_compiler.new( options )
  return sub_definition
end

#constructable?Boolean

Does this type support the generation of subtypes?

Returns:



14
15
16
# File 'lib/attributor/type.rb', line 14

def constructable?
  false
end

#describe(root = false, example: nil) ⇒ Object

Default describe for simple types…only their name (stripping the base attributor module)



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/attributor/type.rb', line 114

def describe(root=false, example: nil)
  type_name = Attributor.type_name(self)
  hash = {
    name: type_name.gsub(Attributor::MODULE_PREFIX_REGEX, ''),
    family: self.family,
    id: self.id
  }
  hash[:anonymous] = @_anonymous unless @_anonymous.nil?
  hash[:example] = example if example
  hash
end

#dsl_compilerObject



101
102
103
# File 'lib/attributor/type.rb', line 101

def dsl_compiler
  DSLCompiler
end

#dump(value, **opts) ⇒ Object

Generic encoding of the attribute



43
44
45
# File 'lib/attributor/type.rb', line 43

def dump(value,**opts)
  value
end

#example(context = nil, options: {}) ⇒ Object

Default, overridable example function



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

def example(context=nil, options:{})
  raise AttributorException.new("#{self} must implement #example")
end

#familyObject



131
132
133
# File 'lib/attributor/type.rb', line 131

def family
  'any'
end

#generate_subcontext(context, subname) ⇒ Object



97
98
99
# File 'lib/attributor/type.rb', line 97

def generate_subcontext(context, subname)
  context + [subname]
end

#idObject



126
127
128
129
# File 'lib/attributor/type.rb', line 126

def id
  return nil if self.name.nil?
  self.name.gsub('::'.freeze,'-'.freeze)
end

#load(value, context = Attributor::DEFAULT_ROOT_CONTEXT, **options) ⇒ Object

Generic decoding and coercion of the attribute.



33
34
35
36
37
38
39
40
# File 'lib/attributor/type.rb', line 33

def load(value,context=Attributor::DEFAULT_ROOT_CONTEXT, **options)
  return nil if value.nil?
  unless value.is_a?(self.native_type)
    raise Attributor::IncompatibleTypeError, context: context, value_type: value.class, type: self
  end

  value
end

#valid_type?(value) ⇒ Boolean

Default, overridable valid_type? function

Returns:

Raises:



65
66
67
68
69
# File 'lib/attributor/type.rb', line 65

def valid_type?(value)
  return value.is_a?(native_type) if respond_to?(:native_type)

  raise AttributorException.new("#{self} must implement #valid_type? or #native_type")
end

#validate(value, context = Attributor::DEFAULT_ROOT_CONTEXT, attribute) ⇒ Object

TODO: refactor this to take just the options instead of the full attribute? TODO: delegate to subclass



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/attributor/type.rb', line 49

def validate(value,context=Attributor::DEFAULT_ROOT_CONTEXT,attribute)
  errors = []
  attribute.options.each do |option, opt_definition|
    case option
    when :max
      errors << "#{Attributor.humanize_context(context)} value (#{value}) is larger than the allowed max (#{opt_definition.inspect})" unless value <= opt_definition
    when :min
      errors << "#{Attributor.humanize_context(context)} value (#{value}) is smaller than the allowed min (#{opt_definition.inspect})" unless value >= opt_definition
    when :regexp
      errors << "#{Attributor.humanize_context(context)} value (#{value}) does not match regexp (#{opt_definition.inspect})"  unless value =~ opt_definition
    end
  end
  errors
end