Class: Ditty::User

Inherits:
Sequel::Model
  • Object
show all
Includes:
Base
Defined in:
lib/ditty/models/user.rb

Instance Method Summary collapse

Methods included from Base

#for_json

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_sym, *arguments, &block) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/ditty/models/user.rb', line 24

def method_missing(method_sym, *arguments, &block)
  if respond_to_missing?(method_sym)
    role?(method_sym[0..-2])
  else
    super
  end
end

Instance Method Details

#after_createObject

Add the basic roles and identity



49
50
51
# File 'lib/ditty/models/user.rb', line 49

def after_create
  check_roles
end

#check_rolesObject



53
54
55
56
57
# File 'lib/ditty/models/user.rb', line 53

def check_roles
  return if roles_dataset.first(name: 'anonymous')
  return if roles_dataset.first(name: 'user')
  add_role Role.find_or_create(name: 'user')
end

#gravatarObject



36
37
38
39
# File 'lib/ditty/models/user.rb', line 36

def gravatar
  hash = Digest::MD5.hexdigest(email.downcase)
  "https://www.gravatar.com/avatar/#{hash}"
end

#index_prefixObject



59
60
61
# File 'lib/ditty/models/user.rb', line 59

def index_prefix
  email
end

#respond_to_missing?(name, _include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/ditty/models/user.rb', line 32

def respond_to_missing?(name, _include_private = false)
  name[-1] == '?'
end

#role?(check) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
21
22
# File 'lib/ditty/models/user.rb', line 17

def role?(check)
  @roles ||= Hash.new do |h,k|
    h[k] = !roles_dataset.first(name: k).nil?
  end
  @roles[check]
end

#usernameObject



63
64
65
# File 'lib/ditty/models/user.rb', line 63

def username
  identity_dataset.first.username
end

#validateObject



41
42
43
44
45
46
# File 'lib/ditty/models/user.rb', line 41

def validate
  validates_presence :email
  return if email.blank?
  validates_unique :email
  validates_format(/\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :email)
end