ki 0.4.2 → 0.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -0
  3. data/Gemfile.lock +1 -1
  4. data/README.md +18 -2
  5. data/Rakefile +8 -0
  6. data/bin/ki +1 -1
  7. data/lib/ki/api_error.rb +15 -7
  8. data/lib/ki/base_request.rb +1 -1
  9. data/lib/ki/helpers.rb +10 -1
  10. data/lib/ki/ki_cli.rb +53 -32
  11. data/lib/ki/middleware.rb +14 -9
  12. data/lib/ki/modules/callbacks.rb +22 -20
  13. data/lib/ki/modules/format_of.rb +10 -6
  14. data/lib/ki/modules/model_helpers.rb +26 -24
  15. data/lib/ki/modules/public_file_helper.rb +12 -8
  16. data/lib/ki/modules/query_interface.rb +28 -26
  17. data/lib/ki/modules/restrictions.rb +34 -32
  18. data/lib/ki/modules/view_helper.rb +12 -8
  19. data/lib/ki/orm.rb +125 -2
  20. data/lib/ki/version.rb +1 -1
  21. data/spec/examples/base/.ruby-version +1 -1
  22. data/spec/examples/base/Gemfile +1 -2
  23. data/spec/examples/couch-lock/Gemfile +0 -1
  24. data/spec/examples/couch-lock/app.rb +52 -0
  25. data/spec/examples/couch-lock/public/doorbell-1.wav +0 -0
  26. data/spec/examples/couch-lock/public/fonts/glyphicons-halflings-regular.eot +0 -0
  27. data/spec/examples/couch-lock/public/fonts/glyphicons-halflings-regular.svg +229 -0
  28. data/spec/examples/couch-lock/public/fonts/glyphicons-halflings-regular.ttf +0 -0
  29. data/spec/examples/couch-lock/public/fonts/glyphicons-halflings-regular.woff +0 -0
  30. data/spec/examples/couch-lock/public/javascripts/angular.min.js +247 -0
  31. data/spec/examples/couch-lock/public/javascripts/clApp.coffee +68 -0
  32. data/spec/examples/couch-lock/public/stylesheets/bootstrap-theme.min.css +5 -0
  33. data/spec/examples/couch-lock/public/stylesheets/bootstrap.min.css +5 -0
  34. data/spec/examples/couch-lock/public/stylesheets/main.sass +29 -0
  35. data/spec/examples/couch-lock/views/index.haml +57 -57
  36. data/spec/examples/couch-lock/views/layout.haml +25 -0
  37. data/spec/examples/couch-lock/views/settings.haml +26 -0
  38. data/spec/examples/couch-lock/views/shop.haml +23 -0
  39. data/spec/examples/couch-lock/views/wiki.haml +6 -0
  40. data/spec/examples/json.northpole.ro/Gemfile.lock +1 -1
  41. data/spec/examples/todo/.ruby-version +1 -1
  42. data/spec/examples/todo/Gemfile +0 -1
  43. data/spec/lib/ki/model_spec.rb +1 -1
  44. data/spec/lib/ki/modules/format_of_spec.rb +2 -2
  45. metadata +29 -7
  46. data/spec/examples/couch-lock/Gemfile.lock +0 -48
  47. data/spec/examples/couch-lock/public/stylesheets/pure-min.css +0 -11
@@ -1,13 +1,17 @@
1
1
  module Ki
2
- module PublicFileHelper
3
- def public_file_exists? path
4
- path = path.path if path.class == BaseRequest
5
- File.file?(public_file_path(path))
6
- end
2
+ module Middleware
3
+ module Helpers
4
+ module PublicFile
5
+ def public_file_exists? path
6
+ path = path.path if path.class == BaseRequest
7
+ File.file?(public_file_path(path))
8
+ end
7
9
 
8
- def public_file_path path
9
- path = path.path if path.class == BaseRequest
10
- File.join(Ki::PUBLIC_PATH, path)
10
+ def public_file_path path
11
+ path = path.path if path.class == BaseRequest
12
+ File.join(Ki::PUBLIC_PATH, path)
13
+ end
14
+ end
11
15
  end
12
16
  end
13
17
  end
@@ -1,36 +1,38 @@
1
1
  module Ki
2
- # the query interface does not respect before/after filters,
3
- # unique attributes, required attributes or anything of the
4
- # sort.
5
- # it writes directly to the database
6
- module QueryInterface
7
- def count hash={}
8
- Orm::Db.instance.count class_name, hash
9
- end
2
+ class Model
3
+ # the query interface does not respect before/after filters,
4
+ # unique attributes, required attributes or anything of the
5
+ # sort.
6
+ # it writes directly to the database
7
+ module QueryInterface
8
+ def count hash={}
9
+ Orm::Db.instance.count class_name, hash
10
+ end
10
11
 
11
- def find hash={}
12
- Orm::Db.instance.find class_name, hash
13
- end
12
+ def find hash={}
13
+ Orm::Db.instance.find class_name, hash
14
+ end
14
15
 
15
- def create hash
16
- Orm::Db.instance.insert class_name, hash
17
- end
16
+ def create hash
17
+ Orm::Db.instance.insert class_name, hash
18
+ end
18
19
 
19
- def find_or_create hash
20
- r = find hash
21
- r.empty? ? create(hash) : r
22
- end
20
+ def find_or_create hash
21
+ r = find hash
22
+ r.empty? ? create(hash) : r
23
+ end
23
24
 
24
- def update hash
25
- Orm::Db.instance.update class_name, hash
26
- end
25
+ def update hash
26
+ Orm::Db.instance.update class_name, hash
27
+ end
27
28
 
28
- def delete hash
29
- Orm::Db.instance.delete class_name, hash
30
- end
29
+ def delete hash
30
+ Orm::Db.instance.delete class_name, hash
31
+ end
31
32
 
32
- def class_name
33
- self.to_s
33
+ def class_name
34
+ self.to_s
35
+ end
34
36
  end
35
37
  end
36
38
  end
@@ -1,47 +1,49 @@
1
1
  module Ki
2
- module Restrictions
3
- def forbidden_actions
4
- []
5
- end
2
+ class Model
3
+ module Restrictions
4
+ def forbidden_actions
5
+ []
6
+ end
7
+
8
+ def forbid *actions
9
+ send :define_method, :forbidden_actions do
10
+ actions
11
+ end
6
12
 
7
- def forbid *actions
8
- send :define_method, :forbidden_actions do
9
- actions
13
+ eigen_class = class << self; self; end
14
+ eigen_class.send(:define_method, :forbidden_actions) do
15
+ actions
16
+ end
10
17
  end
11
18
 
12
- eigen_class = class << self; self; end
13
- eigen_class.send(:define_method, :forbidden_actions) do
14
- actions
19
+ def required_attributes
20
+ []
15
21
  end
16
- end
17
22
 
18
- def required_attributes
19
- []
20
- end
23
+ def requires *attributes
24
+ send :define_method, :required_attributes do
25
+ attributes
26
+ end
21
27
 
22
- def requires *attributes
23
- send :define_method, :required_attributes do
24
- attributes
28
+ eigen_class = class << self; self; end
29
+ eigen_class.send(:define_method, :required_attributes) do
30
+ attributes
31
+ end
25
32
  end
26
33
 
27
- eigen_class = class << self; self; end
28
- eigen_class.send(:define_method, :required_attributes) do
29
- attributes
34
+ def unique_attributes
35
+ []
30
36
  end
31
- end
32
-
33
- def unique_attributes
34
- []
35
- end
36
37
 
37
- def unique *attributes
38
- send :define_method, :unique_attributes do
39
- attributes
40
- end
38
+ def unique *attributes
39
+ send :define_method, :unique_attributes do
40
+ attributes
41
+ end
41
42
 
42
- eigen_class = class << self; self; end
43
- eigen_class.send(:define_method, :unique_attributes) do
44
- attributes
43
+ eigen_class = class << self; self; end
44
+ eigen_class.send(:define_method, :unique_attributes) do
45
+ attributes
46
+ end
45
47
  end
46
48
  end
47
49
  end
@@ -1,13 +1,17 @@
1
1
  module Ki
2
- module ViewHelper
3
- def view_exists? path
4
- path = path.path if path.class == BaseRequest
5
- File.file?(view_path(path))
6
- end
2
+ module Middleware
3
+ module Helpers
4
+ module View
5
+ def view_exists? path
6
+ path = path.path if path.class == BaseRequest
7
+ File.file?(view_path(path))
8
+ end
7
9
 
8
- def view_path path
9
- path = path.path if path.class == BaseRequest
10
- File.join(Ki::VIEWS_PATH, path + ".haml")
10
+ def view_path path
11
+ path = path.path if path.class == BaseRequest
12
+ File.join(Ki::VIEWS_PATH, path + ".haml")
13
+ end
14
+ end
11
15
  end
12
16
  end
13
17
  end
@@ -1,37 +1,122 @@
1
1
  require 'singleton'
2
2
 
3
3
  module Ki
4
- module Orm
4
+ module Orm #:nodoc:
5
+ # This singleton class establishes the database connection and CRUD
6
+ # operations.
7
+ #
8
+ # == Connecting
9
+ #
10
+ # Db.instance.establish_connection
11
+ #
12
+ # == Queries
13
+ #
14
+ # db = Db.instance
15
+ # db.find 'users', {}
16
+ # db.find 'users', { name: 'Homer' }
17
+ #
5
18
  class Db
6
19
  include Singleton
7
20
 
8
21
  attr_reader :config, :connection, :db
9
22
 
23
+ # Creates the mongo connection.
24
+ #
25
+ # It first checks if the env variable "MONGODB_URI" is set. The required
26
+ # format: http://docs.mongodb.org/manual/reference/connection-string/
27
+ #
28
+ # If the env variable is not set, it will use the information stored in
29
+ # config.yml.
30
+ #
10
31
  def establish_connection
11
32
  @config = KiConfig.instance.database
12
33
  if ENV["MONGODB_URI"]
13
34
  @connection = Mongo::Connection.new
35
+ @db = @connection.db
14
36
  else
15
37
  @connection = Mongo::Connection.new(@config['host'], @config['port'])
38
+ @db = @connection.db(@config['name'])
16
39
  end
17
- @db = @connection.db(@config['name'])
18
40
  self
19
41
  end
20
42
 
43
+ # ==== Returns
44
+ #
45
+ # An array of all the collection names in the database.
46
+ #
21
47
  def collection_names
22
48
  @db.collection_names.delete_if{|name| name =~ /^system/}
23
49
  end
24
50
 
51
+ # Insert a hash in the database.
52
+ #
53
+ # ==== Attributes
54
+ #
55
+ # * +name+ - the mongo collection name
56
+ # * +hash+ - the hash which will be inserted
57
+ #
58
+ # ==== Returns
59
+ #
60
+ # The inserted hash.
61
+ #
62
+ # * +:conditions+ - An SQL fragment like "administrator = 1"
63
+ #
64
+ # ==== Examples
65
+ #
66
+ # db = Db.instance
67
+ # db.insert 'users', { name: 'Homer' }
68
+ #
25
69
  def insert name, hash
26
70
  @db[name].insert(hash)
27
71
  [hash].stringify_ids.first
28
72
  end
29
73
 
74
+ # Find a hash in the database
75
+ #
76
+ # ==== Attributes
77
+ #
78
+ # * +name+ - the mongo collection name
79
+ #
80
+ # ==== Options
81
+ #
82
+ # * +:hash+ - Filter by specific hash attributes
83
+ #
84
+ # ==== Returns
85
+ #
86
+ # An array of hashes which match the query criteria
87
+ #
88
+ # ==== Examples
89
+ #
90
+ # db = Db.instance
91
+ # db.find 'users'
92
+ # db.find 'users', { id: 'id' }
93
+ # db.find 'users', { name: 'Homer' }
94
+ #
30
95
  def find name, hash={}
31
96
  hash = nourish_hash_id hash
32
97
  @db[name].find(hash).to_a.stringify_ids
33
98
  end
34
99
 
100
+ # Update a hash from the database
101
+ #
102
+ # The method will update the hash obj with the id specified in the hash
103
+ # attribute.
104
+ #
105
+ # ==== Attributes
106
+ #
107
+ # * +name+ - the mongo collection name
108
+ # * +hash+ - requires at least the id of the hash which needs to be
109
+ # updated
110
+ #
111
+ # ==== Returns
112
+ #
113
+ # The updated hash
114
+ #
115
+ # ==== Examples
116
+ #
117
+ # db = Db.instance
118
+ # db.update('users', { id: 'id', name: 'Sir Homer' })
119
+ #
35
120
  def update name, hash
36
121
  hash = nourish_hash_id hash
37
122
  id = hash['_id'].to_s
@@ -41,12 +126,50 @@ module Ki
41
126
  hash
42
127
  end
43
128
 
129
+ # Deletes a hash from the database
130
+ #
131
+ # ==== Attributes
132
+ #
133
+ # * +name+ - the mongo collection name
134
+ # * +:hash+ - Filter by specific hash attributes. id required
135
+ #
136
+ # ==== Returns
137
+ #
138
+ # Empty hash
139
+ #
140
+ # ==== Examples
141
+ #
142
+ # db = Db.instance
143
+ # db.delete 'users', { id: 'id' }
144
+ # db.delete 'users', {}
145
+ #
44
146
  def delete name, hash
45
147
  hash = nourish_hash_id hash
46
148
  @db[name].remove hash
47
149
  {}
48
150
  end
49
151
 
152
+ # Count the number of hashes found in a specified collection, filtered
153
+ # by a hash attribute
154
+ #
155
+ # ==== Attributes
156
+ #
157
+ # * +name+ - the mongo collection name
158
+ #
159
+ # ==== Options
160
+ #
161
+ # * +hash+ - used for filtering
162
+ #
163
+ # ==== Returns
164
+ #
165
+ # The number of objects found.
166
+ #
167
+ # ==== Examples
168
+ #
169
+ # db = Db.instance
170
+ # db.count 'users'
171
+ # db.count 'users', { name: 'Homer' }
172
+ #
50
173
  def count name, hash={}
51
174
  @db[name].count hash
52
175
  end
@@ -1,3 +1,3 @@
1
1
  module Ki
2
- VERSION = "0.4.2"
2
+ VERSION = "0.4.3"
3
3
  end
@@ -1 +1 @@
1
- 2.1.0
1
+ 2.1.2
@@ -1,4 +1,3 @@
1
- source "https://rubygems.org"
1
+ source 'https://rubygems.org'
2
2
 
3
3
  gem 'ki'
4
- gem 'passenger'
@@ -1,4 +1,3 @@
1
1
  source "https://rubygems.org"
2
2
 
3
3
  gem 'ki', :path => '../../..'
4
- gem 'passenger'
@@ -8,6 +8,13 @@ end
8
8
 
9
9
  module Ki
10
10
  module Helpers
11
+ def wiki_defaults
12
+ base = Wiki.count base: "1"
13
+ if base == 0
14
+ Wiki.create({ title: "wiki", body: "", base: "1" })
15
+ end
16
+ end
17
+
11
18
  def display_status
12
19
  res = run "xrandr | head -n 1 | awk '{ print $8 }'"
13
20
  res.strip.to_i == 1920 ? 'cloned' : 'extended'
@@ -30,6 +37,43 @@ module Ki
30
37
  end
31
38
  end
32
39
 
40
+ class Wiki < Ki::Model
41
+ requires :title, :body
42
+ end
43
+
44
+ class Items < Ki::Model
45
+ requires :name, :qty
46
+ end
47
+
48
+ class Radio < Ki::Model
49
+ forbid :create, :update, :delete
50
+
51
+ def after_find
52
+ return if params["q"].nil?
53
+ return unless ['rockfm'].include? params["q"]
54
+
55
+ run "vlc http://80.86.106.35:800/"
56
+ end
57
+ end
58
+
59
+ class Volume < Ki::Model
60
+ forbid :create, :update, :delete
61
+
62
+ def after_find
63
+ return if params["q"].nil?
64
+ return unless ['up', 'down', 'manager'].include? params["q"]
65
+
66
+ case params["q"]
67
+ when 'up'
68
+ run "amixer -D pulse sset Master 5%+"
69
+ when 'down'
70
+ run "amixer -D pulse sset Master 5%-"
71
+ when 'manager'
72
+ run "gnome-control-center sound &"
73
+ end
74
+ end
75
+ end
76
+
33
77
  class Monitors < Ki::Model
34
78
  forbid :create, :update, :delete
35
79
 
@@ -41,6 +85,14 @@ class Monitors < Ki::Model
41
85
  end
42
86
  end
43
87
 
88
+ class Ring < Ki::Model
89
+ forbid :create, :update, :delete
90
+
91
+ def after_find
92
+ run "aplay public/doorbell-1.wav &"
93
+ end
94
+ end
95
+
44
96
  class Fireplace < Ki::Model
45
97
  forbid :create, :update, :delete
46
98