Module: Grom::Helpers

Included in:
Base
Defined in:
lib/grom/helpers.rb

Instance Method Summary collapse

Instance Method Details

#all_base_url_builder(class_name, *options) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/grom/helpers.rb', line 25

def all_base_url_builder(class_name, *options)
  endpoint = "#{API_ENDPOINT}/#{create_plural_property_name(class_name)}"
    options.each do |option|
      endpoint += "/#{option}" unless option.nil?
    end
  endpoint
end

#associations_url_builder(owner_object, associated_class_name, options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/grom/helpers.rb', line 6

def associations_url_builder(owner_object, associated_class_name, options={})
  id = owner_object.id
  associated_class_name = options[:single].nil? ? create_plural_property_name(associated_class_name) : create_property_name(associated_class_name)
  endpoint = "#{find_base_url_builder(owner_object.class.name, id)}/#{associated_class_name}"
  if options[:optional].nil?
    endpoint += '.ttl'
  else
    options[:optional].each do |option|
      endpoint += "/#{option}" unless option.nil?
    end
    endpoint += '.ttl'
  end
  endpoint
end

#create_class_name(plural_name) ⇒ Object



33
34
35
# File 'lib/grom/helpers.rb', line 33

def create_class_name(plural_name)
  ActiveSupport::Inflector.camelize(ActiveSupport::Inflector.singularize(plural_name.to_s).capitalize)
end

#create_plural_property_name(class_name) ⇒ Object



37
38
39
# File 'lib/grom/helpers.rb', line 37

def create_plural_property_name(class_name)
  ActiveSupport::Inflector.pluralize(create_property_name(class_name))
end

#create_property_name(class_name) ⇒ Object



41
42
43
# File 'lib/grom/helpers.rb', line 41

def create_property_name(class_name)
  ActiveSupport::Inflector.underscore(class_name).downcase
end

#find_base_url_builder(class_name, id) ⇒ Object



21
22
23
# File 'lib/grom/helpers.rb', line 21

def find_base_url_builder(class_name, id)
  "#{API_ENDPOINT}/#{create_plural_property_name(class_name)}/#{id}"
end

#order_list(arr, *parameters) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/grom/helpers.rb', line 45

def order_list(arr, *parameters)
  rejected = []
  arr.delete_if{ |obj| rejected << obj if parameters.any?{ |param| obj.send(param).nil? } }
  sorted_arr = arr.sort_by do |obj|
    parameters.map{ |param| obj.send(param) }.select{ |p| not p.nil? }
  end
  rejected.concat(sorted_arr)
end

#order_list_by_through(arr, through_association, property) ⇒ Object



54
55
56
# File 'lib/grom/helpers.rb', line 54

def order_list_by_through(arr, through_association, property)
  arr.map{ |obj| obj.send(through_association) }.flatten.sort{ |a, b| a[property] <=> b[property] }
end