Module: Linguistics

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

Overview

A language-independent framework for adding linguistics functions to Ruby classes.

Defined Under Namespace

Modules: EN Classes: LanguageProxyClass

Constant Summary collapse

SVNRev =

Subversion revision

%q$Rev: 95 $
SVNid =

Subversion ID

%q$Id: linguistics.rb 95 2007-06-13 05:25:38Z deveiant $
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.



328
329
330
# File 'lib/linguistics.rb', line 328

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)


334
335
336
# File 'lib/linguistics.rb', line 334

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

.const_missing(sym) ⇒ Object

Handle auto-magic usage



245
246
247
# File 'lib/linguistics.rb', line 245

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.



118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/linguistics.rb', line 118

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.



134
135
136
137
# File 'lib/linguistics.rb', line 134

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)


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
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/linguistics.rb', line 196

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



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
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/linguistics.rb', line 151

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.



142
143
144
145
146
147
# File 'lib/linguistics.rb', line 142

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.



320
321
322
# File 'lib/linguistics.rb', line 320

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.



313
314
315
# File 'lib/linguistics.rb', line 313

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.



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/linguistics.rb', line 273

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