module BlockScore::Util

Constants

PLURAL_LOOKUP

Public Instance Methods

create_array(resource, arr) click to toggle source
# File lib/blockscore/util.rb, line 27
def create_array(resource, arr)
  arr.map { |obj| create_object(resource, obj) }
end
create_object(resource, options = {}) click to toggle source
# File lib/blockscore/util.rb, line 23
def create_object(resource, options = {})
  to_constant("BlockScore::#{to_camelcase(resource)}").new(options)
end
parse_json(json_obj) click to toggle source
# File lib/blockscore/util.rb, line 17
def parse_json(json_obj)
  parse_json! json_obj
rescue JSON::ParserError
  raise Error, 'An error has occurred. If this problem persists, please message support@blockscore.com.'
end
parse_json!(json_obj) click to toggle source
# File lib/blockscore/util.rb, line 13
def parse_json!(json_obj)
  JSON.parse(json_obj, symbolize_names: true)
end
to_camelcase(str) click to toggle source
# File lib/blockscore/util.rb, line 67
def to_camelcase(str)
  str.split('_').map(&:capitalize).join('')
end
to_constant(camel_cased_word) click to toggle source

Taken from activesupport: git.io/vkWtR

# File lib/blockscore/util.rb, line 36
def to_constant(camel_cased_word)
  names = camel_cased_word.split('::')

  # Trigger a built-in NameError exception including the ill-formed constant in the message.
  Object.const_get(camel_cased_word) if names.empty?

  # Remove the first blank element in case of '::ClassName' notation.
  names.shift if names.size > 1 && names.first.empty?

  names.inject(Object) do |constant, name|
    if constant == Object
      constant.const_get(name)
    else
      candidate = constant.const_get(name)
      next candidate if constant.const_defined?(name, false)
      next candidate unless Object.const_defined?(name)

      # Go down the ancestors to check if it is owned directly. The check
      # stops when we reach Object or the end of ancestors tree.
      constant = constant.ancestors.inject do |const, ancestor|
        break const    if ancestor == Object
        break ancestor if ancestor.const_defined?(name, false)
        const
      end

      # owner is in Object, so raise
      constant.const_get(name, false)
    end
  end
end
to_plural(str) click to toggle source
# File lib/blockscore/util.rb, line 31
def to_plural(str)
  PLURAL_LOOKUP[str]
end
to_underscore(str) click to toggle source

Taken from Rulers: git.io/vkWqf

# File lib/blockscore/util.rb, line 72
def to_underscore(str)
  str.gsub(/::/, '/')
    .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
    .gsub(/([a-z\d])([A-Z])/, '\1_\2')
    .tr('-', '_')
    .downcase
end