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



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

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



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

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



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

def fields
  @fields
end

.markupRegexp

Returns pattern used to strip markup off values.

Returns:

  • (Regexp)

    pattern used to strip markup off values



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

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



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

def types
  @types
end

Instance Attribute Details

#valueObject

Returns the value wrapped by this variable.

Returns:

  • (Object)

    the value wrapped by this variable



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

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:



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

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:



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

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.



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

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)


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

def date?
  false
end

#initialize_copy(other) ⇒ Object



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

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



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

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



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

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



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

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



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

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



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

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



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

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

#strip_markup!self

Strips markup off the variable’s value.

Returns:

  • (self)


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

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



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

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



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

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



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

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



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

alias to_citeproc to_s

#tokenizeArray<String> Also known as: extract_numbers

Returns tokenizes the variable’s value.

Returns:



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

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



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

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