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)



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

def factories
  @factories
end

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



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

def fields
  @fields
end

.markupRegexp



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

def markup
  @markup
end

.types{Symbol => Symbol} (readonly)



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

def types
  @types
end

Instance Attribute Details

#valueObject



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">

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.

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.



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



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



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

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

#numeric?Boolean



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



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.

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?



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

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

#strip_markupString



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.



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

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

#to_fFloat



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



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

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

#to_jsonString



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



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

alias to_citeproc to_s

#tokenizeArray<String> Also known as: extract_numbers



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



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

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