click 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/bin/click-console CHANGED
@@ -10,11 +10,7 @@ module Click::Database
10
10
  connection_string = "#{ARGV.fetch(0, default_connection_string)}"
11
11
  puts "Using connection string: #{connection_string}"
12
12
 
13
- Click::Database.with_database(connection_string) do |db|
14
- clicker = Click::Clicker.new('click-console')
15
- require 'click/database/writer'
16
- clicker.add_observer(Click::Database::Writer.new(db))
17
-
13
+ Click.clicker_with_database('click-console', connection_string) do |clicker|
18
14
  puts
19
15
  puts 'A clicker was initialized to write to that database.'
20
16
  puts 'You can access it as `clicker`.'
data/demo/app.rb CHANGED
@@ -23,18 +23,12 @@ Thread.abort_on_exception = true
23
23
 
24
24
  Thread.new do
25
25
  begin
26
- clicker = nil
27
- Click::Database.with_database('sqlite:///tmp/click_demo_memory.sqlite') do |click_db|
28
- require 'click/database/writer'
29
- writer = Click::Database::Writer.new(click_db)
30
- clicker = Click::Clicker.new("Click demo app")
31
- clicker.add_observer(writer)
32
- end
33
-
34
- loop do
35
- clicker.click!
36
- puts 'Click!'
37
- sleep 10
26
+ Click.clicker_with_database('Click demo app', 'sqlite:///tmp/click_demo_memory.sqlite') do |clicker|
27
+ loop do
28
+ clicker.click!
29
+ puts 'Click!'
30
+ sleep 10
31
+ end
38
32
  end
39
33
  rescue => e
40
34
  puts "Exception in Click thread: #{e}"
@@ -56,6 +50,8 @@ end
56
50
 
57
51
  $garbage = []
58
52
  get '/make_objects/:count' do |count|
53
+ Visit.create(ip_address: request.ip, timestamp: Time.now)
54
+
59
55
  $garbage.concat(Array.new(count.to_i) { Object.new })
60
56
  "I made #{count} object(s) just for you :)"
61
57
  end
data/lib/click/clicker.rb CHANGED
@@ -48,4 +48,16 @@ module Click
48
48
  @observers ||= []
49
49
  end
50
50
  end
51
+
52
+ class << self
53
+ def clicker_with_database(session_name, connection_string)
54
+ Click::Database.with_database(connection_string) do |db|
55
+ require 'click/database/writer'
56
+ writer = Click::Database::Writer.new(db)
57
+ clicker = Click::Clicker.new(session_name)
58
+ clicker.add_observer(writer)
59
+ yield clicker
60
+ end
61
+ end
62
+ end
51
63
  end
@@ -11,6 +11,10 @@ module Click
11
11
  snapshot_dataset = Snapshot.by_session_name(session_name)
12
12
  from_self.qualify.join(snapshot_dataset, id: :snapshot_id)
13
13
  end
14
+
15
+ def snapshots
16
+ Snapshot.where(id: from_self.map(:snapshot_id).uniq)
17
+ end
14
18
  end
15
19
  end
16
20
  end
@@ -4,12 +4,16 @@ module Click
4
4
  module Database
5
5
  module Models
6
6
  class Session < Sequel::Model
7
- one_to_many :snapshot
7
+ one_to_many :snapshots
8
8
 
9
9
  dataset_module do
10
10
  def by_name(name)
11
11
  from_self.where(name: name)
12
12
  end
13
+
14
+ def snapshots
15
+ Snapshot.where(session_id: from_self.map(:id))
16
+ end
13
17
  end
14
18
  end
15
19
  end
@@ -4,6 +4,7 @@ module Click
4
4
  module Database
5
5
  module Models
6
6
  class Snapshot < Sequel::Model
7
+ many_to_one :session
7
8
  one_to_many :object_counts
8
9
 
9
10
  dataset_module do
@@ -19,6 +20,14 @@ module Click
19
20
  session_dataset = Session.where(name: session_name)
20
21
  from_self.qualify.join(session_dataset, id: :session_id)
21
22
  end
23
+
24
+ def sessions
25
+ Session.where(id: from_self.map(:session_id).uniq)
26
+ end
27
+
28
+ def object_counts
29
+ ObjectCount.where(snapshot_id: from_self.map(:id).uniq)
30
+ end
22
31
  end
23
32
  end
24
33
  end
data/lib/click/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Click
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -23,6 +23,22 @@ module Click::Database::Models
23
23
  end
24
24
  end
25
25
  end
26
+
27
+ describe '.sessions' do
28
+ it 'returns the sessions owning the object counts' do
29
+ with_in_memory_db do
30
+ ignored_session = Session.create(name: 'ignored', started_at: Time.now)
31
+ ignored_snapshot = Snapshot.create(timestamp: Time.now, session_id: ignored_session.id)
32
+ 50.times { ObjectCount.create(class_name: "Foo", count: 1, snapshot_id: ignored_snapshot.id) }
33
+
34
+ important_session = Session.create(name: 'important', started_at: Time.now)
35
+ important_snapshot = Snapshot.create(timestamp: Time.now, session_id: important_session.id)
36
+ important_object_count = ObjectCount.create(class_name: "Bar", count: 2, snapshot_id: important_snapshot.id)
37
+
38
+ expect(ObjectCount.where(class_name: 'Bar').snapshots.all).to eq([important_snapshot])
39
+ end
40
+ end
41
+ end
26
42
  end
27
43
  end
28
44
  end
@@ -18,6 +18,23 @@ module Click::Database::Models
18
18
  end
19
19
  end
20
20
  end
21
+
22
+ describe '.snapshots' do
23
+ it 'returns all the snapshots belonging to the set of sessions' do
24
+ with_in_memory_db do
25
+ ignored_session = Session.create(name: 'ignored', started_at: Time.now)
26
+ 50.times { ignored_session.add_snapshot(Snapshot.new(timestamp: Time.now))}
27
+
28
+ session = Session.create(name: 'important', started_at: Time.now)
29
+ snapshot = session.add_snapshot(Snapshot.new(timestamp: Time.now))
30
+
31
+ other_session = Session.create(name: 'important', started_at: Time.now)
32
+ other_snapshot = other_session.add_snapshot(Snapshot.new(timestamp: Time.now))
33
+
34
+ expect(Session.by_name('important').snapshots.map(:id)).to match_array([snapshot.id, other_snapshot.id])
35
+ end
36
+ end
37
+ end
21
38
  end
22
39
  end
23
40
  end
@@ -46,6 +46,35 @@ module Click::Database::Models
46
46
  end
47
47
  end
48
48
 
49
+ describe '.sessions' do
50
+ it 'returns sessions owning the current snapshots' do
51
+ with_in_memory_db do
52
+ Session.create(name: 'ignored', started_at: Time.now)
53
+
54
+ important_session = Session.create(name: 'important', started_at: Time.now)
55
+ Snapshot.create(timestamp: Time.now, session_id: important_session.id)
56
+
57
+ expect(Snapshot.dataset.sessions.all).to eq([important_session])
58
+ end
59
+ end
60
+ end
61
+
62
+ describe '.object_counts' do
63
+ it 'returns object counts owned by the current snapshots' do
64
+ with_in_memory_db do
65
+ Session.create(name: 'ignored', started_at: Time.now).
66
+ add_snapshot(Snapshot.new(timestamp: Time.now)).
67
+ add_object_count(ObjectCount.new(class_name: 'Bar', count: 1))
68
+
69
+ session = Session.create(name: 'important', started_at: Time.now)
70
+ snapshot = Snapshot.create(timestamp: Time.now, session_id: session.id)
71
+ object_count = snapshot.add_object_count(ObjectCount.new(class_name: 'Foo', count: 5))
72
+
73
+ expect(Snapshot.where(session_id: session.id).object_counts.all).to eq([object_count])
74
+ end
75
+ end
76
+ end
77
+
49
78
  it 'can combine scopes' do
50
79
  with_in_memory_db do
51
80
  described_class.create(timestamp: Date.new(2000), session_id: session_id)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: click
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-12-03 00:00:00.000000000 Z
12
+ date: 2013-12-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler