Module: Grom::Helper

Defined in:
lib/grom/helper.rb

Overview

Namespace for helper methods.

Since:

  • 0.1.0

Class Method Summary collapse

Class Method Details

.get_id(uri) ⇒ String?

Returns the last part of a uri

Parameters:

  • uri (String)

    uri

Returns:

  • (String)

    the last part of the uri or ‘type’ if the uri is an RDF type uri

  • (nil)

    if the uri is not valid

Since:

  • 0.1.0



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/grom/helper.rb', line 40

def self.get_id(uri)
  return nil if uri.to_s['/'].nil?

  if uri == RDF::RDFS.label.to_s
    return 'label'
  elsif uri == RDF::Vocab::SKOS.prefLabel.to_s
    return 'prefLabel'
  elsif uri == RDF.type.to_s
    return 'type'
  else
    uri_object = RDF::URI(uri)

    return uri_object.fragment ? uri_object.fragment : uri_object.path.split('/').last
  end
end

.lazy_array_insert(hash, key, value) ⇒ Array

Creates or inserts an array and values into a hash.

Examples:

Adding values to an existing array within the hash

Grom::Helper.lazy_array_insert({ :numbers => [1, 2, 3] }, :numbers, 4) #=> [1, 2, 3, 4]

Adding values to a hash with no existing array

Grom::Helper.lazy_array_insert({}, :numbers, 4) #=> [4]

Parameters:

  • hash (Hash)
  • key (String, Symbol)

    the key to use in the hash.

  • value (Object)

    the value to attribute to the key.

Returns:

  • (Array)

    array with values inserted

Since:

  • 0.1.0



30
31
32
33
# File 'lib/grom/helper.rb', line 30

def self.lazy_array_insert(hash, key, value)
  hash[key] ||= []
  hash[key] << value
end

.pluralize_instance_variable_symbol(string) ⇒ Symbol

Creates a symbol in instance variable format which has been underscored, pluralized and downcased.

Examples:

Create a pluralized instance variable symbol

Grom::Helper.pluralize_instance_variable_symbol('sittingHasPerson') #=> :@sitting_has_people

Parameters:

  • string (String)

    instance variable name.

Returns:

  • (Symbol)

    instance variable name as a symbol.

Since:

  • 0.1.0



11
12
13
14
15
16
# File 'lib/grom/helper.rb', line 11

def self.pluralize_instance_variable_symbol(string)
  string = ActiveSupport::Inflector.underscore(string)
  string = ActiveSupport::Inflector.pluralize(string).downcase

  "@#{string}".to_sym
end