Class: Inflect::AbstractService

Inherits:
Object
  • Object
show all
Includes:
Comparable, Responsive, Singleton
Defined in:
lib/inflect/abstract_service.rb

Overview

Acts as an specification or standard required for a Service Class to be consumed by the application. A Service Class is just a wrapper for any possible service you'd like to give support to, just by having four prerequisites.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Responsive

#respond, #respond!

Constructor Details

#initializeAbstractService

Returns a new instance of AbstractService.


23
24
25
26
# File 'lib/inflect/abstract_service.rb', line 23

def initialize
  @priority = 0
  @words    = []
end

Instance Attribute Details

#priorityObject (readonly)

In case there are modules that provide similar contents the one with most priority is picked.


21
22
23
# File 'lib/inflect/abstract_service.rb', line 21

def priority
  @priority
end

#wordsObject (readonly)

A words Array constant with the key words of the Service.

Examples:

Array for New York Times service

words = %W[ NEWS TODAY NEW\ YORK\ TIMES]

17
18
19
# File 'lib/inflect/abstract_service.rb', line 17

def words
  @words
end

Instance Method Details

#<=>(other_service) ⇒ Object

Implement Comparable in order to be sortable.


29
30
31
# File 'lib/inflect/abstract_service.rb', line 29

def <=> (other_service)
  priority <=> other_service.priority
end

#handle(words) ⇒ Object

Returns a Hash with retrieved data.

Parameters:

  • words (Array)

    an Array of strings with key words.


45
46
47
48
49
# File 'lib/inflect/abstract_service.rb', line 45

def handle(words)
  message = "#{self.class} must implement handle method,
         for more information see AbstractService class."
  raise NoMethodError.new message
end

#valid?(words) ⇒ Boolean

Receives an Array of words and returns true or false depending if the Service can handle the request given by the words.

Parameters:

  • words (Array)

    an Array of strings with key words.

Returns:

  • (Boolean)

38
39
40
# File 'lib/inflect/abstract_service.rb', line 38

def valid?(words)
  (@words & words).any?
end