defoker 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,165 +1,160 @@
1
- # encoding: utf-8
2
- require 'date'
3
-
4
- module Defoker
5
- # DateBaseName provides methods that various kind of date-base formated name.
6
- # date base, month base, year base, and etc...
7
- class DateBaseName
8
- # Get date-base formated name.
9
- # if additional is empty, return date YYYYMMDD.
10
- # if additional is not empty, return date YYYYMMDD_additional.
11
- #
12
- # @example only date
13
- # DateBaseName.new.to_yyyymmdd(Date.new(2014,9,9)) #=> "20140909"
14
- # @example date with additional text
15
- # DateBaseName.new.to_yyyymmdd(Date.new(2014,9,9), additional: 'hoge') #=> "20140909_hoge"
16
- #
17
- # @param [Date,DateTime] date target date
18
- # @param [String] additional additional text.
19
- # If you want to get only date, you can omit this option.
20
- # @return [String] date-based formated name
21
- #
22
- def to_yyyymmdd(date, additional: '')
23
- DateBaseNameValidators.validate_date(date)
24
- v = date.strftime('%Y%m%d')
25
- return v if additional.empty?
26
- "#{v}_#{additional}"
27
- end
28
-
29
- # Get date-base formated name list.
30
- # if additional is empty, return date YYYYMMDD.
31
- # if additional is not empty, return date YYYYMMDD_additional.
32
- #
33
- # @example only date
34
- # DateBaseName.new.to_yyyymmdd_list(Date.new(2014,9,9), Date.new(2014,9,12))
35
- # => ["20140909","20140910","20140911","20140912"]
36
- # @example date with additional text
37
- # DateBaseName.new.to_yyyymmdd_list(Date.new(2014,9,9), count: Date.new(2014,9,12), additional: 'hoge')
38
- # => ["20140909_hoge", "20140910_hoge", "20140911_hoge", "20140912_hoge"]
39
- # @example range
40
- # DateBaseName.new.to_yyyymmdd_list(Date.new(2014,9,9)..Date.new(2014,9,12), additional: 'hoge')
41
- # => ["20140909_hoge", "20140910_hoge", "20140911_hoge", "20140912_hoge"]
42
- #
43
- # @param [Date,DateTime,Range] day date or from-count range
44
- # @param [Date] count count date.
45
- # @param [String] additional additional text.
46
- # If you want count get only date, you can omit this option.
47
- # @return [Array(String)] date-based formated names
48
- def to_yyyymmdd_list(day, count: nil, additional: '')
49
- DateBaseNameValidators.validate_date(day)
50
- count.times.with_object([]) do |i, memo|
51
- memo << to_yyyymmdd(day + i, additional: additional)
52
- end
53
- end
54
-
55
- # Get month-base formated name.
56
- # if additional is empty, return month YYYYMM.
57
- # if additional is not empty, return month YYYYMM_additional.
58
- #
59
- # @example only month
60
- # DateBaseName.new.to_yyyymm(Date.new(2014,9)) #=> "201409"
61
- # @example month with additional text
62
- # DateBaseName.new.to_yyyymm(Date.new(2014,9), additional: 'hoge') #=> "201409_hoge"
63
- #
64
- # @param [Date,DateTime] month target month
65
- # @param [String] additional additional text.
66
- # If you want to get only month, you can omit this option.
67
- # @return [String] month-based formated name
68
- #
69
- def to_yyyymm(month, additional: '')
70
- DateBaseNameValidators.validate_date(month)
71
- v = month.strftime('%Y%m')
72
- return v if additional.empty?
73
- "#{v}_#{additional}"
74
- end
75
-
76
- # Get month-base formated name list.
77
- # if additional is empty, return month YYYYMM.
78
- # if additional is not empty, return month YYYYMM_additional.
79
- #
80
- # @example only month
81
- # DateBaseName.new.to_yyyymm_list(Date.new(2014,10), count: 4))
82
- # => ["201410","201411","20141","201501"]
83
- # @example only month (omit count. default = 3)
84
- # DateBaseName.new.to_yyyymm_list(Date.new(2014,10)))
85
- # => ["201410","201411","20141"]
86
- # @example month with additional text
87
- # DateBaseName.new.to_yyyymm_list(Date.new(2014,9,9), count: 4, additional: 'hoge')
88
- # => ["201410_hoge","201411_hoge","20141_hoge","201501_hoge"]
89
- #
90
- # @param [Date,DateTime] month month
91
- # @param [Fixnum] count month count. default = 3.
92
- # @param [String] additional additional text.
93
- # If you want to get only month, you can omit this option.
94
- # @return [Array(String)] month-based formated names
95
- def to_yyyymm_list(month, count: 3, additional: '')
96
- DateBaseNameValidators.validate_date(month)
97
- count.times.with_object([]) do |i, memo|
98
- memo << to_yyyymm(month >> i, additional: additional)
99
- end
100
- end
101
-
102
- # Get year-base formated name.
103
- # if additional is empty, return year YYYY.
104
- # if additional is not empty, return year YYYY_additional.
105
- #
106
- # @example only year
107
- # DateBaseName.new.to_yyyy(Date.new(2014)) #=> "2014"
108
- # @example year with additional text
109
- # DateBaseName.new.to_yyyy(Date.new(2014), additional: 'hoge') #=> "2014_hoge"
110
- #
111
- # @param [Date,DateTime] year target year
112
- # @param [String] additional additional text.
113
- # If you want to get only year, you can omit this option.
114
- # @return [String] year-based formated name
115
- #
116
- def to_yyyy(year, additional: '')
117
- DateBaseNameValidators.validate_date(year)
118
- v = year.strftime('%Y')
119
- return v if additional.empty?
120
- "#{v}_#{additional}"
121
- end
122
-
123
- # Get year-base formated name list.
124
- # if additional is empty, return year YYYY.
125
- # if additional is not empty, return year YYYY_additional.
126
- #
127
- # @example only year
128
- # DateBaseName.new.to_yyyymm_list(Date.new(2014), count: 4))
129
- # => ["2014","2015","2016","2017"]
130
- # @example only year (omit count. default = 3)
131
- # DateBaseName.new.to_yyyymm_list(Date.new(2014)))
132
- # => ["2014","2015","2016"]
133
- # @example year with additional text
134
- # DateBaseName.new.to_yyyymm_list(Date.new(2014), count: 4, additional: 'hoge')
135
- # => ["2014_hoge","2015_hoge","2016_hoge","2017_hoge"]
136
- #
137
- # @param [Date,DateTime] year year
138
- # @param [Fixnum] count year count. default = 3.
139
- # @param [String] additional additional text.
140
- # If you want to get only year, you can omit this option.
141
- # @return [Array(String)] year-based formated names
142
- def to_yyyy_list(year, count: 3, additional: '')
143
- DateBaseNameValidators.validate_date(year)
144
- count.times.with_object([]) do |i, memo|
145
- memo << to_yyyy(year >> (i * 12), additional: additional)
146
- end
147
- end
148
- end
149
-
150
- # InvalidRange
151
- class InvalidRange < StandardError; end
152
-
153
- # Validators
154
- class DateBaseNameValidators
155
- def self.validate_date(date)
156
- return if [Date, DateTime].include?(date.class)
157
- fail ArgumentError, "invalid argument type #{date.class}"
158
- end
159
-
160
- def self.validate_date_range(date)
161
- return if [Date, DateTime, Range].include?(date.class)
162
- fail ArgumentError, "invalid argument type #{date.class}"
163
- end
164
- end
165
- end
1
+ # encoding: utf-8
2
+ require 'date'
3
+
4
+ module Defoker
5
+ # DateBaseName provides methods that various kind of date-base formated name.
6
+ # date base, month base, year base, and etc...
7
+ class DateBaseName
8
+ # Get date-base formated name.
9
+ # if additional is empty, return date YYYYMMDD.
10
+ # if additional is not empty, return date YYYYMMDD_additional.
11
+ #
12
+ # @example only date
13
+ # DateBaseName.new.to_yyyymmdd(Date.new(2014,9,9)) #=> "20140909"
14
+ # @example date with additional text
15
+ # DateBaseName.new.to_yyyymmdd(Date.new(2014,9,9), additional: 'hoge') #=> "20140909_hoge"
16
+ #
17
+ # @param [Date,DateTime] date target date
18
+ # @param [String] additional additional text.
19
+ # If you want to get only date, you can omit this option.
20
+ # @return [String] date-based formated name
21
+ #
22
+ def to_yyyymmdd(date, additional: '')
23
+ DateBaseNameValidators.validate_date(date)
24
+ v = date.strftime('%Y%m%d')
25
+ return v if additional.empty?
26
+ "#{v}_#{additional}"
27
+ end
28
+
29
+ # Get date-base formated name list.
30
+ # if additional is empty, return date YYYYMMDD.
31
+ # if additional is not empty, return date YYYYMMDD_additional.
32
+ #
33
+ # @example only date
34
+ # DateBaseName.new.to_yyyymmdd_list(Date.new(2014,9,9), Date.new(2014,9,12))
35
+ # => ["20140909","20140910","20140911","20140912"]
36
+ # @example date with additional text
37
+ # DateBaseName.new.to_yyyymmdd_list(Date.new(2014,9,9), count: Date.new(2014,9,12), additional: 'hoge')
38
+ # => ["20140909_hoge", "20140910_hoge", "20140911_hoge", "20140912_hoge"]
39
+ # @example range
40
+ # DateBaseName.new.to_yyyymmdd_list(Date.new(2014,9,9)..Date.new(2014,9,12), additional: 'hoge')
41
+ # => ["20140909_hoge", "20140910_hoge", "20140911_hoge", "20140912_hoge"]
42
+ #
43
+ # @param [Date,DateTime,Range] day date or from-count range
44
+ # @param [Date] count count date.
45
+ # @param [String] additional additional text.
46
+ # If you want count get only date, you can omit this option.
47
+ # @return [Array(String)] date-based formated names
48
+ def to_yyyymmdd_list(day, count: nil, additional: '')
49
+ DateBaseNameValidators.validate_date(day)
50
+ count.times.with_object([]) do |i, memo|
51
+ memo << to_yyyymmdd(day + i, additional: additional)
52
+ end
53
+ end
54
+
55
+ # Get month-base formated name.
56
+ # if additional is empty, return month YYYYMM.
57
+ # if additional is not empty, return month YYYYMM_additional.
58
+ #
59
+ # @example only month
60
+ # DateBaseName.new.to_yyyymm(Date.new(2014,9)) #=> "201409"
61
+ # @example month with additional text
62
+ # DateBaseName.new.to_yyyymm(Date.new(2014,9), additional: 'hoge') #=> "201409_hoge"
63
+ #
64
+ # @param [Date,DateTime] month target month
65
+ # @param [String] additional additional text.
66
+ # If you want to get only month, you can omit this option.
67
+ # @return [String] month-based formated name
68
+ #
69
+ def to_yyyymm(month, additional: '')
70
+ DateBaseNameValidators.validate_date(month)
71
+ v = month.strftime('%Y%m')
72
+ return v if additional.empty?
73
+ "#{v}_#{additional}"
74
+ end
75
+
76
+ # Get month-base formated name list.
77
+ # if additional is empty, return month YYYYMM.
78
+ # if additional is not empty, return month YYYYMM_additional.
79
+ #
80
+ # @example only month
81
+ # DateBaseName.new.to_yyyymm_list(Date.new(2014,10), count: 4))
82
+ # => ["201410","201411","20141","201501"]
83
+ # @example only month (omit count. default = 3)
84
+ # DateBaseName.new.to_yyyymm_list(Date.new(2014,10)))
85
+ # => ["201410","201411","20141"]
86
+ # @example month with additional text
87
+ # DateBaseName.new.to_yyyymm_list(Date.new(2014,9,9), count: 4, additional: 'hoge')
88
+ # => ["201410_hoge","201411_hoge","20141_hoge","201501_hoge"]
89
+ #
90
+ # @param [Date,DateTime] month month
91
+ # @param [Fixnum] count month count. default = 3.
92
+ # @param [String] additional additional text.
93
+ # If you want to get only month, you can omit this option.
94
+ # @return [Array(String)] month-based formated names
95
+ def to_yyyymm_list(month, count: 3, additional: '')
96
+ DateBaseNameValidators.validate_date(month)
97
+ count.times.with_object([]) do |i, memo|
98
+ memo << to_yyyymm(month >> i, additional: additional)
99
+ end
100
+ end
101
+
102
+ # Get year-base formated name.
103
+ # if additional is empty, return year YYYY.
104
+ # if additional is not empty, return year YYYY_additional.
105
+ #
106
+ # @example only year
107
+ # DateBaseName.new.to_yyyy(Date.new(2014)) #=> "2014"
108
+ # @example year with additional text
109
+ # DateBaseName.new.to_yyyy(Date.new(2014), additional: 'hoge') #=> "2014_hoge"
110
+ #
111
+ # @param [Date,DateTime] year target year
112
+ # @param [String] additional additional text.
113
+ # If you want to get only year, you can omit this option.
114
+ # @return [String] year-based formated name
115
+ #
116
+ def to_yyyy(year, additional: '')
117
+ DateBaseNameValidators.validate_date(year)
118
+ v = year.strftime('%Y')
119
+ return v if additional.empty?
120
+ "#{v}_#{additional}"
121
+ end
122
+
123
+ # Get year-base formated name list.
124
+ # if additional is empty, return year YYYY.
125
+ # if additional is not empty, return year YYYY_additional.
126
+ #
127
+ # @example only year
128
+ # DateBaseName.new.to_yyyymm_list(Date.new(2014), count: 4))
129
+ # => ["2014","2015","2016","2017"]
130
+ # @example only year (omit count. default = 3)
131
+ # DateBaseName.new.to_yyyymm_list(Date.new(2014)))
132
+ # => ["2014","2015","2016"]
133
+ # @example year with additional text
134
+ # DateBaseName.new.to_yyyymm_list(Date.new(2014), count: 4, additional: 'hoge')
135
+ # => ["2014_hoge","2015_hoge","2016_hoge","2017_hoge"]
136
+ #
137
+ # @param [Date,DateTime] year year
138
+ # @param [Fixnum] count year count. default = 3.
139
+ # @param [String] additional additional text.
140
+ # If you want to get only year, you can omit this option.
141
+ # @return [Array(String)] year-based formated names
142
+ def to_yyyy_list(year, count: 3, additional: '')
143
+ DateBaseNameValidators.validate_date(year)
144
+ count.times.with_object([]) do |i, memo|
145
+ memo << to_yyyy(year >> (i * 12), additional: additional)
146
+ end
147
+ end
148
+ end
149
+
150
+ # InvalidRange
151
+ class InvalidRange < StandardError; end
152
+
153
+ # Validators
154
+ class DateBaseNameValidators
155
+ def self.validate_date(date)
156
+ return if [Date, DateTime].include?(date.class)
157
+ fail ArgumentError, "invalid argument type #{date.class}"
158
+ end
159
+ end
160
+ end
@@ -1,3 +1,3 @@
1
- module Defoker
2
- VERSION = '0.0.3'
3
- end
1
+ module Defoker
2
+ VERSION = '0.0.4'
3
+ end
data/lib/defoker_core.rb CHANGED
@@ -1,12 +1,32 @@
1
1
  # encoding: utf-8
2
2
  require 'date_base_name'
3
3
  require 'date'
4
+ require 'defoker_dsl'
5
+ require 'defoker_dsl_model'
4
6
 
5
7
  module Defoker
6
8
  # rubocop disable LineLength
9
+ DEFOKERFILE_PATH = 'Defokerfile'
10
+ DEFOKERFILE_TEMPLATE = <<-EOS
11
+ # type is required.
12
+ # you can choose type form...
13
+ # [:today | :tomorrow | :yesterday | :this_month | :next_month | :previous_month | :this_year | :previous_year | :next_month]
14
+ # example
15
+ # type :this_month
16
+ type :today
17
+ # base is optional.
18
+ # example
19
+ # base 'ruby'
20
+ base ''
21
+ EOS
7
22
 
8
23
  # Defoker Core
9
24
  class Core
25
+ # Generate Defokerfile template
26
+ def init
27
+ File.open(DEFOKERFILE_PATH, 'w:utf-8') { |f|f.puts DEFOKERFILE_TEMPLATE }
28
+ end
29
+
10
30
  # Get today folder name
11
31
  #
12
32
  # @param [String] additional additional name
@@ -108,6 +128,31 @@ module Defoker
108
128
  year = Date.new(year[0..3].to_i)
109
129
  DateBaseName.new.to_yyyy_list(year, count: count, additional: additional)
110
130
  end
131
+
132
+ # Create folder by Defokerfile's rule.
133
+ #
134
+ # @param [String] additional additional name
135
+ # @return [String] folder name
136
+ def rule(additional: '')
137
+ dsl = read_dsl
138
+ base = dsl.defoker.base
139
+ adds = [base, additional].reject(&:empty?)
140
+ send(dsl.defoker.type, additional: adds.join('_'))
141
+ end
142
+
143
+ private
144
+
145
+ def read_dsl
146
+ dsl = Defoker::Dsl.new
147
+ unless File.exist?(DEFOKERFILE_PATH)
148
+ fail DslNotExistError, "#{DEFOKERFILE_PATH} not exist"
149
+ end
150
+ src = File.open(DEFOKERFILE_PATH, 'r:utf-8') { |f|f.read }
151
+ dsl.instance_eval(src)
152
+ dsl
153
+ end
111
154
  end
155
+
156
+ class DslNotExistError < StandardError; end
112
157
  end
113
158
  # rubocop enable LineLength
@@ -0,0 +1,22 @@
1
+ # encoding: utf-8
2
+ require 'defoker_dsl_model'
3
+
4
+ module Defoker
5
+ # Dsl
6
+ class Dsl
7
+ attr_accessor :defoker
8
+
9
+ # String Define
10
+ [:type, :base].each do |f|
11
+ define_method f do |value|
12
+ @defoker.send("#{f}=", value)
13
+ end
14
+ end
15
+
16
+ def initialize
17
+ @defoker = Defoker::DslModel.new
18
+ @defoker.type = ''
19
+ @defoker.base = ''
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,11 @@
1
+ # encoding: utf-8
2
+
3
+ module Defoker
4
+ # DslModel
5
+ class DslModel
6
+ # type command
7
+ attr_accessor :type
8
+ # base command
9
+ attr_accessor :base
10
+ end
11
+ end