Module: Scholar::Utilities::Data

Included in:
Scholar::Utilities
Defined in:
lib/scholar/utilities/data.rb

Overview

Utilities for dealing with hashes and actually creating the citations.

Instance Method Summary collapse

Instance Method Details

#concatenate!(attributes) ⇒ Object

Concatenates hashes values into space-separated String.



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

def concatenate!(attributes)
  attributes.values.compact.reject(&:empty?).join(' ').squish
end

#contributors!(attributes) ⇒ Object

Take contributors in hash and makes a ContributorList for each role.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/scholar/utilities/data.rb', line 16

def contributors!(attributes)
  data = attributes[:contributors]

  attributes.delete(:contributors)

  contributors = {}
  data.each do |c|
    role = c[:role].to_s.pluralize.to_sym

    if contributors.has_key?(role)
      contributors[role] << c
    else
      contributors[role] = [c]
    end
  end

  contributors.each do |role, list|
    if role == :authors
      contributors[role] = ContributorList.new(list, :author).names
    else
      contributors[role] = ContributorList.new(list).names
    end
  end

  attributes.merge(contributors)
end

#format!(attributes, rules = []) ⇒ Object

Formats the keys of a hash with given rules.



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/scholar/utilities/data.rb', line 44

def format!(attributes, rules = [])
  rules.each do |key, action|
    unless attributes[key].nil?
      attributes[key] = Scholar::Utilities.instance_eval do
        action.call(attributes[key])
      end
    end
  end

  attributes
end

#order!(hash, template) ⇒ Object

Re-orders a hash based on a template.



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/scholar/utilities/data.rb', line 57

def order!(hash, template)
  ordered = ActiveSupport::OrderedHash.new

  template.each do |key|
    if hash[key]
      ordered[key] = hash[key]
    end
  end

  ordered
end