formatted_times 0.0.3 → 0.0.4

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: 855574b5f77d303a2897e4f475b8c1b0bd417a76
4
- data.tar.gz: ec74ca6e910e527ed18722236c68803daa867ae1
3
+ metadata.gz: e2b2caa1b5c3262a52e5ffe41ba0115c0c9bab9d
4
+ data.tar.gz: 9d239141b1cb9e5b56aea7fe9cd68b445cb946cc
5
5
  SHA512:
6
- metadata.gz: 85bfe87382bc73fd0a6160cb19f149297375a5ab675a1b44825569c3f519155e4dd639cc746616dd8ebd283c0ef264f0559e9ca332d28ab547183b7b3921bd10
7
- data.tar.gz: 9910c68f46f07ca4030699e9ef1f361dc626b462e9110c37f43073d64af255b6e4efeb0247fcff98c9172097b206d47ef8c9775d5bb44a5ee76723ef97281ce8
6
+ metadata.gz: f0ab070c6449962d2605119154df3bc543a69de3c0d66e6839eb6546aff0d7ec5425fad6d83b906272e4ffe5a3117a56f264e19611285d3dfe82157031a0f5bd
7
+ data.tar.gz: 550d2956d66f7909d82d46baf97665f1cb98c85d98c44ad71588d5286822e11cfa7c85ea0af43b3e168a4ea78f3ef543074ab18796be8b6fd21479fbf9eb4b02
data/README.md CHANGED
@@ -20,7 +20,7 @@ Or install it yourself as:
20
20
 
21
21
  Once included in your rails application you can use any formatting method that is made out of joining the formatting arguments of strftimes by underscore.
22
22
 
23
- Any method name for formatting timestamps should start with 'frmt_' and then followed by the options to get time stamp. Following is the option's hash that is used to get strftime methods argument string :
23
+ Any method name for formatting timestamps should start with 'frmt_' and then followed by the options to get time stamp. Following is the option's hash that is used to get strftime methods argument string :
24
24
 
25
25
  {
26
26
 
@@ -106,18 +106,18 @@ Or install it yourself as:
106
106
  'TT' => '%T' # 24-hour time (%H:%M:%S)
107
107
  }
108
108
 
109
- Now if you need date, month and year out of timestamp then you can simply use :
110
-
109
+ If you need date, month and year out of timestamp then you can simply use :
110
+
111
111
  User.first.created_at.frmt_dd_mm_yy #=> "27 : 09 : 14"
112
112
 
113
- # Getting desizered seperator
113
+ # Getting desizered seperator
114
114
  User.first.created_at.frmt_dd_mm_yy '/' #=> "27/09/14"
115
115
 
116
- # Some simple usage:
116
+ # Some simple usage:
117
117
  2.1.2 :012 > User.first.created_at.frmt_dd_hh_yy ', '
118
118
  => "27, Sep, 14"
119
119
  2.1.2 :020 > User.first.created_at.frmt_dd_hh_yy_HH_MM_SS
120
- => "27 : Sep : 14 : 08 : 44"
120
+ => "27 : Sep : 14 : 08 : 44"
121
121
 
122
122
  # Multiple seperators
123
123
  2.1.2 :002 > User.first.created_at.frmt_dd_mm_yy(', ', true)
@@ -128,9 +128,17 @@ Or install it yourself as:
128
128
 
129
129
  Methods from formatted times accepts two arguments a string as seperator and boolean to specify weather given separator is to be used as multiple separator string or single separator string. In case of multiple separator strings the separator strings are devided into character arrays and interleaved into the strftime methods option string.
130
130
 
131
+ Now you can add custom formats with a hash where its key will be the name of the method to call and value will be strftime argument string. Please refer following example:
132
+ # Custom format definition
133
+ > FormattedTimes.define_formats({:short_date => '%d %h, %Y'})
134
+ # after execution of above statement you can use
135
+ > User.first.created_at.short_date
136
+ => "18 Nov, 2014"
137
+
138
+
131
139
  ## Contributing
132
140
 
133
- 1. Fork it ( http://github.com/raviture91/formatted_times/fork )
141
+ 1. Fork it ( http://github.com/ravi-ture/formatted_times/fork )
134
142
  2. Create your feature branch (`git checkout -b my-new-feature`)
135
143
  3. Commit your changes (`git commit -am 'Add some feature'`)
136
144
  4. Push to the branch (`git push origin my-new-feature`)
@@ -1,113 +1,16 @@
1
+ require "formatted_times/active_support_ext"
1
2
  require "formatted_times/version"
2
3
 
3
- module ActiveSupport
4
- class TimeWithZone
5
4
 
6
- FORMATTING_OPTIONS = {
7
-
8
- # Date related options
9
-
10
- 'YY' => '%Y',
11
- 'CC' => '%C',
12
- 'yy' => '%y',
13
- 'mm' => '%m',
14
- 'BB' => '%B',
15
- 'bb' => '%b',
16
- 'hh' => '%h',
17
- 'dd' => '%d',
18
- 'ee' => '%e',
19
- 'jj' => '%j',
20
-
21
- # Time related options
22
-
23
- 'HH' => '%H',
24
- 'kk' => '%k',
25
- 'II' => '%I',
26
- 'll' => '%l',
27
- 'PP' => '%P',
28
- 'pp' => '%p',
29
- 'MM' => '%M',
30
- 'SS' => '%S',
31
- 'LL' => '%L',
32
- 'NN' => '%N',
33
- '3N' => '%3N',
34
- '6N' => '%6N',
35
- '9N' => '%9N',
36
- '12N' => '%12N',
37
-
38
- # Time zone related Options
39
-
40
- 'zz' => '%z',
41
- '1z' => '%:z',
42
- '2z' => '%::z',
43
- '3z' => '%:::z',
44
- 'ZZ' => '%Z',
45
-
46
- # Weekday related options
47
-
48
- 'AA' => '%A',
49
- 'aa' => '%a',
50
- 'uu' => '%u',
51
- 'ww' => '%w',
52
- 'GG' => '%G',
53
- 'gg' => '%g',
54
- 'VV' => '%V',
55
- 'UU' => '%U',
56
- 'WW' => '%W',
57
-
58
- # Seconds related opions
59
-
60
- 'ss' => '%s',
61
- 'QQ' => '%Q',
62
-
63
- # Literal string related options
64
-
65
- 'nn' => '%n',
66
- 'tt' => '%t',
67
-
68
- # Combination Options
69
-
70
- 'cc' => '%c',
71
- 'DD' => '%D',
72
- 'FF' => '%F',
73
- 'vv' => '%v',
74
- 'xx' => '%x',
75
- 'XX' => '%X',
76
- 'rr' => '%r',
77
- 'RR' => '%R',
78
- 'TT' => '%T'
79
- }
80
-
81
-
82
- def method_missing(sym, *args, &block)
83
- method_name = sym.to_s
84
-
85
- if method_name.starts_with? 'frmt_'
86
- if args.length.in?([1, 2]) and args[0].is_a?(String)
87
- strf_time_string = get_strftime_string(method_name, args[0], args[1])
88
- else
89
- strf_time_string = get_strftime_string(method_name)
5
+ module FormattedTimes
6
+ def self.define_formats(formats)
7
+ return false unless formats.is_a? Hash
8
+ formats.each do |name, format|
9
+ ActiveSupport::TimeWithZone.instance_eval {
10
+ define_method name.to_sym do
11
+ strftime(format)
90
12
  end
91
- return self.strftime(strf_time_string)
92
- end
93
-
94
- wrap_with_time_zone time.__send__(sym, *args, &block)
95
- rescue NoMethodError => e
96
- raise e, e.message.sub(time.inspect, self.inspect), e.backtrace
13
+ }
97
14
  end
98
-
99
- def get_strftime_string(name, *args)
100
- separator = args[0] || ':'
101
- multiple_separator= args[1] || false
102
- options = name.split('_')
103
- options.shift
104
-
105
- invalid_options = options - FORMATTING_OPTIONS.keys
106
- raise ::ArgumentError, "Options #{invalid_options.join(', ')} are invalid." unless invalid_options.empty?
107
-
108
- strf_options = options.collect{ |option| FORMATTING_OPTIONS[option] }
109
- multiple_separator ? strf_options.zip(separator.chars).flatten.compact.join : strf_options.join(separator)
110
- end
111
-
112
15
  end
113
16
  end
@@ -0,0 +1,111 @@
1
+
2
+ module ActiveSupport
3
+ class TimeWithZone
4
+
5
+ FORMATTING_OPTIONS = {
6
+
7
+ # Date related options
8
+
9
+ 'YY' => '%Y',
10
+ 'CC' => '%C',
11
+ 'yy' => '%y',
12
+ 'mm' => '%m',
13
+ 'BB' => '%B',
14
+ 'bb' => '%b',
15
+ 'hh' => '%h',
16
+ 'dd' => '%d',
17
+ 'ee' => '%e',
18
+ 'jj' => '%j',
19
+
20
+ # Time related options
21
+
22
+ 'HH' => '%H',
23
+ 'kk' => '%k',
24
+ 'II' => '%I',
25
+ 'll' => '%l',
26
+ 'PP' => '%P',
27
+ 'pp' => '%p',
28
+ 'MM' => '%M',
29
+ 'SS' => '%S',
30
+ 'LL' => '%L',
31
+ 'NN' => '%N',
32
+ '3N' => '%3N',
33
+ '6N' => '%6N',
34
+ '9N' => '%9N',
35
+ '12N' => '%12N',
36
+
37
+ # Time zone related Options
38
+
39
+ 'zz' => '%z',
40
+ '1z' => '%:z',
41
+ '2z' => '%::z',
42
+ '3z' => '%:::z',
43
+ 'ZZ' => '%Z',
44
+
45
+ # Weekday related options
46
+
47
+ 'AA' => '%A',
48
+ 'aa' => '%a',
49
+ 'uu' => '%u',
50
+ 'ww' => '%w',
51
+ 'GG' => '%G',
52
+ 'gg' => '%g',
53
+ 'VV' => '%V',
54
+ 'UU' => '%U',
55
+ 'WW' => '%W',
56
+
57
+ # Seconds related opions
58
+
59
+ 'ss' => '%s',
60
+ 'QQ' => '%Q',
61
+
62
+ # Literal string related options
63
+
64
+ 'nn' => '%n',
65
+ 'tt' => '%t',
66
+
67
+ # Combination Options
68
+
69
+ 'cc' => '%c',
70
+ 'DD' => '%D',
71
+ 'FF' => '%F',
72
+ 'vv' => '%v',
73
+ 'xx' => '%x',
74
+ 'XX' => '%X',
75
+ 'rr' => '%r',
76
+ 'RR' => '%R',
77
+ 'TT' => '%T'
78
+ }
79
+
80
+ def method_missing(sym, *args, &block)
81
+ method_name = sym.to_s
82
+
83
+ if method_name.starts_with? 'frmt_'
84
+ if args.length.in?([1, 2]) and args[0].is_a?(String)
85
+ strf_time_string = get_strftime_string(method_name, args[0], args[1])
86
+ else
87
+ strf_time_string = get_strftime_string(method_name)
88
+ end
89
+ return self.strftime(strf_time_string)
90
+ end
91
+
92
+ wrap_with_time_zone time.__send__(sym, *args, &block)
93
+ rescue NoMethodError => e
94
+ raise e, e.message.sub(time.inspect, self.inspect), e.backtrace
95
+ end
96
+
97
+ def get_strftime_string(name, *args)
98
+ separator = args[0] || ':'
99
+ multiple_separator= args[1] || false
100
+ options = name.split('_')
101
+ options.shift
102
+
103
+ invalid_options = options - FORMATTING_OPTIONS.keys
104
+ raise ::ArgumentError, "Options #{invalid_options.join(', ')} are invalid." unless invalid_options.empty?
105
+
106
+ strf_options = options.collect{ |option| FORMATTING_OPTIONS[option] }
107
+ multiple_separator ? strf_options.zip(separator.chars).flatten.compact.join : strf_options.join(separator)
108
+ end
109
+
110
+ end
111
+ end
@@ -1,3 +1,3 @@
1
1
  module FormattedTimes
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: formatted_times
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ravi Ture
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-10 00:00:00.000000000 Z
11
+ date: 2015-03-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -69,6 +69,7 @@ files:
69
69
  - contributors.txt
70
70
  - formatted_times.gemspec
71
71
  - lib/formatted_times.rb
72
+ - lib/formatted_times/active_support_ext.rb
72
73
  - lib/formatted_times/version.rb
73
74
  homepage: ''
74
75
  licenses: