Module: Attributor::Type::ClassMethods

Defined in:
lib/attributor/type.rb

Instance Method Summary collapse

Instance Method Details

#check_option!(name, definition) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/attributor/type.rb', line 69

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



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

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

#describe(root = false) ⇒ Object

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



103
104
105
106
# File 'lib/attributor/type.rb', line 103

def describe(root=false)
  type_name = self.ancestors.find { |k| k.name && !k.name.empty? }.name
  { :name => type_name.gsub( Attributor::MODULE_PREFIX_REGEX, '' ) }        
end

#dsl_compilerObject



90
91
92
# File 'lib/attributor/type.rb', line 90

def dsl_compiler
  DSLCompiler
end

#dump(value, **opts) ⇒ Object

Generic encoding of the attribute



25
26
27
# File 'lib/attributor/type.rb', line 25

def dump(value,**opts)
  value
end

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

Default, overridable example function



54
55
56
57
58
59
60
61
62
63
# File 'lib/attributor/type.rb', line 54

def example(context=nil, options:{})
  raise AttributorException.new("#{self} must implement #example")
  # return options[:example] if options.has_key? :example
  # return options[:default] if options.has_key? :default
  # if options.has_key? :values
  #   vals = options[:values]
  #   return vals[rand(vals.size)]
  # end
  # return  nil
end

#generate_subcontext(context, subname) ⇒ Object



86
87
88
# File 'lib/attributor/type.rb', line 86

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

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

Generic decoding and coercion of the attribute.



15
16
17
18
19
20
21
22
# File 'lib/attributor/type.rb', line 15

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:



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

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



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

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