Class: Flycal::Mock::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/flycal/mock/config.rb

Overview

Resolves mock options from CLI and/or a JSON template under mocks/.

Constant Summary collapse

TEMPLATE_KEYS =
%w[
  mockCalendar
  mockEventDescriptionPatterns
  mockEventCount
  mockEventFrom
  mockEventTo
  mockEventDurationMin
  mockEventDurationMax
  mockEventHoursFrom
  mockEventHoursTo
  mockSeed
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Config

Returns a new instance of Config.

Raises:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/flycal/mock/config.rb', line 34

def initialize(options)
  merged = load_template(options[:mockTemplate])
  TEMPLATE_KEYS.each do |key|
    sym = key.to_sym
    value = options[sym]
    next if value.nil? || value.to_s.empty?

    merged[key] = value
  end

  @template_name = options[:mockTemplate].to_s.empty? ? nil : options[:mockTemplate].to_s
  calendar = merged["mockCalendar"].to_s
  calendar = options[:mockCalendar].to_s if calendar.empty?
  @calendar_name = calendar
  raise Flycal::Error,
        "Mock mode requires mockCalendar in the template (or --mockCalendar)." if @calendar_name.empty?

  @raw = merged
  @patterns = split_patterns(merged["mockEventDescriptionPatterns"])
  @count = Integer(merged["mockEventCount"])
  @range_from = parse_date(merged["mockEventFrom"], end_of_day: false)
  @range_to = parse_date(merged["mockEventTo"], end_of_day: true)
  @duration_min_seconds = DurationParser.to_seconds(merged["mockEventDurationMin"].to_s)
  @duration_max_seconds = DurationParser.to_seconds(merged["mockEventDurationMax"].to_s)
  @hours_from = parse_hour_minute(merged["mockEventHoursFrom"])
  @hours_to = parse_hour_minute(merged["mockEventHoursTo"])
  @seed = resolve_seed(merged["mockSeed"])

  validate!
end

Instance Attribute Details

#calendar_nameObject (readonly)

Returns the value of attribute calendar_name.



22
23
24
# File 'lib/flycal/mock/config.rb', line 22

def calendar_name
  @calendar_name
end

#countObject (readonly)

Returns the value of attribute count.



22
23
24
# File 'lib/flycal/mock/config.rb', line 22

def count
  @count
end

#duration_max_secondsObject (readonly)

Returns the value of attribute duration_max_seconds.



22
23
24
# File 'lib/flycal/mock/config.rb', line 22

def duration_max_seconds
  @duration_max_seconds
end

#duration_min_secondsObject (readonly)

Returns the value of attribute duration_min_seconds.



22
23
24
# File 'lib/flycal/mock/config.rb', line 22

def duration_min_seconds
  @duration_min_seconds
end

#hours_fromObject (readonly)

Returns the value of attribute hours_from.



22
23
24
# File 'lib/flycal/mock/config.rb', line 22

def hours_from
  @hours_from
end

#hours_toObject (readonly)

Returns the value of attribute hours_to.



22
23
24
# File 'lib/flycal/mock/config.rb', line 22

def hours_to
  @hours_to
end

#patternsObject (readonly)

Returns the value of attribute patterns.



22
23
24
# File 'lib/flycal/mock/config.rb', line 22

def patterns
  @patterns
end

#range_fromObject (readonly)

Returns the value of attribute range_from.



22
23
24
# File 'lib/flycal/mock/config.rb', line 22

def range_from
  @range_from
end

#range_toObject (readonly)

Returns the value of attribute range_to.



22
23
24
# File 'lib/flycal/mock/config.rb', line 22

def range_to
  @range_to
end

#rawObject (readonly)

Returns the value of attribute raw.



22
23
24
# File 'lib/flycal/mock/config.rb', line 22

def raw
  @raw
end

#seedObject (readonly)

Returns the value of attribute seed.



22
23
24
# File 'lib/flycal/mock/config.rb', line 22

def seed
  @seed
end

#template_nameObject (readonly)

Returns the value of attribute template_name.



22
23
24
# File 'lib/flycal/mock/config.rb', line 22

def template_name
  @template_name
end

Class Method Details

.enabled?(options) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/flycal/mock/config.rb', line 26

def self.enabled?(options)
  !options[:mockTemplate].to_s.empty? || !options[:mockCalendar].to_s.empty?
end

.from_options(options) ⇒ Object



30
31
32
# File 'lib/flycal/mock/config.rb', line 30

def self.from_options(options)
  new(options)
end

Instance Method Details

#to_hObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/flycal/mock/config.rb', line 65

def to_h
  {
    mock_calendar: calendar_name,
    mock_seed: seed,
    mock_template: template_name,
    mock_event_description_patterns: patterns.join(","),
    mock_event_count: count,
    mock_event_from: range_from,
    mock_event_to: range_to,
    mock_event_duration_min: duration_min_seconds,
    mock_event_duration_max: duration_max_seconds,
    mock_event_hours_from: hours_from,
    mock_event_hours_to: hours_to
  }
end