Class: Twimock::User

Inherits:
Database::Table show all
Defined in:
lib/twimock/user.rb

Overview

TODO: 要改善 AccessTokenをUserから分離

Constant Summary collapse

TABLE_NAME =
:users
COLUMN_NAMES =
[:id, :name, :twitter_id, :email, :password, :created_at]
CHILDREN =
[ Twimock::AccessToken, Twimock::RequestToken ]
INFO_KEYS =
[:id, :name, :created_at]

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Database::Table

all, children, #column_names, column_names, column_type, create!, #destroy, #fetch, first, last, #method_missing, method_missing, #persisted?, #save!, table_info, #table_name, table_name, #update_attributes!, where

Constructor Details

#initialize(options = {}) ⇒ User

Returns a new instance of User.



14
15
16
17
18
19
20
21
22
23
# File 'lib/twimock/user.rb', line 14

def initialize(options={})
  opts = Hashie::Mash.new(options)
  id = opts.id || opts.identifier
  @id                  = (id.to_i > 0) ? id.to_i : (Faker::Number.number(10)).to_i
  @name                = opts.name                || create_user_name
  @twitter_id          = opts.twitter_id          || @name.downcase.gsub(" ", "_")
  @email               = opts.email               || Faker::Internet.email
  @password            = opts.password            || Faker::Internet.password
  @created_at     = opts.created_at
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Twimock::Database::Table

Class Method Details

.find_by_tiwtter_id_or_email(value) ⇒ Object



46
47
48
49
# File 'lib/twimock/user.rb', line 46

def self.find_by_tiwtter_id_or_email(value)
  user   = Twimock::User.find_by_twitter_id(value)
  user ||= Twimock::User.find_by_email(value)
end

Instance Method Details

#generate_access_token(application_id = nil) ⇒ Object



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

def generate_access_token(application_id=nil)
  if application_id
    application = Twimock::Application.find_by_id(application_id)
    raise Twimock::Errors::ApplicationNotFound unless application
  end

  access_token = Twimock::AccessToken.new({ application_id: application_id })
  if self.persisted?
    access_token.user_id = self.id
    access_token.save!
  end
  access_token
end

#infoObject



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

def info
  info_hash = Hashie::Mash.new({})
  INFO_KEYS.each { |key| info_hash[key] = self.instance_variable_get("@#{key}") }
  info_hash.id_str = info_hash.id.to_s
  info_hash
end