Class Character
In: app/models/character.rb
Parent: ActiveRecord::Base

Methods

Included Modules

KotobaModel

Constants

MAX_NUMBER_OF_ATTEMPTS = 10
CHARACTER_BASE_URL = 'http://kotoba.wardosworld.com/character_of_moment/'

Public Class methods

Select a character that has a JLPT attribute.

If none is specified we default to 1.

[Source]

    # File app/models/character.rb, line 65
65:   def self.get_character_of_jlpt(jlpt_level = 1)
66:     language_character_jlpt = LanguageCharacterAttribute.find_by_language_id_and_attribute(
67:       Language.get_japanese.id, 
68:       LanguageCharacterAttribute::ATTRIBUTE_JLPT)
69:     characters_with_frequency    = CharacterAttribute.find(
70:       :all, 
71:       :conditions => ['language_character_attribute_id = ? and value = ?', language_character_jlpt.id, jlpt_level], 
72:       :order => 'id desc')
73: 
74:     # make we continue to tweet for each level JLPT without repeating ourselves
75:     tweets_per_day = 2
76:     number_of_characters = characters_with_frequency.size / tweets_per_day
77:     tweets_newer_than = number_of_characters.days.ago
78:     return self.randomly_select_character(characters_with_frequency, :jlpt, tweets_newer_than)
79:   end

[Source]

     # File app/models/character.rb, line 94
 94:   def self.get_character_of_jlpt_message(jlpt=1)
 95:     begin
 96:         random_character = get_character_of_jlpt(jlpt)
 97:         character_of_day = random_character.character
 98:         message     = "文字 JLPT #{jlpt}「#{character_of_day}」:#{CHARACTER_BASE_URL}#{random_character.id}"
 99:         return [random_character, message]
100:     rescue Exception => exception
101:       logger.error(exception.backtrace.join("  \n"))
102:       raise("Unable to create a character of JLPT message due to: #{exception.message}")
103:     end
104:   end

Select a character that has a frequency attribute.

[Source]

    # File app/models/character.rb, line 82
82:   def self.get_character_of_moment    
83:     language_character_frequency = LanguageCharacterAttribute.find_by_language_id_and_attribute(
84:       Language.get_japanese.id, 
85:       LanguageCharacterAttribute::ATTRIBUTE_FREQUENCY)
86:     characters_with_frequency    = CharacterAttribute.find(
87:       :all, 
88:       :conditions => ['language_character_attribute_id = ?', language_character_frequency.id], 
89:       :order => 'id desc')
90:     tweets_newer_than = 10.weeks.ago
91:     return self.randomly_select_character(characters_with_frequency, :moment_of, tweets_newer_than)
92:   end

[Source]

     # File app/models/character.rb, line 106
106:   def self.get_character_of_moment_message
107:     begin
108:         random_character = get_character_of_moment
109:         character_of_day = random_character.character
110:         message     = "文字「#{character_of_day}」:#{CHARACTER_BASE_URL}#{random_character.id}"
111:         return [random_character, message]
112:     rescue Exception => exception
113:       logger.error(exception.backtrace.join("  \n"))
114:       raise("Unable to create a character of moment message due to: #{exception.message}")
115:     end
116:   end

[Source]

   # File app/models/character.rb, line 5
5:   def self.overview
6:     KotobaModel.overview(self)
7:   end

Public Instance methods

[Source]

    # File app/models/character.rb, line 58
58:   def radical_characters
59:     CharacterRadical.find(:all, :conditions => {:radical_id => id})
60:   end

[Source]

    # File app/models/character.rb, line 53
53:   def sentences
54:     return [] if character.empty?
55:     Sentence.find(:all, :conditions => ['sentence like ? and language_id = ?',  "%#{character}%", language_id])
56:   end

[Source]

    # File app/models/character.rb, line 44
44:   def stroke_count
45:     get_attribute('stroke_count')
46:   end

[Source]

    # File app/models/character.rb, line 40
40:   def to_label
41:     character
42:   end

[Source]

    # File app/models/character.rb, line 48
48:   def words
49:     return [] if character.empty?
50:     Word.find(:all, :conditions => ['spelling like ? and language_id = ?', "#{character}%", language_id])
51:   end

Protected Instance methods

[Source]

     # File app/models/character.rb, line 120
120:   def authorized_for_create?
121:     return Authorize.is_role_allowed_for_user?(current_user, UserRole::USER_ROLE_ADMIN)
122:   end

[Source]

     # File app/models/character.rb, line 128
128:   def authorized_for_destroy?
129:     return Authorize.is_role_allowed_for_user?(current_user, UserRole::USER_ROLE_ADMIN)
130:   end

[Source]

     # File app/models/character.rb, line 132
132:   def authorized_for_read?
133:     return Authorize.is_role_allowed_for_user?(current_user, UserRole::USER_ROLE_USER)
134:   end

[Source]

     # File app/models/character.rb, line 124
124:   def authorized_for_update?
125:     return Authorize.is_role_allowed_for_user?(current_user, UserRole::USER_ROLE_USER)
126:   end

[Validate]