Class: Peeek::Hook::Specifier

Inherits:
Object
  • Object
show all
Defined in:
lib/peeek/hook/specifier.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object_name, method_prefix, method_name) ⇒ Specifier

Initialize the hook specifier.

Parameters:

  • object_name (String)

    object name

  • method_prefix (String)

    method prefix

  • method_name (Symbol)

    method name in the object



36
37
38
39
40
# 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

Instance Attribute Details

#method_nameSymbol (readonly)

Returns method name in the object.

Returns:

  • (Symbol)

    method name in the object



52
53
54
# File 'lib/peeek/hook/specifier.rb', line 52

def method_name
  @method_name
end

#method_prefixString (readonly)

Returns method prefix.

Returns:

  • (String)

    method prefix



48
49
50
# File 'lib/peeek/hook/specifier.rb', line 48

def method_prefix
  @method_prefix
end

#method_specifierString (readonly)

Returns method specifier in the object.

Returns:

  • (String)

    method specifier in the object



56
57
58
# File 'lib/peeek/hook/specifier.rb', line 56

def method_specifier
  @method_prefix + @method_name.to_s
end

#object_nameString (readonly)

Returns object name.

Returns:

  • (String)

    object name



44
45
46
# File 'lib/peeek/hook/specifier.rb', line 44

def object_name
  @object_name
end

Class Method Details

.parse(string) ⇒ Peeek::Hook::Specifier

Parse a string as hook specifier.

Parameters:

  • string (String)

    string to parse as hook specifier

Returns:

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 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

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



68
69
70
71
72
73
# 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

#hashObject



76
77
78
79
# 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

#inspectObject



64
65
66
# File 'lib/peeek/hook/specifier.rb', line 64

def inspect
  "#<#{self.class} #{self}>"
end

#to_sObject



60
61
62
# File 'lib/peeek/hook/specifier.rb', line 60

def to_s
  @object_name + method_specifier
end