Class: CiteProc::Variable Abstract

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Comparable
Defined in:
lib/citeproc/variable.rb

Overview

This class is abstract.

A CiteProc Variable represents the content of a text, numeric, date, or name variable. In its basic form it is thin abstraction that behaves almost like a regular Ruby string; more complex Variables are handled by dedicated sub-classes that make the variable’s type more explicit.

Direct Known Subclasses

Date, Names, Number, Text

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = nil) ⇒ Variable

Creates new Variable for the passed-in value



142
143
144
# File 'lib/citeproc/variable.rb', line 142

def initialize(value = nil)
  replace(value)
end

Class Attribute Details

.factories{Symbol => Class} (readonly)

Returns mapping of field names to their respective Variable classes.

Returns:

  • ({Symbol => Class})

    mapping of field names to their respective Variable classes



74
75
76
# File 'lib/citeproc/variable.rb', line 74

def factories
  @factories
end

.fields{Symbol => Array<Symbol>} (readonly)

Returns mapping of variable types to their respective field names.

Returns:

  • ({Symbol => Array<Symbol>})

    mapping of variable types to their respective field names



65
66
67
# File 'lib/citeproc/variable.rb', line 65

def fields
  @fields
end

.markupRegexp

Returns pattern used to strip markup off values.

Returns:

  • (Regexp)

    pattern used to strip markup off values



78
79
80
# File 'lib/citeproc/variable.rb', line 78

def markup
  @markup
end

.types{Symbol => Symbol} (readonly)

Returns mapping of field names to variable types.

Returns:

  • ({Symbol => Symbol})

    mapping of field names to variable types



69
70
71
# File 'lib/citeproc/variable.rb', line 69

def types
  @types
end

Instance Attribute Details

#valueObject

Returns the value wrapped by this variable.

Returns:

  • (Object)

    the value wrapped by this variable



131
132
133
# File 'lib/citeproc/variable.rb', line 131

def value
  @value
end

Class Method Details

.create(value, field = nil) ⇒ Variable

Creates a new CiteProc::Variable instance using the passed-in field name to distinguish which CiteProc::Variable class to use as factory. This method returns nil if the creation fails.

Examples:

Variable.create('foo')
#-> #<CiteProc::Variable "foo">

Variable.create('foo', :title)
#-> #<CiteProc::Text "foo">

Variable.create(['Matz', 'Flanagan'], :author)
#-> #<CiteProc::Names "Matz & Flanagan">

Variable.create(2009, :issued)
#-> #<CiteProc::Date "2009-01-01">

Parameters:

  • value (Object)

    the variable’s value

  • field (Symbol) (defaults to: nil)

    the value’s field name

Returns:

See Also:



103
104
105
106
107
# File 'lib/citeproc/variable.rb', line 103

def create(value, field = nil)
  create!(value, field)
rescue
  nil
end

.create!(value, field = nil) ⇒ Variable

Creates a new CiteProc::Variable instance using the passed-in field name to distinguish which CiteProc::Variable class to use as factory.

Parameters:

  • value (Object)

    the variable’s value

  • field (Symbol) (defaults to: nil)

    the variable’s field name

Returns:

Raises:

  • (TypeError)

    if no variable can be created for the given value and type

See Also:



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

def create!(value, field = nil)
  factory = factories[field]
  value.is_a?(factory) ? value : factory.new(value)
end

Instance Method Details

#<=>(other) ⇒ Fixnum?

Compares the variable with the passed-in value. If other responds to #strip_markup the stripped strings will be compared; otherwise both objects will be converted to and compared as strings.

Parameters:

  • other (Object)

    the object used for comparison

Returns:

  • (Fixnum, nil)

    -1, 0, or 1 depending on the result of the comparison; or nil if the two objects cannot be ignored.



265
266
267
268
269
270
271
272
273
274
# File 'lib/citeproc/variable.rb', line 265

def <=>(other)
  case
  when other.respond_to?(:strip_markup)
    strip_markup <=> other.strip_markup
  when other && other.respond_to?(:to_s)
    to_s <=> other.to_s
  else
    nil
  end
end

#date?Boolean

Returns:

  • (Boolean)


231
232
233
# File 'lib/citeproc/variable.rb', line 231

def date?
  false
end

#initialize_copy(other) ⇒ Object



146
147
148
# File 'lib/citeproc/variable.rb', line 146

def initialize_copy(other)
  @value = other.value.dup
end

#inspectString

Returns a human-readable representation of the variable.

Returns:

  • (String)

    a human-readable representation of the variable



286
287
288
# File 'lib/citeproc/variable.rb', line 286

def inspect
  "#<#{self.class.name} #{to_s.inspect}>"
end

#numeric?Boolean

Returns whether or not the variable’s value is numeric.

Returns:

  • (Boolean)

    whether or not the variable’s value is numeric



227
228
229
# File 'lib/citeproc/variable.rb', line 227

def numeric?
  !!match(/^[\w\.:;]*\d+[\w\.:;]*(\s*[,&-]\s*[\w\.:;]*\d+[\w\.:;]*)*$/i)
end

#plural?Boolean

Returns whether or not the variable holds a plural value.

Returns:

  • (Boolean)

    whether or not the variable holds a plural value



172
173
174
175
176
177
178
# File 'lib/citeproc/variable.rb', line 172

def plural?
	if numeric?
		Number.pluralize?(to_s)
	else
		false
	end
end

#replace(value) ⇒ self

The replace method is typically called by the Variable’s constructor. It will try to set the Variable to the passed in value and should accept a wide range of argument types; subclasses (especially Date and Names) override this method.

Parameters:

  • value (Object)

    the variable’s new value

Returns:

  • (self)

Raises:

  • (TypeError)

    if the variable cannot be set to the passed-in value



160
161
162
163
164
# File 'lib/citeproc/variable.rb', line 160

def replace(value)
  raise TypeError, "failed to set value to #{value.inspect}" unless value.respond_to?(:to_s)
  @value = value.to_s
  self
end

#romanizeString?

Returns roman equivalent of the variable’s numeric value.

Returns:

  • (String, nil)

    roman equivalent of the variable’s numeric value



181
182
183
184
# File 'lib/citeproc/variable.rb', line 181

def romanize
	return unless numeric?
	Number.romanize(to_i)
end

#strip_markupString

Returns the variable’s value stripped of markup.

Returns:

  • (String)

    the variable’s value stripped of markup



248
249
250
# File 'lib/citeproc/variable.rb', line 248

def strip_markup
  gsub(Variable.markup, '')
end

#strip_markup!self

Strips markup off the variable’s value.

Returns:

  • (self)


254
255
256
# File 'lib/citeproc/variable.rb', line 254

def strip_markup!
  gsub!(Variable.markup, '')
end

#to_fFloat

Returns the first (!) numeric or floating point data contained in the variable’s value; zero if no numeric data is present.

Returns:

  • (Float)

    the first (!) numeric or floating point data contained in the variable’s value; zero if no numeric data is present



243
244
245
# File 'lib/citeproc/variable.rb', line 243

def to_f
  to_s =~ /([+-]?\d[\d,\.]*)/ && $1.tr(',','.').to_f || 0.0
end

#to_iFixnum

Returns the first (!) numeric data contained in the variable’s value; zero if no numeric data is present.

Returns:

  • (Fixnum)

    the first (!) numeric data contained in the variable’s value; zero if no numeric data is present



237
238
239
# File 'lib/citeproc/variable.rb', line 237

def to_i
 to_s =~ /([+-]?\d+)/ && $1.to_i || 0
end

#to_jsonString

Returns a JSON string representation of the variable.

Returns:

  • (String)

    a JSON string representation of the variable



281
282
283
# File 'lib/citeproc/variable.rb', line 281

def to_json
  ::JSON.dump(to_citeproc)
end

#to_sString Also known as: to_citeproc

Returns the variable’s value as a string.

Returns:

  • (String)

    the variable’s value as a string



278
# File 'lib/citeproc/variable.rb', line 278

alias to_citeproc to_s

#tokenizeArray<String> Also known as: extract_numbers

Returns tokenizes the variable’s value.

Returns:



207
208
209
210
211
212
213
214
215
216
# File 'lib/citeproc/variable.rb', line 207

def tokenize
			return [] unless numeric?
  numbers = to_s.dup

  numbers.gsub!(/\s*,\s*/, ', ')
  numbers.gsub!(/\s*-\s*/, '-')
  numbers.gsub!(/\s*&\s*/, ' & ')

  numbers.split(/(\s*[,&-]\s*)/)
end

#typeSymbol

Returns the variable’s type.

Returns:

  • (Symbol)

    the variable’s type



167
168
169
# File 'lib/citeproc/variable.rb', line 167

def type
  @type ||= self.class.name.split(/::/)[-1].downcase.to_sym
end