Module: ActivityStreams::Validator

Included in:
Base
Defined in:
lib/activitystreams/validator.rb

Instance Method Summary collapse

Instance Method Details

#to_float(attribute, options = {}) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/activitystreams/validator.rb', line 54

def to_float(attribute, options = {})
  _value_ = self.send attribute
  return unless _value_
  _value_ = BigDecimal.new(_value_.to_s).round(options[:precision]) if options[:precision]
  if options[:range] && !options[:range].include?(_value_)
    raise InvalidAttribute.new("#{attribute} should be within #{options[:range]}")
  end
  self.send :"#{attribute}=", _value_.to_f
end

#to_integer(attribute) ⇒ Object



49
50
51
52
# File 'lib/activitystreams/validator.rb', line 49

def to_integer(attribute)
  _value_ = self.send attribute
  self.send :"#{attribute}=", _value_.try(:to_i)
end

#to_iri(attribute, arrayed = false) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/activitystreams/validator.rb', line 17

def to_iri(attribute, arrayed = false)
  _value_ = self.send attribute
  _value_ = if arrayed
    Array(_value_).collect do |_v_|
      Addressable::URI.parse(_v_)
    end
  else
    Addressable::URI.parse(_value_)
  end
  self.send :"#{attribute}=", _value_
rescue Addressable::URI::InvalidURIError => e
  message = if arrayed
    "#{attribute} should be an array of valid IRI"
  else
    "#{attribute} should be a valid IRI"
  end
  raise InvalidAttribute.new(message)
end

#to_time(attribute) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/activitystreams/validator.rb', line 36

def to_time(attribute)
  _value_ = self.send attribute
  _value_ = case _value_
  when String
    Time.parse _value_, :raise_me
  else
    _value_
  end
  self.send :"#{attribute}=", _value_
rescue ArgumentError, NoMethodError => e
  raise InvalidAttribute.new("#{attribute} should be a valid date-time")
end

#validate_attribute!(attribute, klass, arrayed = false) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/activitystreams/validator.rb', line 3

def validate_attribute!(attribute, klass, arrayed = false)
  _value_ = self.send attribute
  return if _value_.blank?
  if arrayed
    _value_.all? do |_v_|
      _v_.is_a?(klass)
    end or
    raise InvalidAttribute.new("#{attribute} should be an array of #{klass}")
  else
    _value_.is_a?(klass) or
    raise InvalidAttribute.new("#{attribute} should be a #{klass}")
  end
end