Class: CiteProc::CitationData
- Inherits:
-
Object
- Object
- CiteProc::CitationData
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/citeproc/citation_data.rb
Class Attribute Summary collapse
-
.cp2rb ⇒ Object
readonly
Returns the value of attribute cp2rb.
-
.defaults ⇒ Object
readonly
Returns the value of attribute defaults.
-
.rb2cp ⇒ Object
readonly
Returns the value of attribute rb2cp.
Instance Attribute Summary collapse
-
#id ⇒ Object
Returns the value of attribute id.
-
#items ⇒ Object
readonly
Returns the value of attribute items.
-
#options ⇒ Object
(also: #properties)
readonly
Returns the value of attribute options.
-
#sorted_items ⇒ Object
readonly
Returns the value of attribute sorted_items.
Instance Method Summary collapse
- #each(&block) ⇒ Object
- #footnote? ⇒ Boolean
- #index ⇒ Object
-
#initialize(attributes = nil, options = {}) ⇒ CitationData
constructor
A new instance of CitationData.
- #initialize_copy(other) ⇒ Object
-
#inspect ⇒ String
A human-readable representation of the citation data.
- #merge(other) ⇒ Object (also: #update)
- #processed? ⇒ Boolean
- #sort!(&block) ⇒ Object
- #sorted? ⇒ Boolean
- #to_citeproc ⇒ Object
- #to_json ⇒ Object (also: #to_s)
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 = CitationData.defaults.merge() @items, @sorted_items = [], [] merge(attributes) end |
Class Attribute Details
.cp2rb ⇒ Object (readonly)
Returns the value of attribute cp2rb.
109 110 111 |
# File 'lib/citeproc/citation_data.rb', line 109 def cp2rb @cp2rb end |
.defaults ⇒ Object (readonly)
Returns the value of attribute defaults.
109 110 111 |
# File 'lib/citeproc/citation_data.rb', line 109 def defaults @defaults end |
.rb2cp ⇒ Object (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
#id ⇒ Object
Returns the value of attribute id.
112 113 114 |
# File 'lib/citeproc/citation_data.rb', line 112 def id @id end |
#items ⇒ Object (readonly)
Returns the value of attribute items.
114 115 116 |
# File 'lib/citeproc/citation_data.rb', line 114 def items @items end |
#options ⇒ Object (readonly) Also known as: properties
Returns the value of attribute options.
114 115 116 |
# File 'lib/citeproc/citation_data.rb', line 114 def @options end |
#sorted_items ⇒ Object (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
203 204 205 |
# File 'lib/citeproc/citation_data.rb', line 203 def footnote? [:footnote] > 0 end |
#index ⇒ Object
199 200 201 |
# File 'lib/citeproc/citation_data.rb', line 199 def index [: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..dup @items = other.items.map(&:dup) @sorted_items = other.items.map(&:dup) @id = other.id.dup if other.processed? end |
#inspect ⇒ String
Returns 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) .merge!(convert_from_citeproc(Hash[properties])) unless properties.nil? @id = other[:id] if other.has_key?(:id) self end |
#processed? ⇒ 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
190 191 192 |
# File 'lib/citeproc/citation_data.rb', line 190 def sorted? !sorted_items.empty? end |
#to_citeproc ⇒ Object
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_json ⇒ Object Also known as: to_s
218 219 220 |
# File 'lib/citeproc/citation_data.rb', line 218 def to_json ::JSON.dump(to_citeproc) end |