class Peeek::Hook::Specifier

Attributes

method_name[R]

@attribute [r] method_name @return [Symbol] method name in the object

method_prefix[R]

@attribute [r] method_prefix @return [String] method prefix

object_name[R]

@attribute [r] object_name @return [String] object name

Public Class Methods

new(object_name, method_prefix, method_name) click to toggle source

Initialize the hook specifier.

@param [String] object_name object name @param [String] method_prefix method prefix @param [Symbol] method_name method name in the object

# File lib/peeek/hook/specifier.rb, line 36
def initialize(object_name, method_prefix, method_name)
  @object_name   = object_name
  @method_prefix = normalize_method_prefix(method_prefix)
  @method_name   = method_name
end
parse(string) click to toggle source

Parse a string as hook specifier.

@param [String] string string to parse as hook specifier @return [Peeek::Hook::Specifier] a hook specifier

# File lib/peeek/hook/specifier.rb, line 11
def self.parse(string)
  method_prefixes = METHOD_PREFIXES.sort_by(&:length).reverse.map do |method_prefix|
    index = string.rindex(method_prefix)
    priority = index ? [index + method_prefix.length, method_prefix.length] : nil
    [method_prefix, index, priority]
  end

  method_prefixes = method_prefixes.select(&:last)
  raise ArgumentError, "method name that is target of hook isn't specified in #{string.inspect}" if method_prefixes.empty?
  method_prefix, index = method_prefixes.max_by(&:last)
  method_prefix_range = index..(index + method_prefix.length - 1)
  string_range = 0..(string.length - 1)
  raise ArgumentError, "object name should not be empty for #{string.inspect}" unless string_range.begin < method_prefix_range.begin
  raise ArgumentError, "method name should not be empty for #{string.inspect}" unless method_prefix_range.end < string_range.end

  object_name = string[string_range.begin..(method_prefix_range.begin - 1)]
  method_name = string[(method_prefix_range.end + 1)..string_range.end].to_sym
  new(object_name, method_prefix, method_name)
end

Public Instance Methods

==(other) click to toggle source
# File lib/peeek/hook/specifier.rb, line 68
def ==(other)
  self.class     == other.class         &&
  @object_name   == other.object_name   &&
  @method_prefix == other.method_prefix &&
  @method_name   == other.method_name
end
Also aliased as: eql?
eql?(other)
Alias for: ==
hash() click to toggle source
# File lib/peeek/hook/specifier.rb, line 76
def hash
  values = [@object_name, @method_prefix, @method_name]
  values.inject(self.class.hash) { |hash, value| (hash << 32) + value.hash }
end
inspect() click to toggle source
# File lib/peeek/hook/specifier.rb, line 64
def inspect
  "#<#{self.class} #{self}>"
end
method_specifier() click to toggle source

@attribute [r] method_specifier @return [String] method specifier in the object

# File lib/peeek/hook/specifier.rb, line 56
def method_specifier
  @method_prefix + @method_name.to_s
end
to_s() click to toggle source
# File lib/peeek/hook/specifier.rb, line 60
def to_s
  @object_name + method_specifier
end

Private Instance Methods

normalize_method_prefix(method_prefix) click to toggle source
# File lib/peeek/hook/specifier.rb, line 83
def normalize_method_prefix(method_prefix)
  case method_prefix
  when *INSTANCE_METHOD_PREFIXES
    Instance::METHOD_PREFIX
  when *SINGLETON_METHOD_PREFIXES
    Singleton::METHOD_PREFIX
  else
    init = METHOD_PREFIXES.map(&:inspect)
    last = init.pop
    method_prefixes = [init * ', ', last] * ' or '
    raise ArgumentError, "invalid method prefix, #{method_prefixes} are valid"
  end
end