class StrongPresenter::Inferrer
@private Helper class for inferring class names
Attributes
input[RW]
suffix[RW]
Public Class Methods
new(input)
click to toggle source
Constructs inferer object @param [String] input
Input name as base to infer from
# File lib/strong_presenter/inferrer.rb, line 10 def initialize(input) self.input = input end
Protected Class Methods
missing_name?(error, name)
click to toggle source
Detect if error is due to inferred class not existing, or some other error
# File lib/strong_presenter/inferrer.rb, line 46 def self.missing_name?(error, name) return true if error.is_a? UnextractableError missing_name = error.missing_name length = [missing_name.length, name.length].min missing_name[-length..-1] == name[-length..-1] end
Public Instance Methods
chomp(suffix)
click to toggle source
Sets input suffix @param [String] suffix
Suffix which must be present in input to chomp/remove
@return [self] chainable
# File lib/strong_presenter/inferrer.rb, line 18 def chomp(suffix) self.suffix = suffix self end
extract_name()
click to toggle source
Extracts name by removing suffix @return [String]
# File lib/strong_presenter/inferrer.rb, line 25 def extract_name raise UnextractableError if input.nil? || input.demodulize !~ /.+#{suffix}$/ input.chomp(suffix) end
inferred_class() { |name| ... }
click to toggle source
Retrieve inferred class if it exists. If not, nil is returned. @yield (optional) for further transforming name @yieldparam [String] name after suffix removed @yieldreturn [String] name after transformation @return [Class] inferred class
# File lib/strong_presenter/inferrer.rb, line 35 def inferred_class name = extract_name name = yield name if block_given? name.constantize rescue NameError => error raise unless Inferrer.missing_name?(error, name) nil end