Class: Peeek::Hook::Specifier
- Inherits:
-
Object
- Object
- Peeek::Hook::Specifier
- Defined in:
- lib/peeek/hook/specifier.rb
Instance Attribute Summary collapse
-
#method_name ⇒ Symbol
readonly
Method name in the object.
-
#method_prefix ⇒ String
readonly
Method prefix.
-
#method_specifier ⇒ String
readonly
Method specifier in the object.
-
#object_name ⇒ String
readonly
Object name.
Class Method Summary collapse
-
.parse(string) ⇒ Peeek::Hook::Specifier
Parse a string as hook specifier.
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #hash ⇒ Object
-
#initialize(object_name, method_prefix, method_name) ⇒ Specifier
constructor
Initialize the hook specifier.
- #inspect ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(object_name, method_prefix, method_name) ⇒ Specifier
Initialize the hook specifier.
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_name ⇒ Symbol (readonly)
Returns method name in the object.
52 53 54 |
# File 'lib/peeek/hook/specifier.rb', line 52 def method_name @method_name end |
#method_prefix ⇒ String (readonly)
Returns method prefix.
48 49 50 |
# File 'lib/peeek/hook/specifier.rb', line 48 def method_prefix @method_prefix end |
#method_specifier ⇒ String (readonly)
Returns 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_name ⇒ String (readonly)
Returns 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.
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 |
#hash ⇒ Object
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 |
#inspect ⇒ Object
64 65 66 |
# File 'lib/peeek/hook/specifier.rb', line 64 def inspect "#<#{self.class} #{self}>" end |
#to_s ⇒ Object
60 61 62 |
# File 'lib/peeek/hook/specifier.rb', line 60 def to_s @object_name + method_specifier end |