Class: Joey::Post

Inherits:
Model
  • Object
show all
Defined in:
lib/joey/post.rb

Instance Attribute Summary

Attributes inherited from Model

#client, #errors

Instance Method Summary collapse

Methods inherited from Model

define_properties, find, get_all, has_association, hash_populating_accessor, hash_populating_association, #initialize, recognize?

Constructor Details

This class inherits a constructor from Joey::Model

Instance Method Details

#valid?Boolean

Returns:

  • (Boolean)


57
58
59
60
61
# File 'lib/joey/post.rb', line 57

def valid?
  self.validate
  puts self.errors.inspect unless self.errors.empty?
  self.errors.empty?
end

#validateObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/joey/post.rb', line 18

def validate
  created_time.to_time rescue errors << { :message => 'created_time is not compatible' }
  updated_time.to_time rescue errors << { :message => 'updated_time is not compatible' }
  errors << { :message => 'id should not be nil' } if id.nil?
  errors << { :message => "name is neither string nor nil but is #{name.inspect}" } unless name.is_a?(String) || name.nil?
  errors << { :message => "message is neither string nor nil but is #{message.inspect}" } unless message.is_a?(String) || message.nil?
  errors << { :message => "likes is neither string nor nil but is #{likes.inspect}" } unless likes.is_a?(Integer) || likes.nil? || likes.is_a?(Array)
  errors << { :message => "icon is neither string nor nil but is #{icon.inspect}" } unless icon.is_a?(String) || icon.nil?
  errors << { :message => "attribution is neither string nor nil but is #{attribution.inspect}" } unless attribution.is_a?(String) || attribution.nil?

  unless ['music', 'photo', 'video', 'status', 'link'].include?(type)
    errors << { :message => "type should be one of 'music', 'video', 'status', or 'link' but is #{type}" }
  end

  if type == 'picture' && !picture.is_a?(String)
    errors << { :message => "picture is not present for picture post and is #{picture.inspect}" }
  end

  if type == 'link' && !link.is_a?(String)
    errors << { :message => "picture is not present for picture post and is #{picture.inspect}" }
  end

  errors << { :message => "from is not a Joey::User or Joey::Page and is #{from.inspect}" } unless from.is_a?(Joey::User) || from.is_a?(Joey::Page)

  errors << { :message => "to is neither an array nor nil and is #{to.inspect}" }  unless to.is_a?(Array) || to.nil?
  if to.is_a?(Array) && !(to.collect(&:class).uniq - [Joey::User, Joey::Page]).empty?
    errors << { :message => 'to is not an array of Joey::User or Joey::Page' }
  end

  # TODO: Explore following approach.
  # errors << { :message => 'Comments is not valid' }, unless comment.nil? || (comments.collect(&:valid?).uniq - [true]).empty?

  # Sometimes comments is a single Joey::Comment object
  errors << { :message => "comments is not an array nor nil and is #{comments.inspect}" } unless comments.is_a?(Array) || comments.nil?
  if comments.is_a?(Array) && !(comments.collect(&:class).uniq - [Joey::Comment]).empty?
    errors << { :message => 'comments is not an array of Joey::Comment' }
  end
end