Commit d12319e2c6a269340291921004143d61f4c58448

Authored by Andrew Kane
1 parent 17c75ee8

Updated style

.rubocop.yml 0 → 100644
... ... @@ -0,0 +1,38 @@
  1 +Style/StringLiterals:
  2 + EnforcedStyle: double_quotes
  3 +
  4 +Style/SpaceInsideHashLiteralBraces:
  5 + EnforcedStyle: no_space
  6 +
  7 +Style/HashSyntax:
  8 + Enabled: false
  9 +
  10 +Style/Lambda:
  11 + Enabled: false
  12 +
  13 +Style/Documentation:
  14 + Enabled: false
  15 +
  16 +Style/SignalException:
  17 + Enabled: false
  18 +
  19 +Metrics/AbcSize:
  20 + Enabled: false
  21 +
  22 +Metrics/BlockNesting:
  23 + Enabled: false
  24 +
  25 +Metrics/ClassLength:
  26 + Enabled: false
  27 +
  28 +Metrics/CyclomaticComplexity:
  29 + Enabled: false
  30 +
  31 +Metrics/LineLength:
  32 + Enabled: false
  33 +
  34 +Metrics/MethodLength:
  35 + Enabled: false
  36 +
  37 +Metrics/PerceivedComplexity:
  38 + Enabled: false
... ...
Gemfile
1   -source 'https://rubygems.org'
  1 +source "https://rubygems.org"
2 2  
3 3 # Specify your gem's dependencies in groupdate.gemspec
4 4 gemspec
... ...
groupdate.gemspec
1 1 # coding: utf-8
2   -lib = File.expand_path('../lib', __FILE__)
  2 +lib = File.expand_path("../lib", __FILE__)
3 3 $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4   -require 'groupdate/version'
  4 +require "groupdate/version"
5 5  
6 6 Gem::Specification.new do |spec|
7 7 spec.name = "groupdate"
8 8 spec.version = Groupdate::VERSION
9 9 spec.authors = ["Andrew Kane"]
10 10 spec.email = ["acekane1@gmail.com"]
11   - spec.description = %q{The simplest way to group temporal data}
12   - spec.summary = %q{The simplest way to group temporal data}
  11 + spec.description = "The simplest way to group temporal data"
  12 + spec.summary = "The simplest way to group temporal data"
13 13 spec.homepage = "https://github.com/ankane/groupdate"
14 14 spec.license = "MIT"
15 15  
16   - spec.files = `git ls-files`.split($/)
  16 + spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
17 17 spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18 18 spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19 19 spec.require_paths = ["lib"]
... ...
lib/groupdate.rb
... ... @@ -5,7 +5,7 @@ require "groupdate/magic"
5 5  
6 6 module Groupdate
7 7 FIELDS = [:second, :minute, :hour, :day, :week, :month, :year, :day_of_week, :hour_of_day, :day_of_month, :month_of_year]
8   - METHODS = FIELDS.map{|v| :"group_by_#{v}" }
  8 + METHODS = FIELDS.map { |v| :"group_by_#{v}" }
9 9  
10 10 mattr_accessor :week_start, :day_start, :time_zone
11 11 self.week_start = :sun
... ...
lib/groupdate/active_record.rb
... ... @@ -7,8 +7,7 @@ ActiveRecord::Base.send(:extend, Groupdate::Scopes)
7 7  
8 8 module ActiveRecord
9 9 class Relation
10   -
11   - if ActiveRecord::VERSION::MAJOR == 3 and ActiveRecord::VERSION::MINOR < 2
  10 + if ActiveRecord::VERSION::MAJOR == 3 && ActiveRecord::VERSION::MINOR < 2
12 11  
13 12 def method_missing_with_hack(method, *args, &block)
14 13 if Groupdate::METHODS.include?(method)
... ... @@ -20,7 +19,6 @@ module ActiveRecord
20 19 alias_method_chain :method_missing, :hack
21 20  
22 21 end
23   -
24 22 end
25 23 end
26 24  
... ... @@ -38,7 +36,6 @@ end
38 36 # https://github.com/rails/rails/issues/7121
39 37 module ActiveRecord
40 38 module Calculations
41   -
42 39 private
43 40  
44 41 def column_alias_for_with_hack(*keys)
... ... @@ -49,6 +46,5 @@ module ActiveRecord
49 46 end
50 47 end
51 48 alias_method_chain :column_alias_for, :hack
52   -
53 49 end
54 50 end
... ...
lib/groupdate/enumerable.rb
1 1 module Enumerable
2   -
3 2 Groupdate::FIELDS.each do |field|
4 3 define_method :"group_by_#{field}" do |options = {}, &block|
5 4 if block
... ... @@ -9,5 +8,4 @@ module Enumerable
9 8 end
10 9 end
11 10 end
12   -
13 11 end
... ...
lib/groupdate/magic.rb
... ... @@ -8,17 +8,17 @@ module Groupdate
8 8 @field = field
9 9 @options = options
10 10  
11   - if !time_zone
  11 + unless time_zone
12 12 raise "Unrecognized time zone"
13 13 end
14 14  
15   - if field == :week and !week_start
  15 + if field == :week && !week_start
16 16 raise "Unrecognized :week_start option"
17 17 end
18 18 end
19 19  
20   - def group_by(enum, &block)
21   - group = enum.group_by{|v| v = yield(v); v ? round_time(v) : nil }
  20 + def group_by(enum, &_block)
  21 + group = enum.group_by { |v| v = yield(v); v ? round_time(v) : nil }
22 22 if options[:series] == false
23 23 group
24 24 else
... ... @@ -127,15 +127,15 @@ module Groupdate
127 127 cast_method =
128 128 case field
129 129 when :day_of_week, :hour_of_day, :day_of_month, :month_of_year
130   - lambda{|k| k.to_i }
  130 + lambda { |k| k.to_i }
131 131 else
132 132 utc = ActiveSupport::TimeZone["UTC"]
133   - lambda{|k| (k.is_a?(String) ? utc.parse(k) : k.to_time).in_time_zone(time_zone) }
  133 + lambda { |k| (k.is_a?(String) ? utc.parse(k) : k.to_time).in_time_zone(time_zone) }
134 134 end
135 135  
136 136 count =
137 137 begin
138   - Hash[ relation.send(method, *args, &block).map{|k, v| [multiple_groups ? k[0...@group_index] + [cast_method.call(k[@group_index])] + k[(@group_index + 1)..-1] : cast_method.call(k), v] } ]
  138 + Hash[relation.send(method, *args, &block).map { |k, v| [multiple_groups ? k[0...@group_index] + [cast_method.call(k[@group_index])] + k[(@group_index + 1)..-1] : cast_method.call(k), v] }]
139 139 rescue NoMethodError
140 140 raise "Be sure to install time zone support - https://github.com/ankane/groupdate#for-mysql"
141 141 end
... ... @@ -163,7 +163,7 @@ module Groupdate
163 163 def time_range
164 164 @time_range ||= begin
165 165 time_range = options[:range]
166   - if !time_range and options[:last]
  166 + if !time_range && options[:last]
167 167 step = 1.send(field) if 1.respond_to?(field)
168 168 if step
169 169 now = Time.now
... ... @@ -196,7 +196,7 @@ module Groupdate
196 196 # use first and last values
197 197 sorted_keys =
198 198 if multiple_groups
199   - count.keys.map{|k| k[@group_index] }.sort
  199 + count.keys.map { |k| k[@group_index] }.sort
200 200 else
201 201 count.keys.sort
202 202 end
... ... @@ -208,15 +208,15 @@ module Groupdate
208 208  
209 209 step = 1.send(field)
210 210  
211   - while (next_step = round_time(series.last + step)) and time_range.cover?(next_step)
  211 + while (next_step = round_time(series.last + step)) && time_range.cover?(next_step)
212 212 series << next_step
213 213 end
214 214  
215 215 if multiple_groups
216   - keys = count.keys.map{|k| k[0...@group_index] + k[(@group_index + 1)..-1] }.uniq
  216 + keys = count.keys.map { |k| k[0...@group_index] + k[(@group_index + 1)..-1] }.uniq
217 217 series = series.reverse if reverse
218 218 keys.flat_map do |k|
219   - series.map{|s| k[0...@group_index] + [s] + k[@group_index..-1] }
  219 + series.map { |s| k[0...@group_index] + [s] + k[@group_index..-1] }
220 220 end
221 221 else
222 222 series
... ... @@ -227,7 +227,7 @@ module Groupdate
227 227 end
228 228  
229 229 # reversed above if multiple groups
230   - if !multiple_groups and reverse
  230 + if !multiple_groups && reverse
231 231 series = series.to_a.reverse
232 232 end
233 233  
... ... @@ -253,7 +253,7 @@ module Groupdate
253 253 end
254 254 end
255 255 else
256   - lambda{|k| k }
  256 + lambda { |k| k }
257 257 end
258 258  
259 259 value = 0
... ... @@ -302,6 +302,5 @@ module Groupdate
302 302 def activerecord42?
303 303 ActiveRecord::VERSION::STRING.starts_with?("4.2.")
304 304 end
305   -
306 305 end
307 306 end
... ...
lib/groupdate/scopes.rb
1 1 module Groupdate
2 2 module Scopes
3   -
4 3 Groupdate::FIELDS.each do |field|
5 4 define_method :"group_by_#{field}" do |*args|
6 5 args = args.dup
... ... @@ -11,6 +10,5 @@ module Groupdate
11 10 Groupdate::Magic.new(field, options).relation(args[0], self)
12 11 end
13 12 end
14   -
15 13 end
16 14 end
... ...
lib/groupdate/series.rb
... ... @@ -22,6 +22,5 @@ module Groupdate
22 22 def respond_to?(method, include_all = false)
23 23 ActiveRecord::Calculations.method_defined?(method) || relation.respond_to?(method) || super
24 24 end
25   -
26 25 end
27 26 end
... ...
test/enumerable_test.rb
... ... @@ -19,7 +19,6 @@ class TestEnumerable &lt; Minitest::Test
19 19 end
20 20  
21 21 def call_method(method, field, options)
22   - Hash[ User.all.to_a.send(:"group_by_#{method}", options){|u| u.send(field) }.map{|k, v| [k, v.size] } ]
  22 + Hash[User.all.to_a.send(:"group_by_#{method}", options) { |u| u.send(field) }.map { |k, v| [k, v.size] }]
23 23 end
24   -
25 24 end
... ...
test/mysql_test.rb
... ... @@ -7,5 +7,4 @@ class TestMysql &lt; Minitest::Test
7 7 super
8 8 User.establish_connection :adapter => "mysql2", :database => "groupdate_test", :username => "root"
9 9 end
10   -
11 10 end
... ...
test/postgresql_test.rb
... ... @@ -7,5 +7,4 @@ class TestPostgresql &lt; Minitest::Test
7 7 super
8 8 User.establish_connection :adapter => "postgresql", :database => "groupdate_test"
9 9 end
10   -
11 10 end
... ...
test/test_helper.rb
... ... @@ -24,10 +24,8 @@ end
24 24  
25 25 # i18n
26 26 I18n.enforce_available_locales = true
27   -I18n.backend.store_translations :de, {
28   - :date => {
29   - :abbr_month_names => %w(Jan Feb Mar Apr Mai Jun Jul Aug Sep Okt Nov Dez).unshift(nil)
30   - }
  27 +I18n.backend.store_translations :de, :date => {
  28 + :abbr_month_names => %w(Jan Feb Mar Apr Mai Jun Jul Aug Sep Okt Nov Dez).unshift(nil)
31 29 }
32 30  
33 31 # migrations
... ... @@ -47,7 +45,6 @@ I18n.backend.store_translations :de, {
47 45 end
48 46  
49 47 module TestGroupdate
50   -
51 48 def setup
52 49 Groupdate.week_start = :sun
53 50 end
... ... @@ -55,7 +52,7 @@ module TestGroupdate
55 52 # second
56 53  
57 54 def test_second_end_of_second
58   - if ActiveRecord::Base.connection.adapter_name == "Mysql2" and ActiveRecord::VERSION::STRING.starts_with?("4.2.")
  55 + if ActiveRecord::Base.connection.adapter_name == "Mysql2" && ActiveRecord::VERSION::STRING.starts_with?("4.2.")
59 56 skip # no millisecond precision
60 57 else
61 58 assert_result_time :second, "2013-05-03 00:00:00 UTC", "2013-05-03 00:00:00.999"
... ... @@ -752,12 +749,10 @@ module TestGroupdate
752 749 # activerecord default_timezone option
753 750  
754 751 def test_default_timezone_local
755   - begin
756   - User.default_timezone = :local
757   - assert_raises(RuntimeError){ User.group_by_day(:created_at).count }
758   - ensure
759   - User.default_timezone = :utc
760   - end
  752 + User.default_timezone = :local
  753 + assert_raises(RuntimeError) { User.group_by_day(:created_at).count }
  754 + ensure
  755 + User.default_timezone = :utc
761 756 end
762 757  
763 758 # Brasilia Summer Time
... ... @@ -830,5 +825,4 @@ module TestGroupdate
830 825 def teardown
831 826 User.delete_all
832 827 end
833   -
834 828 end
... ...