defoker 0.0.5 → 0.0.6

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: 5ca77bb52cce6fe733a892bfc745ace70203d235
4
- data.tar.gz: 2f0c6249cd8437a0a81eef2c5a578afe751dd943
3
+ metadata.gz: 34c984f9722b99072fdbb1315c5d155e581d0deb
4
+ data.tar.gz: 988dd38c8690bd0381f215b5c795f006a395b7a4
5
5
  SHA512:
6
- metadata.gz: 18dc9f17bf261c2dced6af3dd92f7242306d7f835981e5ff19aaca54fcf53a83bab87bda9cda1d5d74ccfe35f674221a6faba1689e7a3af06164a114f4df253a
7
- data.tar.gz: a1d58f468978180c13769966e46607c1d535f3e3ef07aaf148288c3b739678975c459731e58e971ef73e0b3c2c3d7699afc1fcfccc766a3a0b9ccc0dfbec1b0c
6
+ metadata.gz: d67be9576e0ea9f93a1dde73fa5b85b85c7639760b49d4e507c5c6ea71a8c829c6b15e8d998adde2e495d218ca34c2c58e17093e5a1c58b5073117057dbb29ef
7
+ data.tar.gz: 0332c256d22c4f09b79a1579451a914b065d0b5ec21eab62d3f4dc01f0874c9d796bbcaf908ea7327b004f2787855285018b66e13816733fff20ab5305895851
data/README.md CHANGED
@@ -339,7 +339,42 @@ $ ls -F | grep /
339
339
  201410_monthly_report_hoge/
340
340
  ~~~
341
341
 
342
+ * edit Defokerfile with callback.(type = previous_month, base = monthly_report)
343
+
344
+ ~~~bash
345
+ $ cat Defokerfile
346
+ type :previous_month
347
+ base 'monthly_report'
348
+ callback ->(dir) {
349
+ p dir
350
+ File.open("#{dir}/template.txt", 'w:utf-8') { |e|e.puts "template for #{dir}" }
351
+ File.open("#{dir}/template.rb'" 'w:utf-8') { |e|e.puts "puts '#{dir}'" }
352
+ }
353
+
354
+ $ tree
355
+ # execute at 20141006
356
+ $ defoker rule -a hige
357
+ "20141006_hige"
358
+ # execute at 20141007
359
+ $ defoker rule -a hoge
360
+ "20141007_hoge"
361
+ # execute at 20141008
362
+ $ defoker rule -a hage
363
+ "20141008_hage"
364
+ $ tree
365
+ ┣ 20141006_hige
366
+ ┃ ┣ template.rb
367
+ ┃ ┗ template.txt
368
+ ┣ 20141007_hoge
369
+ ┃ ┣ template.rb
370
+ ┃ ┗ template.txt
371
+ ┗ 20141008_hage
372
+ ┣ template.rb
373
+ ┗ template.txt
374
+ ~~~
375
+
342
376
  ## History
377
+ * version 0.0.6 : Add callback in Defokerfile.
343
378
  * version 0.0.5 : Fix typo in Defokerfile Template.
344
379
  * version 0.0.4 : Add init/rule command for rule-output.
345
380
  * version 0.0.3 : Fix typo in gemspec.
data/bin/defoker CHANGED
@@ -6,6 +6,7 @@ require 'defoker/version'
6
6
  require 'thor'
7
7
  require 'fileutils'
8
8
 
9
+ # rubocop:disable ClassLength
9
10
  module Defoker
10
11
  # = Defoker CLI
11
12
  class CLI < Thor
@@ -14,7 +15,7 @@ module Defoker
14
15
 
15
16
  desc 'init', 'generate Defokerfile template'
16
17
  def init
17
- Defoker::Core.new.init
18
+ Defoker::Core.init
18
19
  exit(true)
19
20
  rescue
20
21
  exit(false)
@@ -98,7 +99,14 @@ module Defoker
98
99
  option :additional, aliases: 'a'
99
100
  desc 'rule', 'create folder by Defokerfile''s rule.'
100
101
  def rule
101
- defoker_core_single_caller(:rule)
102
+ additional = options[:additional] ? options[:additional] : ''
103
+ dir, callback = Defoker::Core.rule(additional: additional)
104
+ FileUtils.mkdir_p(dir)
105
+ callback[dir] unless callback.nil?
106
+ exit(true)
107
+ rescue => e
108
+ STDERR.puts(e.backtrace)
109
+ exit(false)
102
110
  end
103
111
 
104
112
  desc 'version', 'version'
@@ -109,23 +117,26 @@ module Defoker
109
117
  private
110
118
  def defoker_core_single_caller(method_name)
111
119
  additional = options[:additional] ? options[:additional] : ''
112
- dir = Defoker::Core.new.send(method_name, additional: additional)
120
+ dir = Defoker::Core.send(method_name, additional: additional)
113
121
  FileUtils.mkdir_p(dir)
114
122
  exit(true)
115
- rescue
123
+ rescue => e
124
+ STDERR.puts(e.backtrace)
116
125
  exit(false)
117
126
  end
118
127
 
119
128
  def defoker_core_multi_caller(date, method_name)
120
129
  count = options[:count] ? options[:count].to_i : 3
121
130
  additional = options[:additional] ? options[:additional] : ''
122
- dirs = Defoker::Core.new.send(method_name, date, count: count, additional: additional)
131
+ dirs = Defoker::Core.send(method_name, date, count: count, additional: additional)
123
132
  FileUtils.mkdir_p(dirs)
124
133
  exit(true)
125
- rescue
134
+ rescue => e
135
+ STDERR.puts(e.backtrace)
126
136
  exit(false)
127
137
  end
128
138
  end
129
139
  end
130
140
 
131
141
  Defoker::CLI.start(ARGV)
142
+ # rubocop:enable ClassLength
@@ -19,7 +19,7 @@ module Defoker
19
19
  # If you want to get only date, you can omit this option.
20
20
  # @return [String] date-based formated name
21
21
  #
22
- def to_yyyymmdd(date, additional: '')
22
+ def self.to_yyyymmdd(date, additional: '')
23
23
  DateBaseNameValidators.validate_date(date)
24
24
  v = date.strftime('%Y%m%d')
25
25
  return v if additional.empty?
@@ -45,7 +45,7 @@ module Defoker
45
45
  # @param [String] additional additional text.
46
46
  # If you want count get only date, you can omit this option.
47
47
  # @return [Array(String)] date-based formated names
48
- def to_yyyymmdd_list(day, count: nil, additional: '')
48
+ def self.to_yyyymmdd_list(day, count: nil, additional: '')
49
49
  DateBaseNameValidators.validate_date(day)
50
50
  count.times.with_object([]) do |i, memo|
51
51
  memo << to_yyyymmdd(day + i, additional: additional)
@@ -66,7 +66,7 @@ module Defoker
66
66
  # If you want to get only month, you can omit this option.
67
67
  # @return [String] month-based formated name
68
68
  #
69
- def to_yyyymm(month, additional: '')
69
+ def self.to_yyyymm(month, additional: '')
70
70
  DateBaseNameValidators.validate_date(month)
71
71
  v = month.strftime('%Y%m')
72
72
  return v if additional.empty?
@@ -92,7 +92,7 @@ module Defoker
92
92
  # @param [String] additional additional text.
93
93
  # If you want to get only month, you can omit this option.
94
94
  # @return [Array(String)] month-based formated names
95
- def to_yyyymm_list(month, count: 3, additional: '')
95
+ def self.to_yyyymm_list(month, count: 3, additional: '')
96
96
  DateBaseNameValidators.validate_date(month)
97
97
  count.times.with_object([]) do |i, memo|
98
98
  memo << to_yyyymm(month >> i, additional: additional)
@@ -113,7 +113,7 @@ module Defoker
113
113
  # If you want to get only year, you can omit this option.
114
114
  # @return [String] year-based formated name
115
115
  #
116
- def to_yyyy(year, additional: '')
116
+ def self.to_yyyy(year, additional: '')
117
117
  DateBaseNameValidators.validate_date(year)
118
118
  v = year.strftime('%Y')
119
119
  return v if additional.empty?
@@ -139,7 +139,7 @@ module Defoker
139
139
  # @param [String] additional additional text.
140
140
  # If you want to get only year, you can omit this option.
141
141
  # @return [Array(String)] year-based formated names
142
- def to_yyyy_list(year, count: 3, additional: '')
142
+ def self.to_yyyy_list(year, count: 3, additional: '')
143
143
  DateBaseNameValidators.validate_date(year)
144
144
  count.times.with_object([]) do |i, memo|
145
145
  memo << to_yyyy(year >> (i * 12), additional: additional)
@@ -1,3 +1,3 @@
1
1
  module Defoker
2
- VERSION = '0.0.5'
2
+ VERSION = '0.0.6'
3
3
  end
data/lib/defoker_core.rb CHANGED
@@ -14,16 +14,28 @@ module Defoker
14
14
  # example
15
15
  # type :this_month
16
16
  type :today
17
+
17
18
  # base is optional.
18
19
  # example
19
20
  # base 'ruby'
20
21
  base ''
22
+
23
+ # callback is ruby's callback(it will call after create folder).
24
+ # Block parameter is foldername string.
25
+ # If you want some action after create folder,
26
+ # comment out following lines and edit your logic.
27
+
28
+ # callback ->(dir) {
29
+ # p dir
30
+ # # create File in your dir
31
+ # File.open("\#{dir}/some_template_file.txt", "w:utf-8") { |e|e.puts 'some template'}
32
+ # }
21
33
  EOS
22
34
 
23
35
  # Defoker Core
24
36
  class Core
25
37
  # Generate Defokerfile template
26
- def init
38
+ def self.init
27
39
  File.open(DEFOKERFILE_PATH, 'w:utf-8') { |f|f.puts DEFOKERFILE_TEMPLATE }
28
40
  end
29
41
 
@@ -31,24 +43,24 @@ base ''
31
43
  #
32
44
  # @param [String] additional additional name
33
45
  # @return [String] today folder name
34
- def today(additional: '')
35
- DateBaseName.new.to_yyyymmdd(Date.today, additional: additional)
46
+ def self.today(additional: '')
47
+ DateBaseName.to_yyyymmdd(Date.today, additional: additional)
36
48
  end
37
49
 
38
50
  # Get tomorrow folder name
39
51
  #
40
52
  # @param [String] additional additional name
41
53
  # @return [String] tomorrow folder name
42
- def tomorrow(additional: '')
43
- DateBaseName.new.to_yyyymmdd(Date.today + 1, additional: additional)
54
+ def self.tomorrow(additional: '')
55
+ DateBaseName.to_yyyymmdd(Date.today + 1, additional: additional)
44
56
  end
45
57
 
46
58
  # Get yesterday folder name
47
59
  #
48
60
  # @param [String] additional additional name
49
61
  # @return [String] yesterday folder name
50
- def yesterday(additional: '')
51
- DateBaseName.new.to_yyyymmdd(Date.today - 1, additional: additional)
62
+ def self.yesterday(additional: '')
63
+ DateBaseName.to_yyyymmdd(Date.today - 1, additional: additional)
52
64
  end
53
65
 
54
66
  # Get days folder name list
@@ -56,33 +68,33 @@ base ''
56
68
  # @param [String] date yyyymmdd format string
57
69
  # @param [String] additional additional name
58
70
  # @return [String] yesterday folder name
59
- def days(date, count: 3, additional: '')
71
+ def self.days(date, count: 3, additional: '')
60
72
  date = Date.new(date[0..3].to_i, date[4..5].to_i, date[6..7].to_i)
61
- DateBaseName.new.to_yyyymmdd_list(date, count: count, additional: additional)
73
+ DateBaseName.to_yyyymmdd_list(date, count: count, additional: additional)
62
74
  end
63
75
 
64
76
  # Get this month folder name
65
77
  #
66
78
  # @param [String] additional additional name
67
79
  # @return [String] this month folder name
68
- def this_month(additional: '')
69
- DateBaseName.new.to_yyyymm(Date.today, additional: additional)
80
+ def self.this_month(additional: '')
81
+ DateBaseName.to_yyyymm(Date.today, additional: additional)
70
82
  end
71
83
 
72
84
  # Get next month folder name
73
85
  #
74
86
  # @param [String] additional additional name
75
87
  # @return [String] next month folder name
76
- def next_month(additional: '')
77
- DateBaseName.new.to_yyyymm(Date.today >> 1, additional: additional)
88
+ def self.next_month(additional: '')
89
+ DateBaseName.to_yyyymm(Date.today >> 1, additional: additional)
78
90
  end
79
91
 
80
92
  # Get previous month folder name
81
93
  #
82
94
  # @param [String] additional additional name
83
95
  # @return [String] previous month folder name
84
- def previous_month(additional: '')
85
- DateBaseName.new.to_yyyymm(Date.today << 1, additional: additional)
96
+ def self.previous_month(additional: '')
97
+ DateBaseName.to_yyyymm(Date.today << 1, additional: additional)
86
98
  end
87
99
 
88
100
  # Get months folder name list
@@ -90,33 +102,33 @@ base ''
90
102
  # @param [String] month yyyymmdd format string
91
103
  # @param [String] additional additional name
92
104
  # @return [String] yesterday folder name
93
- def months(month, count: 3, additional: '')
105
+ def self.months(month, count: 3, additional: '')
94
106
  month = Date.new(month[0..3].to_i, month[4..5].to_i)
95
- DateBaseName.new.to_yyyymm_list(month, count: count, additional: additional)
107
+ DateBaseName.to_yyyymm_list(month, count: count, additional: additional)
96
108
  end
97
109
 
98
110
  # Get this year folder name
99
111
  #
100
112
  # @param [String] additional additional name
101
113
  # @return [String] this year folder name
102
- def this_year(additional: '')
103
- DateBaseName.new.to_yyyy(Date.today, additional: additional)
114
+ def self.this_year(additional: '')
115
+ DateBaseName.to_yyyy(Date.today, additional: additional)
104
116
  end
105
117
 
106
118
  # Get next year folder name
107
119
  #
108
120
  # @param [String] additional additional name
109
121
  # @return [String] next year folder name
110
- def next_year(additional: '')
111
- DateBaseName.new.to_yyyy(Date.today >> 12, additional: additional)
122
+ def self.next_year(additional: '')
123
+ DateBaseName.to_yyyy(Date.today >> 12, additional: additional)
112
124
  end
113
125
 
114
126
  # Get previous year folder name
115
127
  #
116
128
  # @param [String] additional additional name
117
129
  # @return [String] previous year folder name
118
- def previous_year(additional: '')
119
- DateBaseName.new.to_yyyy(Date.today << 12, additional: additional)
130
+ def self.previous_year(additional: '')
131
+ DateBaseName.to_yyyy(Date.today << 12, additional: additional)
120
132
  end
121
133
 
122
134
  # Get years folder name list
@@ -124,25 +136,25 @@ base ''
124
136
  # @param [String] year yyyymmdd format string
125
137
  # @param [String] additional additional name
126
138
  # @return [String] yesterday folder name
127
- def years(year, count: 3, additional: '')
139
+ def self.years(year, count: 3, additional: '')
128
140
  year = Date.new(year[0..3].to_i)
129
- DateBaseName.new.to_yyyy_list(year, count: count, additional: additional)
141
+ DateBaseName.to_yyyy_list(year, count: count, additional: additional)
130
142
  end
131
143
 
132
144
  # Create folder by Defokerfile's rule.
133
145
  #
134
146
  # @param [String] additional additional name
135
147
  # @return [String] folder name
136
- def rule(additional: '')
148
+ def self.rule(additional: '')
137
149
  dsl = read_dsl
138
150
  base = dsl.defoker.base
151
+ callback = dsl.defoker.callback
139
152
  adds = [base, additional].reject(&:empty?)
140
- send(dsl.defoker.type, additional: adds.join('_'))
153
+ dir = send(dsl.defoker.type, additional: adds.join('_'))
154
+ [dir, callback]
141
155
  end
142
156
 
143
- private
144
-
145
- def read_dsl
157
+ def self.read_dsl
146
158
  dsl = Defoker::Dsl.new
147
159
  unless File.exist?(DEFOKERFILE_PATH)
148
160
  fail DslNotExistError, "#{DEFOKERFILE_PATH} not exist"
@@ -151,6 +163,7 @@ base ''
151
163
  dsl.instance_eval(src)
152
164
  dsl
153
165
  end
166
+ private_class_method :read_dsl
154
167
  end
155
168
 
156
169
  class DslNotExistError < StandardError; end
data/lib/defoker_dsl.rb CHANGED
@@ -7,7 +7,7 @@ module Defoker
7
7
  attr_accessor :defoker
8
8
 
9
9
  # String Define
10
- [:type, :base].each do |f|
10
+ [:type, :base, :callback].each do |f|
11
11
  define_method f do |value|
12
12
  @defoker.send("#{f}=", value)
13
13
  end
@@ -17,6 +17,7 @@ module Defoker
17
17
  @defoker = Defoker::DslModel.new
18
18
  @defoker.type = ''
19
19
  @defoker.base = ''
20
+ @defoker.callback = nil
20
21
  end
21
22
  end
22
23
  end
@@ -3,9 +3,11 @@
3
3
  module Defoker
4
4
  # DslModel
5
5
  class DslModel
6
- # type command
6
+ # type
7
7
  attr_accessor :type
8
- # base command
8
+ # base
9
9
  attr_accessor :base
10
+ # callback
11
+ attr_accessor :callback
10
12
  end
11
13
  end