ki 0.4.2 → 0.4.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/Gemfile.lock +1 -1
- data/README.md +18 -2
- data/Rakefile +8 -0
- data/bin/ki +1 -1
- data/lib/ki/api_error.rb +15 -7
- data/lib/ki/base_request.rb +1 -1
- data/lib/ki/helpers.rb +10 -1
- data/lib/ki/ki_cli.rb +53 -32
- data/lib/ki/middleware.rb +14 -9
- data/lib/ki/modules/callbacks.rb +22 -20
- data/lib/ki/modules/format_of.rb +10 -6
- data/lib/ki/modules/model_helpers.rb +26 -24
- data/lib/ki/modules/public_file_helper.rb +12 -8
- data/lib/ki/modules/query_interface.rb +28 -26
- data/lib/ki/modules/restrictions.rb +34 -32
- data/lib/ki/modules/view_helper.rb +12 -8
- data/lib/ki/orm.rb +125 -2
- data/lib/ki/version.rb +1 -1
- data/spec/examples/base/.ruby-version +1 -1
- data/spec/examples/base/Gemfile +1 -2
- data/spec/examples/couch-lock/Gemfile +0 -1
- data/spec/examples/couch-lock/app.rb +52 -0
- data/spec/examples/couch-lock/public/doorbell-1.wav +0 -0
- data/spec/examples/couch-lock/public/fonts/glyphicons-halflings-regular.eot +0 -0
- data/spec/examples/couch-lock/public/fonts/glyphicons-halflings-regular.svg +229 -0
- data/spec/examples/couch-lock/public/fonts/glyphicons-halflings-regular.ttf +0 -0
- data/spec/examples/couch-lock/public/fonts/glyphicons-halflings-regular.woff +0 -0
- data/spec/examples/couch-lock/public/javascripts/angular.min.js +247 -0
- data/spec/examples/couch-lock/public/javascripts/clApp.coffee +68 -0
- data/spec/examples/couch-lock/public/stylesheets/bootstrap-theme.min.css +5 -0
- data/spec/examples/couch-lock/public/stylesheets/bootstrap.min.css +5 -0
- data/spec/examples/couch-lock/public/stylesheets/main.sass +29 -0
- data/spec/examples/couch-lock/views/index.haml +57 -57
- data/spec/examples/couch-lock/views/layout.haml +25 -0
- data/spec/examples/couch-lock/views/settings.haml +26 -0
- data/spec/examples/couch-lock/views/shop.haml +23 -0
- data/spec/examples/couch-lock/views/wiki.haml +6 -0
- data/spec/examples/json.northpole.ro/Gemfile.lock +1 -1
- data/spec/examples/todo/.ruby-version +1 -1
- data/spec/examples/todo/Gemfile +0 -1
- data/spec/lib/ki/model_spec.rb +1 -1
- data/spec/lib/ki/modules/format_of_spec.rb +2 -2
- metadata +29 -7
- data/spec/examples/couch-lock/Gemfile.lock +0 -48
- data/spec/examples/couch-lock/public/stylesheets/pure-min.css +0 -11
@@ -1,13 +1,17 @@
|
|
1
1
|
module Ki
|
2
|
-
module
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
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
|
-
|
9
|
-
|
10
|
-
|
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
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
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
|
-
|
12
|
-
|
13
|
-
|
12
|
+
def find hash={}
|
13
|
+
Orm::Db.instance.find class_name, hash
|
14
|
+
end
|
14
15
|
|
15
|
-
|
16
|
-
|
17
|
-
|
16
|
+
def create hash
|
17
|
+
Orm::Db.instance.insert class_name, hash
|
18
|
+
end
|
18
19
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
20
|
+
def find_or_create hash
|
21
|
+
r = find hash
|
22
|
+
r.empty? ? create(hash) : r
|
23
|
+
end
|
23
24
|
|
24
|
-
|
25
|
-
|
26
|
-
|
25
|
+
def update hash
|
26
|
+
Orm::Db.instance.update class_name, hash
|
27
|
+
end
|
27
28
|
|
28
|
-
|
29
|
-
|
30
|
-
|
29
|
+
def delete hash
|
30
|
+
Orm::Db.instance.delete class_name, hash
|
31
|
+
end
|
31
32
|
|
32
|
-
|
33
|
-
|
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
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
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
|
-
|
8
|
-
|
9
|
-
|
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
|
-
|
13
|
-
|
14
|
-
actions
|
19
|
+
def required_attributes
|
20
|
+
[]
|
15
21
|
end
|
16
|
-
end
|
17
22
|
|
18
|
-
|
19
|
-
|
20
|
-
|
23
|
+
def requires *attributes
|
24
|
+
send :define_method, :required_attributes do
|
25
|
+
attributes
|
26
|
+
end
|
21
27
|
|
22
|
-
|
23
|
-
|
24
|
-
|
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
|
-
|
28
|
-
|
29
|
-
attributes
|
34
|
+
def unique_attributes
|
35
|
+
[]
|
30
36
|
end
|
31
|
-
end
|
32
|
-
|
33
|
-
def unique_attributes
|
34
|
-
[]
|
35
|
-
end
|
36
37
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
38
|
+
def unique *attributes
|
39
|
+
send :define_method, :unique_attributes do
|
40
|
+
attributes
|
41
|
+
end
|
41
42
|
|
42
|
-
|
43
|
-
|
44
|
-
|
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
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
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
|
-
|
9
|
-
|
10
|
-
|
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
|
data/lib/ki/orm.rb
CHANGED
@@ -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
|
data/lib/ki/version.rb
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.1.
|
1
|
+
2.1.2
|
data/spec/examples/base/Gemfile
CHANGED
@@ -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
|
|