Class: Kilt::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/kilt/utils.rb

Class Method Summary collapse

Class Method Details

.db(&block) ⇒ Object

Make a db call



39
40
41
42
43
44
# File 'lib/kilt/utils.rb', line 39

def self.db(&block)
  db = r.connect(:host => Kilt.config.db.host, :port => Kilt.config.db.port).repl
  results = block.call()
  db.close
  results
end

.deslugify(str) ⇒ Object

Break down a slug



105
106
107
108
109
# File 'lib/kilt/utils.rb', line 105

def self.deslugify(str)
  ret = str.to_s
  ret.gsub! /_/, " "   
  ret.capitalize
end

.download_location(type, value) ⇒ Object

Get a file download URL



66
67
68
69
70
71
72
# File 'lib/kilt/utils.rb', line 66

def self.download_location(type, value)
  if Kilt.config.storage.strategy == 'local'
    "/uploads/#{type.to_s}/#{value.to_s}"
  elsif Kilt.config.storage.strategy == 's3'
    "http://#{Kilt.config.s3.bucket}.s3.amazonaws.com/#{type.to_s}/#{value.to_s}"
  end
end

.ensure_local_storage_dir_existsObject

Ensure we have local storage dirs



47
48
49
50
51
# File 'lib/kilt/utils.rb', line 47

def self.ensure_local_storage_dir_exists
  Dir.mkdir(Rails.root.join('public', 'uploads'))         unless File.exists?(Rails.root.join('public', 'uploads'))  
  Dir.mkdir(Rails.root.join('public', 'uploads', 'file')) unless File.exists?(Rails.root.join('public', 'uploads', 'file')) 
  Dir.mkdir(Rails.root.join('public', 'uploads', 'image')) unless File.exists?(Rails.root.join('public', 'uploads', 'image'))  
end

.ensure_s3_bucket_existsObject

Ensure we have an s3 bucket



54
55
56
57
58
59
60
61
62
63
# File 'lib/kilt/utils.rb', line 54

def self.ensure_s3_bucket_exists
  s3 = AWS::S3.new(
    :access_key_id     => Kilt.config.s3.key,
    :secret_access_key => Kilt.config.s3.secret)  
  puts s3 
  bucket = s3.buckets[Kilt.config.s3.bucket]
  if !bucket.exists?
    bucket = s3.buckets.create(Kilt.config.s3.bucket, :acl => :public_read)
  end
end

.is_singular?(str) ⇒ Boolean

Determine if the string passed in is singular or not

Returns:

  • (Boolean)


75
76
77
# File 'lib/kilt/utils.rb', line 75

def self.is_singular?(str)
  str.pluralize != str && str.singularize == str
end

.setup_dbObject

Set up the database



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/kilt/utils.rb', line 5

def self.setup_db
  if Kilt.config.db.host && Kilt.config.db.port
    begin
      db = r.connect(:host => Kilt.config.db.host, :port => Kilt.config.db.port).repl
    rescue
      raise Kilt::CantConnectToDatabaseError 
    end
    
    begin
      
      # See if the db exists and create it otherwise
      dbs = r.db_list.run
      if !dbs.to_a.include? Kilt.config.db.db
        r.db_create(Kilt.config.db.db).run
      end
      
      # See if the table exists and create it otherwise
      tables = r.db(Kilt.config.db.db).table_list.run
      if !tables.to_a.include? "objects"
        r.db(Kilt.config.db.db).table_create("objects", :primary_key => "unique_id").run
      end
  
    rescue
      raise Kilt::CantSetupDatabaseError
    ensure
      db.close
    end
    
  else
    raise Kilt::NoDatabaseConfigError
  end
end

.slugify(str) ⇒ Object

Create a slug



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/kilt/utils.rb', line 80

def self.slugify(str)
  #strip the string
  ret = str.strip

  #blow away apostrophes
  ret.gsub! /['`]/,""

  # @ --> at, and & --> and
  ret.gsub! /\s*@\s*/, " at "
  ret.gsub! /\s*&\s*/, " and "

  #replace all non alphanumeric, underscore or periods with dash
  ret.gsub! /\s*[^A-Za-z0-9\.\_]\s*/, '-'  

  #convert double dash to single
  ret.gsub! /-+/, "-"

  #strip off leading/trailing dash
  ret.gsub! /\A[-\.]+|[-\.]+\z/, ""

  #return a downcase string
  ret.downcase
end

.tipsObject

Print a list of objects, functions, etc.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/kilt/utils.rb', line 112

def self.tips
  lines = []
  if !Kilt.config.empty? && Kilt.config.db
    lines << ''
    
    # Store the first type so we can use it down below
    first_type = nil
    Kilt.types.each do |type|
      
      if !first_type
        first_type = type
      end
      
      lines << "#{type.capitalize}: "
      lines << "  Kilt.#{type} -> Type definition"
      lines << "  Kilt.#{type}.fields -> List of fields"
      lines << "  Kilt.#{type.pluralize} -> Array of all #{type.capitalize} objects in natural order"
      lines << "  Kilt.#{type.pluralize}.order -> Array of all #{type.capitalize} objects ordered by 'name' field"
      lines << "  Kilt.#{type.pluralize}.order('age') -> Array of all #{type.capitalize} objects ordered by 'age' field"
      lines << "  Kilt.#{type.pluralize}.order('age').group('section') -> Array of all #{type.capitalize} objects ordered by 'age' field, then grouped by the 'section' field"
    end
    lines << ''
    lines << 'Get object:'
    lines << '  Kilt.get(\'some-slug\')'
    lines << ''
    lines << 'Loop through objects:'
    lines << "  Kilt.#{first_type.pluralize}.each do |#{first_type}|"
    lines << "    puts #{first_type}['name']"
    lines << '  end'
    lines << ''
    lines << 'Loop through objects ordered by field \'age\':'
    lines << "  Kilt.#{first_type.pluralize}.order('age').each do |#{first_type}|"
    lines << "    puts #{first_type}['name']"
    lines << '  end'        
    lines << ''
    lines << 'Loop through objects ordered by field \'name\' (the default), then grouped by field \'section\':'
    lines << "  Kilt.#{first_type.pluralize}.order.group(\'section\').each do |section, #{first_type.downcase.pluralize}|"
    lines << '    puts section'
    lines << "    #{first_type.downcase.pluralize}.each do |#{first_type.downcase}|"
    lines << "      puts #{first_type.downcase}['name']"
    lines << "    end"
    lines << '  end'
  else
    lines << 'The Kilt gem has been installed, but you haven\'t configured it yet.'
    lines << 'Start configuring Kilt by running the following command:'
    lines << ''
    lines << '   rails g kilt:backend'
    lines << ''
    lines << 'Then open config/kilt/config.yml and config/kilt/creds.yml, add your database information, define your data model, start Rails, and visit http://&lt;your_app&gt;/admin'
  end
  lines.join("\n")
end