lib/blocks/utilities/options_set.rb in blocks-3.0.4 vs lib/blocks/utilities/options_set.rb in blocks-4.0.0.rc1

- old
+ new

@@ -1,95 +1,70 @@ +# frozen_string_literal: true + module Blocks - class OptionsSet < HashWithIndifferentAccess + class OptionsSet < HashWithRenderStrategy attr_accessor :name - - attr_accessor :runtime_options - attr_accessor :standard_options attr_accessor :default_options def initialize(*args, &block) - options = args.extract_options! + super self.name = args.first - reset - add_options options, &block - super(&nil) end - 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 - - 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 - + # def to_s + # description = [] + # description << "Block Name: #{name}" + # description << "------------------------------" + # description << "Standard Options:" + # description << standard_options.to_s + # description << "------------------------------" + # description << "Default Options:" + # description << default_options.to_s + # description.join("\n") + # end + # def inspect - hash = standard_options.to_hash - hash[:defaults] = default_options if default_options.present? - hash[:runtime] = runtime_options if runtime_options.present? + hash = to_hash + hash[:defaults] = default_options.to_hash if default_options.present? hash end - def add_options(*args, &block) + def reverse_merge!(*args, &block) options = args.extract_options! caller_id = args.first - runtime, defaults, standard = if options.is_a?(OptionsSet) + defaults, standard = if options.is_a?(OptionsSet) caller_id ||= options.name - [options.runtime_options, options.default_options, options.standard_options] + [options.default_options, options] else - if !options.is_a?(HashWithIndifferentAccess) - options = options.with_indifferent_access - end - [options.delete(:runtime), options.delete(:defaults), options] + [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 + if standard.present? || block + super caller_id, standard, &block + end + if defaults.present? + if !default_options + self.default_options = HashWithRenderStrategy.new "#{name} Default Options" + end + default_options.reverse_merge! caller_id, defaults + end + self end - 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" + def renders_with_proxy? + render_strategy == HashWithRenderStrategy::RENDER_WITH_PROXY end - def current_render_strategy_and_item - render_strategies_and_items.compact.first + def render_strategy + super || default_options.try(:render_strategy) end - def render_strategies_and_items - [ - runtime_options.render_strategy_and_item, - standard_options.render_strategy_and_item, - default_options.render_strategy_and_item - ] - end - - def nested_under_indifferent_access - self + def render_strategy_item + super || default_options.try(:render_strategy_item) end end end \ No newline at end of file