Module: Kilt

Defined in:
lib/kilt.rb,
lib/kilt/form.rb,
lib/kilt/utils.rb,
lib/kilt/engine.rb,
lib/kilt/object.rb,
lib/kilt/upload.rb,
lib/kilt/exceptions.rb,
lib/kilt/formatting.rb,
lib/kilt/db/rethink_db.rb,
lib/kilt/db/active_record.rb,
lib/kilt/object_collection.rb,
app/controllers/kilt/kilt_controller.rb,
lib/generators/kilt/object_generator.rb,
lib/generators/kilt/backend_generator.rb,
lib/generators/kilt/install_generator.rb,
lib/generators/kilt/frontend_generator.rb

Defined Under Namespace

Modules: DB, Form, Formatting, Generators Classes: CantConnectToDatabaseError, CantSetupDatabaseError, ConfigError, DatabaseError, Engine, KiltController, NoConfigError, NoDatabaseConfigError, Object, ObjectCollection, Upload, Utils

Class Method Summary collapse

Class Method Details

.create(object) ⇒ Object

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



40
41
42
43
44
45
46
47
# File 'lib/kilt.rb', line 40

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']       = slug_for object

  Utils.database.create object
end

.delete(slug) ⇒ Object

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



62
63
64
# File 'lib/kilt.rb', line 62

def self.delete(slug)
  Utils.database.delete slug
end

.get(slug) ⇒ Object

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



69
70
71
72
73
# File 'lib/kilt.rb', line 69

def self.get(slug)
  result = Utils.database.find(slug)
  result ? Kilt::Object.new(result['type'], result)
         : nil
end

.get_collection(object_type) ⇒ Object

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



79
80
81
82
# File 'lib/kilt.rb', line 79

def self.get_collection(object_type)
  results = Utils.database.find_all_by_type object_type
  Kilt::ObjectCollection.new results
end

.method_missing(method, *args) ⇒ Object

Auto-generated endpoints



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/kilt.rb', line 14

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

.typesObject

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



32
33
34
# File 'lib/kilt.rb', line 32

def self.types
  Kilt.config.objects.map { |key, value| key.to_s }
end

.update(slug, object) ⇒ Object

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



52
53
54
55
56
57
# File 'lib/kilt.rb', line 52

def self.update(slug, object)
  object['updated_at'] = Time.now
  object['slug']       = slug_for object

  Utils.database.update object
end