Class: CiteProc::CitationData

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/citeproc/citation_data.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = nil, options = {}) ⇒ CitationData

Returns a new instance of CitationData.



128
129
130
131
132
# File 'lib/citeproc/citation_data.rb', line 128

def initialize(attributes = nil, options = {})
  @options = CitationData.defaults.merge(options)
  @items, @sorted_items = [], []
  merge(attributes)
end

Class Attribute Details

.cp2rbObject (readonly)

Returns the value of attribute cp2rb.



109
110
111
# File 'lib/citeproc/citation_data.rb', line 109

def cp2rb
  @cp2rb
end

.defaultsObject (readonly)

Returns the value of attribute defaults.



109
110
111
# File 'lib/citeproc/citation_data.rb', line 109

def defaults
  @defaults
end

.rb2cpObject (readonly)

Returns the value of attribute rb2cp.



109
110
111
# File 'lib/citeproc/citation_data.rb', line 109

def rb2cp
  @rb2cp
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



112
113
114
# File 'lib/citeproc/citation_data.rb', line 112

def id
  @id
end

#itemsObject (readonly)

Returns the value of attribute items.



114
115
116
# File 'lib/citeproc/citation_data.rb', line 114

def items
  @items
end

#optionsObject (readonly) Also known as: properties

Returns the value of attribute options.



114
115
116
# File 'lib/citeproc/citation_data.rb', line 114

def options
  @options
end

#sorted_itemsObject (readonly)

Returns the value of attribute sorted_items.



114
115
116
# File 'lib/citeproc/citation_data.rb', line 114

def sorted_items
  @sorted_items
end

Instance Method Details

#each(&block) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
# 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?Boolean

Returns:

  • (Boolean)


203
204
205
# File 'lib/citeproc/citation_data.rb', line 203

def footnote?
  options[:footnote] > 0
end

#indexObject



199
200
201
# File 'lib/citeproc/citation_data.rb', line 199

def index
  options[:footnote]
end

#initialize_copy(other) ⇒ Object



134
135
136
137
138
139
# 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

#inspectString

Returns a human-readable representation of the citation data.

Returns:

  • (String)

    a human-readable representation of the citation data



225
226
227
# File 'lib/citeproc/citation_data.rb', line 225

def inspect
  "#<CiteProc::CitationData items=[#{length}]>"
end

#merge(other) ⇒ Object Also known as: update



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# 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

#processed?Boolean

Returns:

  • (Boolean)


186
187
188
# File 'lib/citeproc/citation_data.rb', line 186

def processed?
  !!id
end

#sort!(&block) ⇒ Object



194
195
196
197
# File 'lib/citeproc/citation_data.rb', line 194

def sort!(&block)
  @sorted_items = items.sort(&block)
  self
end

#sorted?Boolean

Returns:

  • (Boolean)


190
191
192
# File 'lib/citeproc/citation_data.rb', line 190

def sorted?
  !sorted_items.empty?
end

#to_citeprocObject



207
208
209
210
211
212
213
214
215
216
# 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_jsonObject Also known as: to_s



218
219
220
# File 'lib/citeproc/citation_data.rb', line 218

def to_json
  ::JSON.dump(to_citeproc)
end