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
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.
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.
65 66 67 |
# File 'lib/citeproc/variable.rb', line 65 def fields @fields end |
.markup ⇒ Regexp
Returns 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.
69 70 71 |
# File 'lib/citeproc/variable.rb', line 69 def types @types end |
Instance Attribute Details
#value ⇒ Object
Returns 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.
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.
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.
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
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 |
#inspect ⇒ String
Returns 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.
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.
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.
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 |
#romanize ⇒ String?
Returns 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_markup ⇒ String
Returns 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.
254 255 256 |
# File 'lib/citeproc/variable.rb', line 254 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.
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_i ⇒ Fixnum
Returns 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_json ⇒ String
Returns 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_s ⇒ String Also known as: to_citeproc
Returns the variable’s value as a string.
278 |
# File 'lib/citeproc/variable.rb', line 278 alias to_citeproc to_s |
#tokenize ⇒ Array<String> Also known as: extract_numbers
Returns tokenizes the variable’s value.
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 |
#type ⇒ Symbol
Returns 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 |