class CiteProc::CitationData

Attributes

cp2rb[R]
defaults[R]
rb2cp[R]
id[RW]
items[R]
options[R]
properties[R]
sorted_items[R]

Public Class Methods

new(attributes = nil, options = {}) click to toggle source
# File lib/citeproc/citation_data.rb, line 128
def initialize(attributes = nil, options = {})
  @options = CitationData.defaults.merge(options)
  @items, @sorted_items = [], []
  merge(attributes)
end

Public Instance Methods

each(&block) click to toggle source
# File lib/citeproc/citation_data.rb, line 172
def each(&block)
  if block_given?
    if sorted?
      sorted_items.each(&block)
    else
      items.each(&block)
    end

    self
  else
    to_enum
  end
end
footnote?() click to toggle source
# File lib/citeproc/citation_data.rb, line 203
def footnote?
  options[:footnote] > 0
end
index() click to toggle source
# File lib/citeproc/citation_data.rb, line 199
def index
  options[:footnote]
end
initialize_copy(other) click to toggle source
# File lib/citeproc/citation_data.rb, line 134
def initialize_copy(other)
  @options = other.options.dup
  @items = other.items.map(&:dup)
  @sorted_items = other.items.map(&:dup)
  @id = other.id.dup if other.processed?
end
inspect() click to toggle source

@return [String] a human-readable representation of the citation data

# File lib/citeproc/citation_data.rb, line 225
def inspect
  "#<CiteProc::CitationData items=[#{length}]>"
end
merge(other) click to toggle source
# File lib/citeproc/citation_data.rb, line 141
def merge(other)
  return self if other.nil?

  case other
  when String, /^\s*\{/
    other = JSON.parse(other, :symbolize_names => true)
  when Hash
    # do nothing
  when Array
    other = { :items => other }
  when Attributes
    other = other.to_hash
  else
    raise ParseError, "failed to merge citation data and #{other.inspect}"
  end

  other = convert_from_citeproc(other)

  items.concat(Array(other.delete(:items)).map { |i| CitationItem.create!(i) })
  sorted_items.concat(Array(other.delete(:sorted_items)))

  properties = other.delete(:options)
  options.merge!(convert_from_citeproc(Hash[properties])) unless properties.nil?

  @id = other[:id] if other.has_key?(:id)

  self
end
Also aliased as: update
processed?() click to toggle source
# File lib/citeproc/citation_data.rb, line 186
def processed?
  !!id
end
sort!(&block) click to toggle source
# File lib/citeproc/citation_data.rb, line 194
def sort!(&block)
  @sorted_items = items.sort(&block)
  self
end
sorted?() click to toggle source
# File lib/citeproc/citation_data.rb, line 190
def sorted?
  !sorted_items.empty?
end
to_citeproc() click to toggle source
# File lib/citeproc/citation_data.rb, line 207
def to_citeproc
  cp = {}

  cp[CitationData.rb2cp[:items]] = items.map(&:to_citeproc)
  cp[CitationData.rb2cp[:options]] = { CitationData.rb2cp[:footnote] => index }

  cp[CitationData.rb2cp[:id]] = id if processed?

  cp
end
to_json() click to toggle source
# File lib/citeproc/citation_data.rb, line 218
def to_json
  ::JSON.dump(to_citeproc)
end
Also aliased as: to_s
to_s()
Alias for: to_json
update(other)
Alias for: merge

Private Instance Methods

convert_from_citeproc(hash) click to toggle source
# File lib/citeproc/citation_data.rb, line 231
def convert_from_citeproc(hash)
  hash = hash.symbolize_keys

  CitationData.cp2rb.each do |cp, rb|
    cp = cp.to_sym
    hash[rb] = hash.delete(cp) if hash.has_key?(cp)
  end

  hash
end