tricle 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1e1bf9319ba25a271eaae97c379e62f2f6a6b68e
4
- data.tar.gz: 4a515d52060d25dcae9a4f6c00c41786bf930b10
3
+ metadata.gz: 9ab31c91268e7d383dbc8090108459f7359d9d94
4
+ data.tar.gz: 2408d69e0b97e949ff16e572eece95f8ceca80c0
5
5
  SHA512:
6
- metadata.gz: bc6c048e313464575809c42d0dfd56857893be868834de9b8fc3c411c05dcfe813401cd60589ba4f9a65c0d7a7cf810486a5b542a2ece34857f102cec507d040
7
- data.tar.gz: 204151436a1410c2b7138dd1480f929e733a8260b4c6b1a97dc893ad6c3f3dd46f8a62ebbd7e15fa3743b608bab95c56c34e9566be0a858802205684229bdc98
6
+ metadata.gz: ef63f1335cb81ca70af935ae1e495b0a2f78c260f7ca7a02a232de3f059dc267b40505ff99d4f10d89fa704420154ae309f9f8ad253570321cc1e0caff5ee649
7
+ data.tar.gz: f2eadcb789c8caf60fa2a1446740646831898eea9f734856c03f1355844628ef46fefc3a10bd93c5e6669649a48111572571bd3a54bdb5df8e419b3999dc8145
data/Gemfile CHANGED
@@ -6,8 +6,8 @@ group :development, :test do
6
6
  gem 'appraisal'
7
7
  gem 'bundler'
8
8
  gem 'byebug', platforms: :mri_20
9
- gem 'guard', '~> 2.6'
10
9
  gem 'guard-rspec'
10
+ gem 'guard', '~> 2.6'
11
11
  gem 'rake'
12
12
  gem 'rspec', '~> 3.0'
13
13
  gem 'ruby_gntp'
data/README.md CHANGED
@@ -181,6 +181,23 @@ end
181
181
 
182
182
  The subject line will be based on the Mailer class name.
183
183
 
184
+ #### Passing options to a Metric
185
+
186
+ Sometimes, you'll want to initialize a Metric with specific options. If you pass a hash as a second argument to the mailer's `metric` method, the Metric will be initialized with an `@options` instance variable.
187
+
188
+ ```ruby
189
+ class IntelligenceBrief < Tricle::Mailer
190
+
191
+ metric NewUsers, matching_email: '@gmail.com'
192
+
193
+ # or for a list...
194
+ list NewUsers, matching_email: '@gmail.com' do |item|
195
+ ...
196
+ end
197
+
198
+ end
199
+ ```
200
+
184
201
  ### Previewing
185
202
 
186
203
  Since you'd probably like to preview your mailers before sending them, set up the `Tricle::MailPreview` Rack app (which uses [MailView](https://github.com/37signals/mail_view)).
@@ -9,8 +9,8 @@ group :development, :test do
9
9
  gem "appraisal"
10
10
  gem "bundler"
11
11
  gem "byebug", :platforms => :mri_20
12
- gem "guard", "~> 2.6"
13
12
  gem "guard-rspec"
13
+ gem "guard", "~> 2.6"
14
14
  gem "rake"
15
15
  gem "rspec", "~> 3.0"
16
16
  gem "ruby_gntp"
@@ -9,8 +9,8 @@ group :development, :test do
9
9
  gem "appraisal"
10
10
  gem "bundler"
11
11
  gem "byebug", :platforms => :mri_20
12
- gem "guard", "~> 2.6"
13
12
  gem "guard-rspec"
13
+ gem "guard", "~> 2.6"
14
14
  gem "rake"
15
15
  gem "rspec", "~> 3.0"
16
16
  gem "ruby_gntp"
@@ -4,15 +4,6 @@ module Tricle
4
4
  module Aggregation
5
5
  extend ActiveSupport::Concern
6
6
 
7
- included do
8
- attr_reader :now
9
-
10
- def initialize
11
- # TODO allow Time to be passed in so it can be frozen
12
- @now = Time.now
13
- end
14
- end
15
-
16
7
  def days_ago(n)
17
8
  start_at = self.now.beginning_of_day.ago(n.days)
18
9
  end_at = start_at + 1.day
@@ -33,9 +24,14 @@ module Tricle
33
24
  self.weeks_ago(1)
34
25
  end
35
26
 
36
- def weeks_average(past_num_weeks)
27
+ def weekly_values(past_num_weeks)
37
28
  weeks_range = 1..past_num_weeks
38
- total = weeks_range.reduce(0){|sum, n| sum + self.weeks_ago(n) }
29
+ weeks_range.map{|n| self.weeks_ago(n) }
30
+ end
31
+
32
+ def weeks_average(past_num_weeks)
33
+ values = self.weekly_values(past_num_weeks)
34
+ total = values.reduce(0, :+)
39
35
  total.to_f / past_num_weeks
40
36
  end
41
37
 
@@ -44,7 +44,7 @@ module Tricle
44
44
  when :higher
45
45
  (new_val >= old_val) ? 'good' : 'bad'
46
46
  when :lower
47
- (new_val >= old_val) ? 'bad' : 'good'
47
+ (new_val > old_val) ? 'bad' : 'good'
48
48
  else
49
49
  ''
50
50
  end
@@ -87,5 +87,11 @@ module Tricle
87
87
  end_at = start_at + 7.days
88
88
  list.items_markup(start_at, end_at).html_safe
89
89
  end
90
+
91
+ def sparkline(metric)
92
+ values = metric.weekly_values(13)
93
+ attachment_url = "https://sparklines.herokuapp.com/api/v1.png?values=#{values.join(',')}"
94
+ image_tag(attachment_url, alt: 'sparkline').html_safe
95
+ end
90
96
  end
91
97
  end
data/lib/tricle/mailer.rb CHANGED
@@ -53,12 +53,12 @@ module Tricle
53
53
  yield if block_given?
54
54
  end
55
55
 
56
- def metric(klass)
57
- self.report.add_metric(klass)
56
+ def metric(klass, opts = {})
57
+ self.report.add_metric(klass, opts)
58
58
  end
59
59
 
60
- def list(klass, &block)
61
- self.report.add_list(klass, &block)
60
+ def list(klass, opts = {}, &block)
61
+ self.report.add_list(klass, opts, &block)
62
62
  end
63
63
 
64
64
  def send_all
data/lib/tricle/metric.rb CHANGED
@@ -7,12 +7,19 @@ module Tricle
7
7
  class Metric
8
8
  include Aggregation
9
9
 
10
+ attr_reader :now, :options
11
+
12
+ def initialize(opts = {})
13
+ @now = opts[:now] || Time.now
14
+ @options = opts
15
+ end
16
+
10
17
  def better
11
- :higher
18
+ options[:better] || :higher
12
19
  end
13
20
 
14
21
  def title
15
- self.class.name.titleize
22
+ options[:title] || self.class.name.titleize
16
23
  end
17
24
 
18
25
  def size_for_range(start_at, end_at)
@@ -10,8 +10,8 @@ module Tricle
10
10
  @metrics = []
11
11
  end
12
12
 
13
- def add_metric(klass)
14
- self.metrics << klass.new
13
+ def add_metric(klass, opts = {})
14
+ self.metrics << klass.new(opts)
15
15
  end
16
16
  end
17
17
  end
@@ -4,13 +4,13 @@ module Tricle
4
4
  class List < Section
5
5
  attr_reader :block, :metric
6
6
 
7
- def initialize(klass, &block)
8
- @metric = klass.new
7
+ def initialize(klass, opts = {}, &block)
8
+ @metric = klass.new(opts)
9
9
  @block = block
10
10
  end
11
11
 
12
12
  def title
13
- self.metric.title
13
+ self.metric.options[:title] || self.metric.title
14
14
  end
15
15
 
16
16
  def items_markup(start_at, end_at)
@@ -21,18 +21,18 @@ module Tricle
21
21
  self.add_section(group)
22
22
  end
23
23
 
24
- def add_metric(klass)
24
+ def add_metric(klass, opts = {})
25
25
  last_section = self.sections.last
26
26
  unless last_section.is_a?(Tricle::Presenters::Group)
27
27
  last_section = self.add_group
28
28
  end
29
29
 
30
30
  # TODO don't assume they want to add this metric to the last group?
31
- last_section.add_metric(klass)
31
+ last_section.add_metric(klass, opts)
32
32
  end
33
33
 
34
- def add_list(klass, &block)
35
- list = Tricle::List.new(klass, &block)
34
+ def add_list(klass, opts = {}, &block)
35
+ list = Tricle::List.new(klass, opts, &block)
36
36
  self.add_section(list)
37
37
  end
38
38
  end
@@ -28,6 +28,10 @@ td {
28
28
  border: 1px solid #c7c7c7;
29
29
  }
30
30
 
31
+ .separator {
32
+ height: 10px;
33
+ }
34
+
31
35
  .list-item {
32
36
  text-align: left;
33
37
  }
@@ -29,7 +29,7 @@
29
29
  </tr>
30
30
  <% section.metrics.each do |metric| %>
31
31
  <tr>
32
- <th class="metric-title"><%= metric.title %></th>
32
+ <th class="metric-title" rowspan="2"><%= metric.title %></th>
33
33
  <td>
34
34
  <div><%= format_number(metric.last_week) %></div>
35
35
  <% if metric.total? %>
@@ -39,6 +39,12 @@
39
39
  <%= percent_change_cell(metric.last_week, metric.weeks_ago(2), metric.better) %>
40
40
  <%= percent_change_cell(metric.last_week, metric.week_average_this_quarter, metric.better) %>
41
41
  </tr>
42
+ <tr>
43
+ <td colspan="3">
44
+ <%= sparkline(metric) %>
45
+ </td>
46
+ </tr>
47
+ <tr class="separator"></tr>
42
48
  <% end %>
43
49
  <% end %>
44
50
  <% end %>
@@ -1,3 +1,3 @@
1
1
  module Tricle
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
data/screenshot.png CHANGED
Binary file
@@ -0,0 +1,10 @@
1
+ require_relative '../../lib/tricle/mailer'
2
+ require_relative 'test_metric'
3
+
4
+ class ListTestMailerWithOptions < Tricle::Mailer
5
+ default(to: 'recipient1@test.com', from: 'sender@test.com')
6
+
7
+ list TestMetric, title: 'TheNewTitle' do |val|
8
+ sprintf('%.1f', val)
9
+ end
10
+ end
@@ -5,8 +5,8 @@ require_relative '../../lib/tricle/range_data'
5
5
  class TestMetricWithNoTotal < Tricle::Metric
6
6
  attr_accessor :data_by_start_on
7
7
 
8
- def initialize
9
- super
8
+ def initialize(*args)
9
+ super(*args)
10
10
  self.load_data
11
11
  end
12
12
 
@@ -12,5 +12,11 @@ describe Tricle::Presenters::Group do
12
12
  metric = group.metrics.first
13
13
  expect(metric).to be_a(TestMetric)
14
14
  end
15
+
16
+ it "should pass options to the metric" do
17
+ group.add_metric(TestMetric, foo: 'bar')
18
+ metric = group.metrics.first
19
+ expect(metric.instance_variable_get(:@options)[:foo]).to eq 'bar'
20
+ end
15
21
  end
16
22
  end
@@ -2,6 +2,7 @@ require 'spec_helper'
2
2
  require_relative '../../lib/tricle/mailer'
3
3
  require_relative '../app/group_test_mailer'
4
4
  require_relative '../app/list_test_mailer'
5
+ require_relative '../app/list_test_mailer_with_options'
5
6
  require_relative '../app/no_total_test_mailer'
6
7
  require_relative '../app/test_mailer'
7
8
 
@@ -62,12 +63,17 @@ describe Tricle::Mailer do
62
63
  expect(markup).to include('62.0')
63
64
  expect(markup).not_to include('79.0')
64
65
  end
66
+
67
+ it "should pass options" do
68
+ deliver(ListTestMailerWithOptions)
69
+ expect(markup).to include("TheNewTitle")
70
+ end
65
71
  end
66
72
 
67
73
  describe '.send_all' do
68
74
  it "should .deliver all defined mailers" do
69
75
  Tricle::Mailer.send_all
70
- expect(ActionMailer::Base.deliveries.length).to eq(4)
76
+ expect(ActionMailer::Base.deliveries.length).to eq(5)
71
77
  end
72
78
  end
73
79
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tricle
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aidan Feldman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-02 00:00:00.000000000 Z
11
+ date: 2014-09-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionmailer
@@ -133,6 +133,7 @@ files:
133
133
  - screenshot.png
134
134
  - spec/app/group_test_mailer.rb
135
135
  - spec/app/list_test_mailer.rb
136
+ - spec/app/list_test_mailer_with_options.rb
136
137
  - spec/app/no_total_test_mailer.rb
137
138
  - spec/app/test_mailer.rb
138
139
  - spec/app/test_metric.rb
@@ -183,6 +184,7 @@ summary: A datastore-agnostic mailer where you can define custom metrics, where
183
184
  test_files:
184
185
  - spec/app/group_test_mailer.rb
185
186
  - spec/app/list_test_mailer.rb
187
+ - spec/app/list_test_mailer_with_options.rb
186
188
  - spec/app/no_total_test_mailer.rb
187
189
  - spec/app/test_mailer.rb
188
190
  - spec/app/test_metric.rb