tp_common 0.1.2 → 0.1.3

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: b356520bfa9dafe1b9d9faf0085ab4036e1e5dca
4
- data.tar.gz: a015f6bd07598ffe465aa90e51730670e73ca2b5
3
+ metadata.gz: 4bf78bbec3e7eb66ca30db9547608273bf7ae424
4
+ data.tar.gz: be07e3981aa0085c53a7de8321b49b5700ec0feb
5
5
  SHA512:
6
- metadata.gz: 2bb409e4a85367d909016d000374132818be6ea00c9f18b5dfa9c2ce554ead08144ecdd9d8d7255a2775f28ffcdb3c86fca45a9a3701c2850afb67b05c6738d7
7
- data.tar.gz: c7dbe6e37ca7f854a9883f2550fa7c4b3b992b95820cb20e965f0728157e398b519386e3afa61cc7edc273be672b4682ce001d2335a2593d59b6aebb16477858
6
+ metadata.gz: e48f961396cc11f1cafc047ccf3db3a157f6f333965de9c6d23bb3042f50c95d67a93c1fbee165f5cff7734f9441025432f36e42dcde156e1cb7f78a08e03b53
7
+ data.tar.gz: 87f69132d8cd2d176117ce59eb9c97a0e9e3b703f2a0bb2930792a8c11cbf592b1ae0221a09040cb6eae5a89b988fdf287473f2928e0e14a7ff003dfd6aacd57
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
- # TpCommon
1
+ [![Build Status](https://travis-ci.org/anvox/tp_common.svg?branch=master)](https://travis-ci.org/anvox/tp_common)
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/tp_common`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ # TpCommon
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
5
+ This gem contains common libraries which shared between projects used in TINYpulse.
6
6
 
7
7
  ## Installation
8
8
 
@@ -22,7 +22,7 @@ Or install it yourself as:
22
22
 
23
23
  ## Usage
24
24
 
25
- TODO: Write usage instructions here
25
+ Reference http://www.rubydoc.info/gems/tp_common for more detail of each module.
26
26
 
27
27
  ## Development
28
28
 
@@ -1,4 +1,7 @@
1
1
  module TpCommon
2
+ # Hook libraries initialize to Rails if using in rails app.
3
+ # Use this way to wait Rails.application is initialized, so we could use rails application config.
4
+ #
2
5
  class Railtie < ::Rails::Railtie
3
6
  config.after_initialize do
4
7
  TpCommon::Timezones::Config.config
@@ -1,6 +1,8 @@
1
1
  module TpCommon
2
2
  module Timezones
3
3
  class Config
4
+ # Try to load TpCommon::Timezones::LIST_ZONES in config/timezones.yml from rails app.
5
+ # if not, load the default in timezones/config/timezones.yml
4
6
  def self.config
5
7
  return if TpCommon::Timezones.const_defined?("LIST_ZONES")
6
8
  if defined?(::Rails::Railtie)
@@ -17,6 +19,10 @@ module TpCommon
17
19
  TpCommon::Timezones.const_set("LIST_ZONES", load_default_timezones)
18
20
  end
19
21
 
22
+ # Private
23
+ # Load the default TpCommon::Timezones::LIST_ZONES in timezones/config/timezones.yml
24
+ # This method is clone from Rails' config_for
25
+ #
20
26
  def self.load_default_timezones
21
27
  file_path = File.join(File.dirname(__FILE__),"config/timezones.yml")
22
28
  yaml = Pathname.new(file_path)
@@ -1,13 +1,20 @@
1
1
  require 'tp_common/timezones/config'
2
2
 
3
3
  module TpCommon
4
+ # Helper methods for handle, display timezones.
5
+ # Use TP's custom mapping timezones instead of ActiveRecord or TzInfo
6
+ #
4
7
  module Timezones
5
8
  # LIST_ZONES = # Dynamic defined in TpCommon::Timezones::Config
6
9
 
10
+ # Get current date in specific timezone.
11
+ # timezone is LIST_ZONES[time_zone_key]
12
+ #
7
13
  def self.current_date_in_time_zone(time_zone_key)
8
- self.converted_time(Time.now, time_zone_key).strftime('%Y-%m-%d %H:%M:%S').to_date
14
+ self.converted_time(Time.now.utc, time_zone_key).strftime('%Y-%m-%d %H:%M:%S').to_date
9
15
  end
10
16
 
17
+ # Human readable offset: GMT+7, GMT-9,...
11
18
  def self.offset_in_words(zone_name)
12
19
  offset = Time.now.in_time_zone(zone_name).utc_offset
13
20
  in_words = 'GMT'
@@ -19,6 +26,10 @@ module TpCommon
19
26
  in_words
20
27
  end
21
28
 
29
+ # Zone title which display in select options.
30
+ # if this zone applied DST, display sun_emoji at tail
31
+ # Display as: title + offset_in_words [+ sun_emoji]
32
+ #
22
33
  def self.zone_title(zone)
23
34
  zone_value = LIST_ZONES[zone]
24
35
  return nil unless zone_value
@@ -28,6 +39,8 @@ module TpCommon
28
39
  [zone_value[:title], self.offset_in_words(zone_value[:name]), dst_icon].reject{|part| part.empty? }.join(" ")
29
40
  end
30
41
 
42
+ # The same as (see #zone_title) but doesn't display DST sun_emoji
43
+ #
31
44
  def self.zone_title_without_dst(zone)
32
45
  zone_value = LIST_ZONES[zone]
33
46
  return nil unless zone_value
@@ -35,20 +48,28 @@ module TpCommon
35
48
  [zone_value[:title], self.offset_in_words(zone_value[:name])].reject{|part| part.empty? }.join(" ")
36
49
  end
37
50
 
51
+ # LIST_ZONES which map to format [text_to_display, key_identity]
52
+ # For use in render select tag
53
+ #
38
54
  def self.list_for_select
39
55
  LIST_ZONES.map{ |k, v| [self.zone_title(k), k] }
40
56
  end
41
57
 
42
58
  # return hash offsets. Ex: {'GMT' => 0, ...}
59
+ #
43
60
  def self.list_zone_offsets
44
61
  Hash[LIST_ZONES.map{ |k, v| [k, Time.now.in_time_zone(v[:name]).utc_offset] }]
45
62
  end
46
63
 
64
+ # Convert back a time in zone (defined in LIST_ZONES) to utc time
65
+ #
47
66
  def self.local_to_utc(time, zone)
48
67
  zone_value = LIST_ZONES[zone]
49
68
  ActiveSupport::TimeZone.new(zone_value[:name]).local_to_utc(time)
50
69
  end
51
70
 
71
+ # Opposite of #local_to_utc It conver a time to time in zone defined in LIST_ZONES
72
+ #
52
73
  def self.converted_time(time, zone)
53
74
  if time
54
75
  zone_value = LIST_ZONES[zone]
@@ -61,11 +82,15 @@ module TpCommon
61
82
  end
62
83
  end
63
84
 
85
+ # Get zone name by time_zone_key
86
+ #
64
87
  def self.zone_abbreviation(time_zone_key)
65
88
  zone_value = LIST_ZONES[time_zone_key]
66
89
  zone_value[:name]
67
90
  end
68
91
 
92
+ # Return a DateTime object of param time in specific timezone decide by time_zone_key
93
+ #
69
94
  def self.relative_time_in_time_zone(time, time_zone_key)
70
95
  zone = zone_abbreviation(time_zone_key)
71
96
  DateTime.parse(time.strftime("%d %b %Y %H:%M:%S #{time.in_time_zone(zone).formatted_offset}"))
@@ -1,3 +1,3 @@
1
1
  module TpCommon
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
data/lib/tp_common.rb CHANGED
@@ -3,12 +3,13 @@ require 'active_support/all'
3
3
  require 'psych'
4
4
  require 'yaml'
5
5
 
6
- require 'pry-byebug'
7
-
8
6
  if defined?(::Rails::Railtie)
9
7
  require 'tp_common/railtie'
10
8
  end
11
9
 
10
+ # Main scope of libraries.
11
+ # Dont put anything here.
12
+ #
12
13
  module TpCommon
13
14
  end
14
15
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tp_common
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - An Vo