Class: Blocks::OptionsSet

Inherits:
HashWithIndifferentAccess
  • Object
show all
Defined in:
lib/blocks/utilities/options_set.rb

Direct Known Subclasses

BlockDefinition, HookDefinition

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ OptionsSet

Returns a new instance of OptionsSet.



9
10
11
12
13
14
15
# File 'lib/blocks/utilities/options_set.rb', line 9

def initialize(*args, &block)
  options = args.extract_options!
  self.name = args.first
  reset
  add_options options, &block
  super(&nil)
end

Instance Attribute Details

#default_optionsObject

Returns the value of attribute default_options.



7
8
9
# File 'lib/blocks/utilities/options_set.rb', line 7

def default_options
  @default_options
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/blocks/utilities/options_set.rb', line 3

def name
  @name
end

#runtime_optionsObject

Returns the value of attribute runtime_options.



5
6
7
# File 'lib/blocks/utilities/options_set.rb', line 5

def runtime_options
  @runtime_options
end

#standard_optionsObject

Returns the value of attribute standard_options.



6
7
8
# File 'lib/blocks/utilities/options_set.rb', line 6

def standard_options
  @standard_options
end

Instance Method Details

#add_options(*args, &block) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/blocks/utilities/options_set.rb', line 50

def add_options(*args, &block)
  options = args.extract_options!
  caller_id = args.first

  runtime, defaults, standard = if options.is_a?(OptionsSet)
    caller_id ||= options.name
    [options.runtime_options, options.default_options, options.standard_options]
  else
    if !options.is_a?(HashWithIndifferentAccess)
      options = options.with_indifferent_access
    end
    [options.delete(:runtime), options.delete(:defaults), options]
  end

  caller_id ||= self.name

  runtime_options.add_options caller_id, runtime
  standard_options.add_options caller_id, standard, &block
  default_options.add_options caller_id, defaults

  self
end

#current_render_strategy_and_itemObject



79
80
81
# File 'lib/blocks/utilities/options_set.rb', line 79

def current_render_strategy_and_item
  render_strategies_and_items.compact.first
end

#initialize_copy(original) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/blocks/utilities/options_set.rb', line 17

def initialize_copy(original)
  super
  control_fields = (
    RuntimeContext::CONTROL_VARIABLES.keys +
    RuntimeContext::CONTROL_VARIABLES.values
  ).flatten.compact
  self.runtime_options = original.runtime_options.clone.except(*control_fields)
  self.default_options = original.default_options.clone.except(*control_fields)
  self.standard_options = original.standard_options.clone.except(*control_fields)
end

#inspectObject



43
44
45
46
47
48
# File 'lib/blocks/utilities/options_set.rb', line 43

def inspect
  hash = standard_options.to_hash
  hash[:defaults] = default_options if default_options.present?
  hash[:runtime] = runtime_options if runtime_options.present?
  hash
end

#nested_under_indifferent_accessObject



91
92
93
# File 'lib/blocks/utilities/options_set.rb', line 91

def nested_under_indifferent_access
  self
end

#render_strategies_and_itemsObject



83
84
85
86
87
88
89
# File 'lib/blocks/utilities/options_set.rb', line 83

def render_strategies_and_items
  [
    runtime_options.render_strategy_and_item,
    standard_options.render_strategy_and_item,
    default_options.render_strategy_and_item
  ]
end

#resetObject



73
74
75
76
77
# File 'lib/blocks/utilities/options_set.rb', line 73

def reset
  self.runtime_options = HashWithRenderStrategy.new "#{name} Runtime Options"
  self.standard_options = HashWithRenderStrategy.new "#{name} Standard Options"
  self.default_options = HashWithRenderStrategy.new "#{name} Default Options"
end

#to_sObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/blocks/utilities/options_set.rb', line 28

def to_s
  description = []
  description << "Block Name: #{name}"
  description << "------------------------------"
  description << "Runtime Options:"
  description << runtime_options.to_s
  description << "------------------------------"
  description << "Standard Options:"
  description << standard_options.to_s
  description << "------------------------------"
  description << "Default Options:"
  description << default_options.to_s
  description.join("\n")
end