module Kilt

Public Class Methods

all_used_fields() click to toggle source

Get every field type used Returns: Array of strings Example: Kilt.all_used_fields

# File lib/kilt.rb, line 84
def self.all_used_fields
  used_field_types = Kilt.config[:objects].map do |object|
                       object.map do |config|
                         begin
                           config[:fields].map { |_, v| v }
                         rescue
                           nil
                         end
                       end
                     end.flatten
  used_field_types.select { |x| x }.group_by { |x| x }.map { |x| x[0] }
end
create(object) click to toggle source

Create an object Returns: boolean Example: Kilt.create(object)

# File lib/kilt.rb, line 37
def self.create(object)
  object['created_at'] = object['updated_at'] = Time.now
  object['unique_id']  = "#{(Time.now.to_f * 1000).to_i}"
  object['type']       = object.instance_eval { @type }
  object['slug']       = Slugger.slug_for object

  Utils.database.create object
end
delete(slug) click to toggle source

Delete an object Returns: boolean Example: Kilt.delete('some-object')

# File lib/kilt.rb, line 59
def self.delete(slug)
  Utils.database.delete slug
end
get(slug) click to toggle source

Get the content for a specific object Returns: Kilt::Object instance Example: Kilt.object('big-event')

# File lib/kilt.rb, line 66
def self.get(slug)
  result = Utils.database.find(slug)
  result ? Kilt::Object.new(result['type'], result)
         : nil
end
get_collection(object_type) click to toggle source

Get a list of objects Returns: array of hashes Example: Kilt.objects('events') Used directly or via method_missing

# File lib/kilt.rb, line 76
def self.get_collection(object_type)
  results = Utils.database.find_all_by_type object_type
  Kilt::ObjectCollection.new results
end
method_missing(method, *args) click to toggle source

Auto-generated endpoints

# File lib/kilt.rb, line 11
def self.method_missing(method, *args)
  begin
    
    if Utils.is_singular? method.to_s
      # Get the configuration for a type
      # Example: Kilt.event
      Kilt.config.objects[method]
    else
      # Get a list of objects
      # Example: Kilt.events
      Kilt.get_collection method.to_s 
    end
  end
end
types() click to toggle source

Get the list of types Returns: array of type names Example: Kilt.types

# File lib/kilt.rb, line 29
def self.types
  Kilt.config.objects.map { |key, value| key.to_s }
end
update(slug, object) click to toggle source

Update an object Returns: boolean Example: Kilt.update(object)

# File lib/kilt.rb, line 49
def self.update(slug, object)
  object['updated_at'] = Time.now
  object['slug']       = Slugger.slug_for object

  Utils.database.update object
end