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/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: Form, 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)



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

def self.create(object)
  object['created_at'] = object['updated_at'] = Time.now
  object['unique_id'] = "#{(Time.now.to_f * 1000).to_i}"
  result = Utils.db do
    
    # Check for slug uniqueness
    results = r.db(Kilt.config.db.db).table('objects').filter({'slug' => "#{object['slug'].to_s}"}).run
    if results.to_a.length > 0
      object['slug'] = "#{object['slug']}-#{(Time.now.to_f * 1000).to_i}"
    end
    
    # Insert the record
    r.db(Kilt.config.db.db).table('objects').insert(object.values).run
    
  end
  (result['errors'] == 0)
end

.delete(slug) ⇒ Object

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



88
89
90
91
92
93
# File 'lib/kilt.rb', line 88

def self.delete(slug)
  result = Utils.db do
    r.db(Kilt.config.db.db).table('objects').filter({'slug' => "#{slug.to_s}"}).delete().run
  end
  (result['errors'] == 0)
end

.get(slug) ⇒ Object

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



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/kilt.rb', line 98

def self.get(slug)
  # connect to the db, get the object, close the connection, return the object
  values = Utils.db do
    r.db(Kilt.config.db.db).table('objects').filter({'slug' => "#{slug.to_s}"}).run
  end
  
  result = values.to_a.first
  
  # create an object and return it
  Kilt::Object.new(result['type'], result)
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



114
115
116
117
118
119
120
121
122
# File 'lib/kilt.rb', line 114

def self.get_collection(object_type)
  # connect to the db, get the date, close the connection, return the array
  results = Utils.db do
    r.db(Kilt.config.db.db).table('objects').filter({'type' => "#{object_type.singularize.to_s}"}).run
  end
  
  # create an object collection
  Kilt::ObjectCollection.new(results.to_a)
end

.method_missing(method, *args) ⇒ Object

Auto-generated endpoints



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

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



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

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)



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/kilt.rb', line 60

def self.update(slug, object)
  object['updated_at'] = Time.now
  result = Utils.db do
    
    # Keep the original slug handy
    results = r.db(Kilt.config.db.db).table('objects').filter({'slug' => "#{slug}"}).limit(1).run
    original = results.to_a.first['unique_id']
    
    # Check for slug uniqueness
    results = r.db(Kilt.config.db.db).table('objects').filter({'slug' => "#{object['slug']}"}).run
    if results
      result = results.to_a.first
      if result && result['unique_id'] != original
        object['slug'] = "#{Kilt::Utils.slugify(object['name'])}-#{(Time.now.to_f * 1000).to_i}"
      end
    end
    
    # Update the record
    r.db(Kilt.config.db.db).table('objects').filter({'unique_id' => "#{original}"}).update(object.values).run
    
  end
  (result['errors'] == 0)
end