From 19c301696b4cfeee5d36f2305e956c8547de4333 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Tue, 21 Jan 2014 00:21:29 +0100 Subject: [PATCH] add Pry::Config::Behavior. a Module Pry::Config and Pry::Config::Default can use to share common behavior. before now ::Default was a subclass of Pry::Config but are class methods(such as Pry::Config.from_hash()) that do not make sense on Pry::Config::Default. --- lib/pry/config.rb | 57 ++------------------------------------ lib/pry/config/behavior.rb | 57 ++++++++++++++++++++++++++++++++++++++ lib/pry/config/default.rb | 4 ++- 3 files changed, 62 insertions(+), 56 deletions(-) create mode 100644 lib/pry/config/behavior.rb diff --git a/lib/pry/config.rb b/lib/pry/config.rb index 7aded635..f6308ed4 100644 --- a/lib/pry/config.rb +++ b/lib/pry/config.rb @@ -1,8 +1,9 @@ class Pry::Config require 'ostruct' + require 'pry/config/behavior' require 'pry/config/default' require 'pry/config/convenience' - ASSIGNMENT = "=".freeze + include Pry::Config::Behavior def self.shortcuts Convenience::SHORTCUTS @@ -14,56 +15,6 @@ class Pry::Config end end - def initialize(default = Pry.config) - @default = default - @lookup = {} - end - - def [](key) - @lookup[key.to_s] - end - - def []=(key, value) - @lookup[key.to_s] = value - end - - def method_missing(name, *args, &block) - key = name.to_s - if key[-1] == ASSIGNMENT - short_key = key[0..-2] - self[short_key] = args[0] - elsif @lookup.has_key?(key) - self[key] - elsif @default.respond_to?(name) - @default.public_send(name, *args, &block) - else - nil - end - end - - def merge!(other) - raise TypeError, "cannot coerce argument to Hash" unless other.respond_to?(:to_hash) - other = other.to_hash - keys, values = other.keys.map(&:to_s), other.values - @lookup.merge! Hash[keys.zip(values)] - end - - def respond_to?(name, boolean=false) - @lookup.has_key?(name.to_s) or @default.respond_to?(name) or super(name, boolean) - end - - def refresh - @lookup = {} - end - - def to_hash - @lookup - end - - def to_h - @lookup - end - # # FIXME # @param [Pry::Hooks] hooks @@ -77,8 +28,4 @@ class Pry::Config self["hooks"] = hooks end end - - def quiet? - quiet - end end diff --git a/lib/pry/config/behavior.rb b/lib/pry/config/behavior.rb new file mode 100644 index 00000000..36d45313 --- /dev/null +++ b/lib/pry/config/behavior.rb @@ -0,0 +1,57 @@ +module Pry::Config::Behavior + ASSIGNMENT = "=".freeze + + def initialize(default = Pry.config) + @default = default + @lookup = {} + end + + def [](key) + @lookup[key.to_s] + end + + def []=(key, value) + @lookup[key.to_s] = value + end + + def method_missing(name, *args, &block) + key = name.to_s + if key[-1] == ASSIGNMENT + short_key = key[0..-2] + self[short_key] = args[0] + elsif @lookup.has_key?(key) + self[key] + elsif @default.respond_to?(name) + @default.public_send(name, *args, &block) + else + nil + end + end + + def merge!(other) + raise TypeError, "cannot coerce argument to Hash" unless other.respond_to?(:to_hash) + other = other.to_hash + keys, values = other.keys.map(&:to_s), other.values + @lookup.merge! Hash[keys.zip(values)] + end + + def respond_to?(name, boolean=false) + @lookup.has_key?(name.to_s) or @default.respond_to?(name) or super(name, boolean) + end + + def refresh + @lookup = {} + end + + def to_hash + @lookup + end + + def to_h + @lookup + end + + def quiet? + quiet + end +end diff --git a/lib/pry/config/default.rb b/lib/pry/config/default.rb index 2cf94ffd..80691093 100644 --- a/lib/pry/config/default.rb +++ b/lib/pry/config/default.rb @@ -1,4 +1,6 @@ -class Pry::Config::Default < Pry::Config +class Pry::Config::Default + include Pry::Config::Behavior + default = { :input => proc { Readline }, :output => proc { $stdout },