Class: Scholar::Citation

Inherits:
Object
  • Object
show all
Defined in:
lib/scholar/citation.rb

Overview

A Citation object containing the attributes of the citation and the HTML citation itself.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Citation

Creates a new Citation from the given attributes.

Attributes

  • options - The attributes of the Citation.

Options

  • :type - Not optional. The type of source you are citing.

  • :contributors - An array of hashes of contributors.

Examples

citation = Scholar::Citation.new({
  :type => :book,
  :title => "Foobar",
  :contributors => [
    {
      :role => :author,
      :first => "John",
      :last => "Sample"
    }
  ]
})

Obviously, you’d include more than that, but you need to see the specific sources for more documentation.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/scholar/citation.rb', line 43

def initialize(options = {})
  source = "Scholar::Sources::#{options[:type].to_s.camelize}".constantize

  @attributes = options
  @raw = @attributes.clone
  @attributes.delete(:type)

  @attributes.each do |k, v|
    if v.is_a?(Fixnum)
      @attributes[k] = v.to_s
    end
  end

  @attributes.delete_if {|k, v| v.nil? || v.empty? }

  if @attributes[:contributors]
    @attributes = Scholar::Utilities.contributors!(@attributes)
  end

  @attributes = Scholar::Utilities.order!(@attributes, source.sequence)

  attributes = @attributes.clone
  attributes = Scholar::Utilities.format!(attributes, source.rules)

  @html = Scholar::Utilities.concatenate!(attributes)
end

Instance Attribute Details

#attributesObject (readonly)

The pieces of data in the Citation.



11
12
13
# File 'lib/scholar/citation.rb', line 11

def attributes
  @attributes
end

#htmlObject (readonly)

The actual HTML citation.



14
15
16
# File 'lib/scholar/citation.rb', line 14

def html
  @html
end

#rawObject (readonly)

The raw hash given Citation.



8
9
10
# File 'lib/scholar/citation.rb', line 8

def raw
  @raw
end

Instance Method Details

#to_hashObject

Returns the Citation object in Hash form.



71
72
73
74
75
76
77
78
79
# File 'lib/scholar/citation.rb', line 71

def to_hash
  hash = {}

  instance_variables.each do |v|
    hash[v.to_s[1..-1].to_sym] = instance_variable_get(v)
  end

  hash
end