class CiteProc::Item
Items are similar to a Ruby Hash but pose a number of constraints on their contents: keys are always (implicitly converted to) symbols and values are strictly {Variable Variables}. When Items are constructed from (or merged with) JSON objects or Hashes {Variable} instances are automatically created by passing the variable's key as type to {Variable.create}; this will create the expected {Variable} type for all fields defined in CSL (for example, the `issued' field will become a {Date} object; unknown types will be converted to simple {Variable} instances, which should be fine for numeric or string values but may cause problems for more complex types.
Every Item provides accessor methods for all known field names; unknown fields can still be accessed using array accessor syntax.
i = Item.new(:edition => 3, :unknown_field => 42) i.edition #-> #<CiteProc::Number "3"> i[:unknown_field] #-> #<CiteProc::Variable "42">
Items can be converted to the CiteProc JSON format via {#to_citeproc} and {#to_json}.
Attributes
Public Class Methods
Hide attributes reader: All access should go through (read|write)_attribute
# File lib/citeproc/item.rb, line 77 def initialize(attributes = nil) merge(attributes) yield self if block_given? end
Public Instance Methods
# File lib/citeproc/item.rb, line 224 def <=>(other) return nil unless other.is_a?(Attributes) eql?(other) ? 0 : length <=> other.length end
# File lib/citeproc/item.rb, line 219 def ==(other) return false unless other.is_a?(Item) id == other.id end
Returns a CitationItem with a copy of this item attached as data.
If given, number is added as the item's citation-number variable.
@param number [Fixnum] the item's citation-number @return [CitationItem] a citation item for this item
# File lib/citeproc/item.rb, line 95 def cite(number = nil) CitationItem.new :id => id do |c| c.data = dup c.data[:'citation-number'] = number unless number.nil? end end
Calls a block once for each field in the item, passing the field's name-value pair as parameters.
If not block is given, an enumerator is returned instead.
item.each { |name, value| block } #-> item item.each #-> an enumerator
@yieldparam field [Symbol] the field name @yieldparam value [Variable] the value @return [self,Enumerator] the item or an enumerator if no block is given
# File lib/citeproc/item.rb, line 168 def each(&block) if block_given? attributes.each_pair(&block) self else to_enum end end
Calls a block once for each field in the item, passing the field's value as parameters.
If not block is given, an enumerator is returned instead.
item.each_value { |value| block } #-> item item.each_value #-> an enumerator
@yieldparam value [Variable] the value @return [self,Enumerator] the item or an enumerator if no block is given
# File lib/citeproc/item.rb, line 191 def each_value(&block) if block_given? attributes.each_value(&block) self else enum_for :each_value end end
An Item is interpreted as being English unless it has an attribute 'language' set to something other than 'en'.
@return [Boolean] whether or not this is an English Item
# File lib/citeproc/item.rb, line 148 def english? lang = language lang.nil? || lang == 'en' end
# File lib/citeproc/item.rb, line 82 def initialize_copy(other) @attributes = other.attributes.deep_copy end
@return [String] a string containing a human-readable
representation of the item
# File lib/citeproc/item.rb, line 264 def inspect "#<CiteProc::Item id=#{id.to_s.inspect} attributes={#{attributes.length}}>" end
# File lib/citeproc/item.rb, line 140 def language unobservable_read_attribute(:language) end
# File lib/citeproc/item.rb, line 102 def observable_read_attribute(key) value = original_read_attribute(key) return if suppressed?(key) value ensure changed notify_observers :read, key, value end
# File lib/citeproc/item.rb, line 111 def simulate_read_attribute(key, value) changed notify_observers :read, key, value end
# File lib/citeproc/item.rb, line 204 def suppress!(*keys) keys.flatten.each do |key| suppressed << key.to_s end suppressed.sort! suppressed.uniq! self end
# File lib/citeproc/item.rb, line 215 def suppressed @suppressed ||= [] end
# File lib/citeproc/item.rb, line 200 def suppressed?(key) suppressed.include?(key.to_s) end
Returns a corresponding BibTeX::Entry if the bibtex-ruby gem is installed; otherwise returns a BibTeX string.
# File lib/citeproc/item.rb, line 231 def to_bibtex # hash = to_hash # # hash[:type] = Item.bibtex_types[hash[:type]] # # if hash.has_key?(:issued) # date = hash.delete(:issued) # hash[:year] = date.year # hash[:month] = date.month # end # # Variable.fields[:date].each do |field| # hash[field] = hash[field].to_s if hash.has_key?(field) # end # # Variable.fields[:names].each do |field| # hash[field] = hash[field].to_bibtex # end raise 'not implemented yet' end
@return [Symbol,nil] the item's id
# File lib/citeproc/item.rb, line 254 def to_sym if id? id.to_s.intern else nil end end
@param name [Symbol] the name of the variable
@param options [Hash] @option options [:short] :form (nil) when given, the variable's
short form will be returned if available.
@return [Variable, nil] the matching variable
# File lib/citeproc/item.rb, line 131 def variable(name, options = {}) if options.key?(:form) && options[:form].to_sym == :short var = read_attribute "#{name}-short" return var unless var.nil? end read_attribute name end
Private Instance Methods
@private
# File lib/citeproc/item.rb, line 271 def filter_value(value, key = nil) Variable.create!(value, key) end