Class: CiteProc::Variable Abstract
- Inherits:
-
Object
- Object
- CiteProc::Variable
- Extended by:
- Forwardable
- Includes:
- Comparable
- Defined in:
- lib/citeproc/variable.rb
Overview
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.
Class Attribute Summary collapse
-
.factories ⇒ {Symbol => Class}
readonly
Mapping of field names to their respective Variable classes.
-
.fields ⇒ {Symbol => Array<Symbol>}
readonly
Mapping of variable types to their respective field names.
-
.markup ⇒ Regexp
Pattern used to strip markup off values.
-
.types ⇒ {Symbol => Symbol}
readonly
Mapping of field names to variable types.
Instance Attribute Summary collapse
-
#value ⇒ Object
The value wrapped by this variable.
Class Method Summary collapse
Instance Method Summary collapse
-
#<=>(other) ⇒ Fixnum?
Compares the variable with the passed-in value.
- #date? ⇒ Boolean
-
#initialize(value = nil) ⇒ Variable
constructor
Creates new Variable for the passed-in value.
- #initialize_copy(other) ⇒ Object
-
#inspect ⇒ String
A human-readable representation of the variable.
-
#numeric? ⇒ Boolean
Whether or not the variable’s value is numeric.
-
#plural? ⇒ Boolean
Whether or not the variable holds a plural value.
-
#replace(value) ⇒ self
The replace method is typically called by the Variable’s constructor.
-
#romanize ⇒ String?
Roman equivalent of the variable’s numeric value.
-
#strip_markup ⇒ String
The variable’s value stripped of markup.
-
#strip_markup! ⇒ self
Strips markup off the variable’s value.
-
#to_f ⇒ Float
The first (!) numeric or floating point data contained in the variable’s value; zero if no numeric data is present.
-
#to_i ⇒ Fixnum
The first (!) numeric data contained in the variable’s value; zero if no numeric data is present.
-
#to_json ⇒ String
A JSON string representation of the variable.
-
#to_s ⇒ String
(also: #to_citeproc)
The variable’s value as a string.
-
#tokenize ⇒ Array<String>
(also: #extract_numbers)
Tokenizes the variable’s value.
-
#type ⇒ Symbol
The variable’s type.
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.
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.
66 67 68 |
# File 'lib/citeproc/variable.rb', line 66 def fields @fields end |
.markup ⇒ Regexp
Returns 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.
70 71 72 |
# File 'lib/citeproc/variable.rb', line 70 def types @types end |
Instance Attribute Details
#value ⇒ Object
Returns 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.
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.
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 |
#inspect ⇒ String
Returns 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.
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.
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.
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 |
#romanize ⇒ String?
Returns 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_markup ⇒ String
Returns 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.
255 256 257 |
# File 'lib/citeproc/variable.rb', line 255 def strip_markup! gsub!(Variable.markup, '') end |
#to_f ⇒ Float
Returns 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_i ⇒ Fixnum
Returns 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_json ⇒ String
Returns 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_s ⇒ String Also known as: to_citeproc
Returns the variable’s value as a string.
279 |
# File 'lib/citeproc/variable.rb', line 279 alias to_citeproc to_s |
#tokenize ⇒ Array<String> Also known as: extract_numbers
Returns tokenizes the variable’s value.
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 |
#type ⇒ Symbol
Returns 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 |