Class: Masamune::DataPlan::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/masamune/data_plan/rule.rb

Constant Summary collapse

TERMINAL =
nil

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(engine, name, type, options = {}) ⇒ Rule

Returns a new instance of Rule.



36
37
38
39
40
41
# File 'lib/masamune/data_plan/rule.rb', line 36

def initialize(engine, name, type, options = {})
  @engine  = engine
  @name    = name
  @type    = type
  @options = options
end

Instance Attribute Details

#engineObject (readonly)

Returns the value of attribute engine.



34
35
36
# File 'lib/masamune/data_plan/rule.rb', line 34

def engine
  @engine
end

#nameObject (readonly)

Returns the value of attribute name.



34
35
36
# File 'lib/masamune/data_plan/rule.rb', line 34

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



34
35
36
# File 'lib/masamune/data_plan/rule.rb', line 34

def options
  @options
end

#typeObject (readonly)

Returns the value of attribute type.



34
35
36
# File 'lib/masamune/data_plan/rule.rb', line 34

def type
  @type
end

Instance Method Details

#==(other) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/masamune/data_plan/rule.rb', line 79

def ==(other)
  engine  == other.engine &&
  name    == other.name &&
  type    == other.type &&
  pattern == other.pattern &&
  options == other.options
end

#adjacent_matches(instance) {|instance| ... } ⇒ Object

Yields:

  • (instance)


222
223
224
225
226
227
228
229
230
231
# File 'lib/masamune/data_plan/rule.rb', line 222

def adjacent_matches(instance)
  return Set.new(to_enum(:adjacent_matches, instance)) unless block_given?
  (-window..-1).each do |i|
    yield instance.prev(i.abs)
  end
  yield instance
  (1..window).each do |i|
    yield instance.next(i)
  end
end

#bind_date_or_time(input = nil) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/masamune/data_plan/rule.rb', line 124

def bind_date_or_time(input = nil)
  input_time =
  case input
  when Time, DateTime
    input
  when Date
    input.to_time
  else
    raise ArgumentError, "Cannot bind_date_or_time with type #{input.class}"
  end
  output_time = tz.utc_to_local(input_time)
  Masamune::DataPlan::Elem.new(self, output_time, options_for_elem)
end

#bind_input(input) ⇒ Object



138
139
140
141
142
143
144
# File 'lib/masamune/data_plan/rule.rb', line 138

def bind_input(input)
  return input if input.is_a?(Masamune::DataPlan::Elem)
  matched_pattern = match_data_hash(matcher.match(input))
  raise "Cannot bind_input #{input} to #{pattern}" unless matched_pattern
  output_date = matched_date(matched_pattern)
  Masamune::DataPlan::Elem.new(self, output_date, options_for_elem.merge(matched_extra(matched_pattern)))
end

#bound?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/masamune/data_plan/rule.rb', line 115

def bound?
  !free?
end

#cache_depthObject



255
256
257
# File 'lib/masamune/data_plan/rule.rb', line 255

def cache_depth
  [max_cache_depth, step_cache_depth].min
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/masamune/data_plan/rule.rb', line 87

def eql?(other)
  self == other
end

#for_path?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/masamune/data_plan/rule.rb', line 55

def for_path?
  @options.key?(:path)
end

#for_sources?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/masamune/data_plan/rule.rb', line 51

def for_sources?
  @type == :source
end

#for_table?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/masamune/data_plan/rule.rb', line 59

def for_table?
  @options.key?(:table)
end

#for_table_with_partition?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/masamune/data_plan/rule.rb', line 63

def for_table_with_partition?
  @options.key?(:table) && @options.key?(:partition)
end

#for_targets?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/masamune/data_plan/rule.rb', line 47

def for_targets?
  @type == :target
end

#free?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/masamune/data_plan/rule.rb', line 111

def free?
  pattern.include?('%') || pattern.include?('*')
end

#generate(start_time, stop_time) ⇒ Object



150
151
152
153
154
155
156
157
158
159
# File 'lib/masamune/data_plan/rule.rb', line 150

def generate(start_time, stop_time)
  return Set.new(to_enum(:generate, start_time, stop_time)) unless block_given?
  instance = bind_date_or_time(start_time)

  loop do
    yield instance
    instance = instance.next
    break unless instance.start_time <= stop_time
  end
end

#generate_via_unify(elem, rule) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
# File 'lib/masamune/data_plan/rule.rb', line 161

def generate_via_unify(elem, rule)
  return Set.new(to_enum(:generate_via_unify, elem, rule)) unless block_given?
  instance = unify(elem, rule)

  stop_time = instance.start_time.advance(time_step => 1)
  loop do
    yield instance
    instance = instance.next
    break unless instance.start_time < stop_time
  end
end

#hashObject



91
92
93
# File 'lib/masamune/data_plan/rule.rb', line 91

def hash
  [engine, name, type, pattern, options].hash
end

#inspectObject



233
234
235
# File 'lib/masamune/data_plan/rule.rb', line 233

def inspect
  { type: type, pattern: pattern, options: options }.to_s
end

#matches?(input) ⇒ Boolean

Returns:

  • (Boolean)


119
120
121
122
# File 'lib/masamune/data_plan/rule.rb', line 119

def matches?(input)
  matched_pattern = match_data_hash(matcher.match(input))
  matched_pattern.present? && (matched_pattern[:rest].blank? || matched_pattern[:rest].include?('*'))
end

#partitionObject



75
76
77
# File 'lib/masamune/data_plan/rule.rb', line 75

def partition
  @options[:partition]
end

#pathObject



67
68
69
# File 'lib/masamune/data_plan/rule.rb', line 67

def path
  @options[:path]
end

#patternObject



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/masamune/data_plan/rule.rb', line 95

def pattern
  @pattern ||= begin
    if for_path?
      engine.filesystem.eval_path(path)
    elsif for_table_with_partition?
      [table, partition].join('_')
    elsif for_table?
      table
    end.to_s
  end
end

#prepareObject



43
44
45
# File 'lib/masamune/data_plan/rule.rb', line 43

def prepare
  pattern
end

#primary?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/masamune/data_plan/rule.rb', line 107

def primary?
  @options.fetch(:primary, true)
end

#round(grain) ⇒ Object



247
248
249
250
251
252
253
# File 'lib/masamune/data_plan/rule.rb', line 247

def round(grain)
  pattern_parts = pattern.split('/')
  part_index = pattern_parts.find_index { |part| part =~ time_step_to_format(grain) }
  raise "cannot round to :#{grain} for #{pattern}" unless part_index
  new_pattern = pattern_parts[0..part_index].join('/')
  self.class.new(engine, name, type, options.merge(path: new_pattern))
end

#strftime_formatObject



237
238
239
240
241
242
243
244
245
# File 'lib/masamune/data_plan/rule.rb', line 237

def strftime_format
  @strftime_format ||=
  pattern.dup.tap do |format|
    format.gsub!('%H-s', '%s')
    format.gsub!('%d-s', '%s')
    format.gsub!('%m-s', '%s')
    format.gsub!('%Y-s', '%s')
  end
end

#tableObject



71
72
73
# File 'lib/masamune/data_plan/rule.rb', line 71

def table
  @options[:table]
end

#time_round(time) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/masamune/data_plan/rule.rb', line 203

def time_round(time)
  case time_step
  when :hours
    DateTime.civil(time.year, time.month, time.day, time.hour)
  when :days
    DateTime.civil(time.year, time.month, time.day)
  when :months
    DateTime.civil(time.year, time.month)
  when :years
    DateTime.civil(time.year)
  else
    time
  end
end

#time_stepObject



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/masamune/data_plan/rule.rb', line 177

def time_step
  @time_step ||=
  case pattern
  when /%s/
    :hours
  when /%H-s/
    :hours
  when /%d-s/
    :days
  when /%m-s/
    :months
  when /%Y-s/
    :years
  when /%-?k/, /%-?H/
    :hours
  when /%-?d/
    :days
  when /%-?m/
    :months
  when /%-?Y/
    :years
  else
    :hours
  end
end

#tzObject



173
174
175
# File 'lib/masamune/data_plan/rule.rb', line 173

def tz
  ActiveSupport::TimeZone[@options.fetch(:tz, 'UTC')]
end

#unify(elem, rule) ⇒ Object



146
147
148
# File 'lib/masamune/data_plan/rule.rb', line 146

def unify(elem, rule)
  rule.bind_date_or_time(elem.start_time)
end

#windowObject



218
219
220
# File 'lib/masamune/data_plan/rule.rb', line 218

def window
  @options[:window] || 0
end