Class: Diecut::ContextHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/diecut/context-handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#context_classObject

Returns the value of attribute context_class.



4
5
6
# File 'lib/diecut/context-handler.rb', line 4

def context_class
  @context_class
end

#issue_handlerObject



6
7
8
# File 'lib/diecut/context-handler.rb', line 6

def issue_handler
  @issue_handler ||= Diecut.issue_handler
end

#pluginsObject

Returns the value of attribute plugins.



4
5
6
# File 'lib/diecut/context-handler.rb', line 4

def plugins
  @plugins
end

#ui_classObject

Returns the value of attribute ui_class.



4
5
6
# File 'lib/diecut/context-handler.rb', line 4

def ui_class
  @ui_class
end

Instance Method Details

#apply_option_to_ui(plugin, option) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/diecut/context-handler.rb', line 61

def apply_option_to_ui(plugin, option)
  ui_class.options_hash[option.name] = option

  if option.has_context_path?
     = context_class.walk_path(option.context_path).last.
    if .nil?
      issue_handler.missing_context_field(plugin.name, option.name, option.context_path)
      return
    end
    if option.has_default?
      ui_class.setting(option.name, option.default_value)
    elsif .is?(:defaulting)
      ui_class.setting(option.name, .default_value)
    else
      ui_class.setting(option.name)
    end
  else
    if option.has_default?
      ui_class.setting(option.name, option.default_value)
    else
      ui_class.setting(option.name)
    end
  end
end

#apply_simple_default(plugin, default) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/diecut/context-handler.rb', line 51

def apply_simple_default(plugin, default)
  target = context_class.walk_path(default.context_path).last
  if target..nil?
    issue_handler.unused_default(plugin.name, context_path)
  else
    target..default_value = default.value
    target..is(:defaulting)
  end
end

#apply_simple_defaultsObject



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/diecut/context-handler.rb', line 11

def apply_simple_defaults
  plugins.each do |plugin|
    plugin.context_defaults.each do |default|
      next unless default.simple?
      begin
        apply_simple_default(plugin, default)
      rescue Error
        raise Error, "Plugin #{plugin.name.inspect} failed"
      end
    end
  end
end

#apply_to_uiObject



24
25
26
27
28
29
30
# File 'lib/diecut/context-handler.rb', line 24

def apply_to_ui
  plugins.each do |plugin|
    plugin.options.each do |option|
      apply_option_to_ui(plugin, option)
    end
  end
end

#backfill_options_to_contextObject



32
33
34
35
36
37
38
# File 'lib/diecut/context-handler.rb', line 32

def backfill_options_to_context
  plugins.each do |plugin|
    plugin.options.each do |option|
      backfill_to_context(option)
    end
  end
end

#backfill_to_context(option) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/diecut/context-handler.rb', line 40

def backfill_to_context(option)
  return unless option.has_context_path?

  segment = context_class.walk_path(option.context_path).last
  if option.has_default?
    segment.klass.setting(segment.name, option.default_value)
  else
    segment.klass.setting(segment.name)
  end
end