Class: Masamune::DataPlan::Rule
- Inherits:
-
Object
- Object
- Masamune::DataPlan::Rule
- Defined in:
- lib/masamune/data_plan/rule.rb
Constant Summary collapse
- TERMINAL =
nil
Instance Attribute Summary collapse
-
#engine ⇒ Object
readonly
Returns the value of attribute engine.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #adjacent_matches(instance) {|instance| ... } ⇒ Object
- #bind_date_or_time(input = nil) ⇒ Object
- #bind_input(input) ⇒ Object
- #bound? ⇒ Boolean
- #cache_depth ⇒ Object
- #eql?(other) ⇒ Boolean
- #for_path? ⇒ Boolean
- #for_sources? ⇒ Boolean
- #for_table? ⇒ Boolean
- #for_table_with_partition? ⇒ Boolean
- #for_targets? ⇒ Boolean
- #free? ⇒ Boolean
- #generate(start_time, stop_time) ⇒ Object
- #generate_via_unify(elem, rule) ⇒ Object
- #hash ⇒ Object
-
#initialize(engine, name, type, options = {}) ⇒ Rule
constructor
A new instance of Rule.
- #inspect ⇒ Object
- #matches?(input) ⇒ Boolean
- #partition ⇒ Object
- #path ⇒ Object
- #pattern ⇒ Object
- #prepare ⇒ Object
- #primary? ⇒ Boolean
- #round(grain) ⇒ Object
- #strftime_format ⇒ Object
- #table ⇒ Object
- #time_round(time) ⇒ Object
- #time_step ⇒ Object
- #tz ⇒ Object
- #unify(elem, rule) ⇒ Object
- #window ⇒ Object
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, = {}) @engine = engine @name = name @type = type = end |
Instance Attribute Details
#engine ⇒ Object (readonly)
Returns the value of attribute engine.
34 35 36 |
# File 'lib/masamune/data_plan/rule.rb', line 34 def engine @engine end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
34 35 36 |
# File 'lib/masamune/data_plan/rule.rb', line 34 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
34 35 36 |
# File 'lib/masamune/data_plan/rule.rb', line 34 def end |
#type ⇒ Object (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 && == other. end |
#adjacent_matches(instance) {|instance| ... } ⇒ Object
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, ) 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, .merge(matched_extra(matched_pattern))) end |
#bound? ⇒ Boolean
115 116 117 |
# File 'lib/masamune/data_plan/rule.rb', line 115 def bound? !free? end |
#cache_depth ⇒ Object
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
87 88 89 |
# File 'lib/masamune/data_plan/rule.rb', line 87 def eql?(other) self == other end |
#for_path? ⇒ Boolean
55 56 57 |
# File 'lib/masamune/data_plan/rule.rb', line 55 def for_path? .key?(:path) end |
#for_sources? ⇒ Boolean
51 52 53 |
# File 'lib/masamune/data_plan/rule.rb', line 51 def for_sources? @type == :source end |
#for_table? ⇒ Boolean
59 60 61 |
# File 'lib/masamune/data_plan/rule.rb', line 59 def for_table? .key?(:table) end |
#for_table_with_partition? ⇒ Boolean
63 64 65 |
# File 'lib/masamune/data_plan/rule.rb', line 63 def for_table_with_partition? .key?(:table) && .key?(:partition) end |
#for_targets? ⇒ Boolean
47 48 49 |
# File 'lib/masamune/data_plan/rule.rb', line 47 def for_targets? @type == :target end |
#free? ⇒ 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 |
#hash ⇒ Object
91 92 93 |
# File 'lib/masamune/data_plan/rule.rb', line 91 def hash [engine, name, type, pattern, ].hash end |
#inspect ⇒ Object
233 234 235 |
# File 'lib/masamune/data_plan/rule.rb', line 233 def inspect { type: type, pattern: pattern, options: }.to_s end |
#matches?(input) ⇒ 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 |
#partition ⇒ Object
75 76 77 |
# File 'lib/masamune/data_plan/rule.rb', line 75 def partition [:partition] end |
#path ⇒ Object
67 68 69 |
# File 'lib/masamune/data_plan/rule.rb', line 67 def path [:path] end |
#pattern ⇒ Object
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 |
#prepare ⇒ Object
43 44 45 |
# File 'lib/masamune/data_plan/rule.rb', line 43 def prepare pattern end |
#primary? ⇒ Boolean
107 108 109 |
# File 'lib/masamune/data_plan/rule.rb', line 107 def primary? .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, .merge(path: new_pattern)) end |
#strftime_format ⇒ Object
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 |
#table ⇒ Object
71 72 73 |
# File 'lib/masamune/data_plan/rule.rb', line 71 def table [: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_step ⇒ Object
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 |
#tz ⇒ Object
173 174 175 |
# File 'lib/masamune/data_plan/rule.rb', line 173 def tz ActiveSupport::TimeZone[.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 |
#window ⇒ Object
218 219 220 |
# File 'lib/masamune/data_plan/rule.rb', line 218 def window [:window] || 0 end |