Class: Blocks::HashWithRenderStrategy

Inherits:
Hash
  • Object
show all
Defined in:
lib/blocks/utilities/hash_with_render_strategy.rb

Direct Known Subclasses

HookDefinition, OptionsSet, RuntimeContext

Constant Summary collapse

RENDER_WITH_PROXY =
:with
RENDER_WITH_BLOCK =
:block
RENDER_WITH_PARTIAL =
:partial
RENDERING_STRATEGIES =
[RENDER_WITH_PROXY, RENDER_WITH_PARTIAL, RENDER_WITH_BLOCK]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ HashWithRenderStrategy

Returns a new instance of HashWithRenderStrategy.



13
14
15
16
17
# File 'lib/blocks/utilities/hash_with_render_strategy.rb', line 13

def initialize(*args, &block)
  super &nil
  options = args.extract_options!
  reverse_merge!(args.first, options, &block)
end

Instance Attribute Details

#render_strategyObject

Returns the value of attribute render_strategy.



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

def render_strategy
  @render_strategy
end

Instance Method Details

#except(*keys) ⇒ Object



27
28
29
# File 'lib/blocks/utilities/hash_with_render_strategy.rb', line 27

def except(*keys)
  slice(*self.keys - keys)
end

#extractable_options?Boolean

Returns true so that Array#extract_options! finds members of this class.

Returns:

  • (Boolean)


72
73
74
# File 'lib/blocks/utilities/hash_with_render_strategy.rb', line 72

def extractable_options?
  true
end

#nested_under_indifferent_accessObject



66
67
68
# File 'lib/blocks/utilities/hash_with_render_strategy.rb', line 66

def nested_under_indifferent_access
  self
end

#render_strategy_and_itemObject



54
55
56
# File 'lib/blocks/utilities/hash_with_render_strategy.rb', line 54

def render_strategy_and_item
  [render_strategy, self[render_strategy]] if render_strategy
end

#render_strategy_itemObject



58
59
60
# File 'lib/blocks/utilities/hash_with_render_strategy.rb', line 58

def render_strategy_item
  self[render_strategy] if render_strategy
end

#renders_with_proxy?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/blocks/utilities/hash_with_render_strategy.rb', line 62

def renders_with_proxy?
  render_strategy == RENDER_WITH_PROXY
end

#reverse_merge!(*args, &block) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/blocks/utilities/hash_with_render_strategy.rb', line 31

def reverse_merge!(*args, &block)
  options = args.extract_options!
  options[:block] = block if block
  if render_strategy.nil?
    self.render_strategy = if options.is_a?(HashWithRenderStrategy)
      options.render_strategy
    else
      RENDERING_STRATEGIES.detect {|render_strategy| options[render_strategy].present? }
    end
  end

  options.each do |key, value|
    current_value = self[key]

    if !self.key?(key)
      self[key] = value
    elsif current_value.is_a?(Hash) && value.is_a?(Hash)
      # TODO: handle attribute merges here
      self[key] = value.deep_merge(current_value)
    end
  end
end

#slice(*keys) ⇒ Object



23
24
25
# File 'lib/blocks/utilities/hash_with_render_strategy.rb', line 23

def slice(*keys)
  self.class.new(super)
end

#to_hashObject



19
20
21
# File 'lib/blocks/utilities/hash_with_render_strategy.rb', line 19

def to_hash
  {}.update(self)
end

#to_sObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/blocks/utilities/hash_with_render_strategy.rb', line 76

def to_s
  description = []

  description << "{"
  description << map do |key, value|
    value_display = case value
    when Symbol
      ":#{value}"
    when String
      "\"#{value}\""
    when Proc
      "Proc"
    else
      value
    end
    # "\"#{key}\" => #{value_display}, # [#{callers[key]}]"
    "\"#{key}\" => #{value_display}"
  end.join(",\n")
  description << "}"
  description.join("\n")
end