Module: Linguistics

Defined in:
lib/linguistics.rb,
lib/linguistics/iso639.rb

Overview

linguistics/iso639.rb - A hash of International 2- and 3-letter ISO639-1 and ISO639-2 language codes. Each entry has two keys:

:codes

All of the codes known for this language

:desc

The English-language description of the language.

:include: LICENSE

Please see the file LICENSE in the base directory for licensing details.

Defined Under Namespace

Modules: EN Classes: LanguageProxyClass

Constant Summary collapse

VERSION =

Release version

'1.0.9'
DefaultLanguages =

Language module implementors should do something like:

Linguistics::DefaultLanguages.push( :ja ) # or whatever

so that direct requiring of a language module sets the default.

[]
DefaultExtClasses =

The list of Classes to add linguistic behaviours to.

[String, Numeric, Array]
LanguageCodes =

Hash of ISO639 2- and 3-letter language codes

{}

Class Method Summary collapse

Class Method Details

.classical=(val) ⇒ Object

Set the ‘classical pluralizations’ flag to val. Setting is local to calling thread.



316
317
318
# File 'lib/linguistics.rb', line 316

def classical=( val )
	Thread.current[:classical_plurals] = val
end

.classical?Boolean

Return the value of the ‘classical pluralizations’ flag. Setting is local to calling thread.

Returns:

  • (Boolean)


322
323
324
# File 'lib/linguistics.rb', line 322

def classical?
	Thread.current[:classical_plurals] ? true : false
end

.const_missing(sym) ⇒ Object

Handle auto-magic usage



233
234
235
# File 'lib/linguistics.rb', line 233

def self::const_missing( sym )
	load_language( sym.to_s.downcase )
end

.extend_object(obj) ⇒ Object

Extend the specified target object with one or more language proxy methods, each of which provides access to one or more linguistic methods for that language.



106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/linguistics.rb', line 106

def self::extend_object( obj )
	case obj
	when Class
		# $stderr.puts "Extending %p" % obj if $DEBUG
		self::install_language_proxy( obj )
	else
		sclass = (class << obj; self; end)
		# $stderr.puts "Extending a object's metaclass: %p" % obj if $DEBUG
		self::install_language_proxy( sclass )
	end

	super
end

.included(mod) ⇒ Object

Extend the including class with linguistics proxy methods.



122
123
124
125
# File 'lib/linguistics.rb', line 122

def self::included( mod )
	# $stderr.puts "Including Linguistics in %p" % mod if $DEBUG
	mod.extend( self ) unless mod == Linguistics
end

.install_delegator_proxy(klass, langcode) ⇒ Object

Install a regular proxy method in the given klass that will delegate calls to missing method to the languageProxy for the given language.

Raises:

  • (ArgumentError)


184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/linguistics.rb', line 184

def self::install_delegator_proxy( klass, langcode )
	raise ArgumentError, "Missing langcode" if langcode.nil?

	# Alias any currently-extant
	if klass.instance_methods( false ).include?( "method_missing" )
		klass.module_eval %{
			alias_method :__orig_method_missing, :method_missing
		}
	end

	# Add the #method_missing method that auto-installs delegator methods
	# for methods supported by the linguistic proxy objects.
	klass.module_eval %{
		def method_missing( sym, *args, &block )

			# If the linguistic delegator answers the message, install a
			# delegator method and call it.
			if self.send( :#{langcode} ).respond_to?( sym )

				# $stderr.puts "Installing linguistic delegator method \#{sym} " \
				#	"for the '#{langcode}' proxy"
				self.class.module_eval %{
					def \#{sym}( *args, &block )
						self.#{langcode}.\#{sym}( *args, &block )
					end
				}
				self.method( sym ).call( *args, &block )

			# Otherwise either call the overridden proxy method if there is
			# one, or just let our parent deal with it.
			else
				if self.respond_to?( :__orig_method_missing )
					return self.__orig_method_missing( sym, *args, &block )
				else
					super( sym, *args, &block )
				end
			end
		end
	}
end

.install_language_proxy(klass, languages = DefaultLanguages) ⇒ Object

Install the language proxy



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/linguistics.rb', line 139

def self::install_language_proxy( klass, languages=DefaultLanguages )
	languages.replace( DefaultLanguages ) if languages.empty?

	# Create an languageProxy class for each language specified
	languages.each do |lang|
		# $stderr.puts "Extending the %p class with %p" %
		#	[ klass, lang ] if $DEBUG

		# Load the language module (skipping to the next if it's already
		# loaded), make a languageProxy class that delegates to it, and
		# figure out what the languageProxy method will be called.
		mod = load_language( lang.to_s.downcase )
		ifaceMeth = mod.name.downcase.sub( /.*:/, '' )
		languageProxyClass = make_language_proxy( mod )

		# Install a hash for languageProxy classes and an accessor for the
		# hash if it's not already present.
		if !klass.class_variables.include?( "@@__languageProxy_class" )
			klass.module_eval %{
				@@__languageProxy_class = {}
				def self::__languageProxy_class; @@__languageProxy_class; end
			}, __FILE__, __LINE__
		end

		# Merge the current languageProxy into the hash
		klass.__languageProxy_class.merge!( ifaceMeth => languageProxyClass )

		# Set the language-code proxy method for the class unless it has one
		# already
		unless klass.instance_methods(true).include?( ifaceMeth )
			klass.module_eval %{
				def #{ifaceMeth}
					@__#{ifaceMeth}_languageProxy ||=
						self.class.__languageProxy_class["#{ifaceMeth}"].
						new( self )
				end
			}, __FILE__, __LINE__
		end
	end
end

.make_language_proxy(mod) ⇒ Object

Make an languageProxy class that encapsulates all of the inflect operations using the given language module.



130
131
132
133
134
135
# File 'lib/linguistics.rb', line 130

def self::make_language_proxy( mod )
	# $stderr.puts "Making language proxy for mod %p" % [mod]
	Class::new( LanguageProxyClass ) {
		@langmod = mod
	}
end

.numObject Also known as: NUM

Get the default count for all unspecified plurals. Setting is local to calling thread.



308
309
310
# File 'lib/linguistics.rb', line 308

def num
	Thread.current[:persistent_count]
end

.num=(val) ⇒ Object Also known as: NUM=

Set the default count for all unspecified plurals to val. Setting is local to calling thread.



301
302
303
# File 'lib/linguistics.rb', line 301

def num=( val )
	Thread.current[:persistent_count] = val
end

.use(*languages) ⇒ Object

Add linguistics functions for the specified languages to Ruby’s core classes. The interface to all linguistic functions for a given language is through a method which is the same the language’s international 2- or 3-letter code (ISO 639). You can also specify a Hash of configuration options which control which classes are extended:

:classes

Specify the classes which are to be extended. If this is not specified, the Class objects in Linguistics::DefaultExtClasses (an Array) are extended.

:installProxy

Install a proxy method in each of the classes which are to be extended which will search for missing methods in the languageProxy for the language code specified as the value. This allows linguistics methods to be called directly on extended objects directly (e.g., 12.en.ordinal becomes 12.ordinal). Obviously, methods which would collide with the object’s builtin methods will need to be invoked through the languageProxy. Any existing proxy methods in the extended classes will be preserved.



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/linguistics.rb', line 261

def use( *languages )
	config = {}
	config = languages.pop if languages.last.is_a?( Hash )

	classes = config.key?( :classes ) ? config[:classes] : DefaultExtClasses
	classes = [ classes ] unless classes.is_a?( Array )

	# Install the languageProxy in each class.
	classes.each {|klass|

		# Create an languageProxy class for each installed language
		install_language_proxy( klass, languages )

		# Install the delegator proxy if configured
		if config[:installProxy]
			case config[:installProxy]
			when Symbol
				langcode = config[:installProxy]
			when String
				langcode = config[:installProxy].intern
			when TrueClass
				langcode = languages[0] || DefaultLanguages[0] || :en
			else
				raise ArgumentError,
					"Unexpected value %p for :installProxy" %
					config[:installProxy]
			end

			install_delegator_proxy( klass, langcode )
		end
	}
end