inflect 0.5.0 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +2 -4
- data/lib/inflect/abstract_service.rb +10 -12
- data/lib/inflect/configuration.rb +10 -1
- data/lib/inflect/director.rb +6 -11
- data/lib/inflect/service_provider.rb +28 -0
- data/lib/inflect/service_provider_methods.rb +23 -0
- data/lib/inflect/version.rb +1 -1
- data/lib/inflect.rb +6 -1
- metadata +4 -3
- data/lib/services/random_service.rb +0 -27
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 255046c5af958b2b850542fe3f257856598dbdd3
|
4
|
+
data.tar.gz: 24f592fa8da795e93d1d5dbc03b8c412519a82ca
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 06ae10b731c6f821cb5fecc08b154c723883aba705a298b9dff376062a8da8528d6bb89add35eaef3ad4b8a1da193891e88a20fc13d073ff9084109736c3b2b5
|
7
|
+
data.tar.gz: ce790b83f577d02190fa5773dc2e185f7ebcc93db0bebec0ed77c7d7149a00c0b71645027084e5919e6eb9759d3c7b732a2efdb2fed375d9c5fd2001bc8b411b
|
data/README.md
CHANGED
@@ -30,11 +30,9 @@ Generate your Service:
|
|
30
30
|
|
31
31
|
$ inflect generate:service Weather
|
32
32
|
|
33
|
-
IMPORTANT: service name must be in CamelCase
|
33
|
+
IMPORTANT: service name must be in CamelCase.
|
34
34
|
|
35
|
-
|
36
|
-
|
37
|
-
Implement at least the WeatherService#handle method:
|
35
|
+
All the actions that you declare in the array must have its corresponding method.
|
38
36
|
|
39
37
|
```ruby
|
40
38
|
class WeatherService < Inflect::AbstractService
|
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'singleton'
|
2
2
|
require 'inflect/responsive'
|
3
|
+
require 'inflect/service_provider_methods'
|
3
4
|
|
4
5
|
module Inflect
|
5
6
|
# Acts as an specification or standard required for a Service
|
@@ -10,6 +11,7 @@ module Inflect
|
|
10
11
|
include Comparable
|
11
12
|
include Singleton
|
12
13
|
include Responsive
|
14
|
+
include ServiceProviderMethods
|
13
15
|
|
14
16
|
# A +words+ Array constant with the key +words+ of the Service.
|
15
17
|
# @example Array for New York Times service
|
@@ -26,7 +28,7 @@ module Inflect
|
|
26
28
|
end
|
27
29
|
|
28
30
|
# Implement Comparable in order to be sortable.
|
29
|
-
def <=>
|
31
|
+
def <=>(other_service)
|
30
32
|
priority <=> other_service.priority
|
31
33
|
end
|
32
34
|
|
@@ -39,19 +41,13 @@ module Inflect
|
|
39
41
|
action_defined(request.action)
|
40
42
|
end
|
41
43
|
|
42
|
-
# The +default+ is the method called when there is no action parameter inside
|
43
|
-
# the words query array.
|
44
|
-
def default
|
45
|
-
no_method_error
|
46
|
-
end
|
47
|
-
|
48
44
|
# Returns a Hash with retrieved data by routing from the request
|
49
45
|
# to the method specified by the request's action attribute.
|
50
46
|
# @param Inflect::Request
|
51
47
|
# @return Inflect::Response
|
52
48
|
def handle(request)
|
53
49
|
if action_defined(request.action) && !action_implemented(request.action)
|
54
|
-
no_method_error
|
50
|
+
no_method_error request
|
55
51
|
else
|
56
52
|
if request.arguments.empty?
|
57
53
|
send request.action
|
@@ -88,10 +84,12 @@ module Inflect
|
|
88
84
|
|
89
85
|
# Method fired when users don't implement the method corresponding
|
90
86
|
# to the actions they define in the +words+ array.
|
91
|
-
def no_method_error
|
92
|
-
|
93
|
-
|
94
|
-
|
87
|
+
def no_method_error(request)
|
88
|
+
if request.action.eql? :default
|
89
|
+
message = "No default action declared for #{self.class}, the default method must be implemented if you want #{self.class} to respond to #{self.words}."
|
90
|
+
else
|
91
|
+
message = "#{self.class} declared the action #{request.action} in #{self.words} but the method is missing, for more information see Inflect::AbstractService class."
|
92
|
+
end
|
95
93
|
raise NoMethodError.new message
|
96
94
|
end
|
97
95
|
end
|
@@ -12,6 +12,7 @@ module Inflect
|
|
12
12
|
# Method that allows configuration via block.
|
13
13
|
def self.configure
|
14
14
|
yield configuration if block_given?
|
15
|
+
reload if configuration.services_path_changed
|
15
16
|
end
|
16
17
|
|
17
18
|
# The class in charge of centralizing the application's
|
@@ -23,10 +24,18 @@ module Inflect
|
|
23
24
|
# Location of the locale file.
|
24
25
|
attr_accessor :locale_path
|
25
26
|
|
27
|
+
# Default service path
|
28
|
+
attr_accessor :default_path
|
29
|
+
|
26
30
|
|
27
31
|
def initialize
|
28
|
-
@
|
32
|
+
@default_path = File.join('lib', 'services')
|
33
|
+
@services_path = @services_path || default_path
|
29
34
|
@locale_path = File.join(File.dirname(__FILE__), 'locale/en.yml')
|
30
35
|
end
|
36
|
+
|
37
|
+
def services_path_changed
|
38
|
+
!services_path.eql? default_path
|
39
|
+
end
|
31
40
|
end
|
32
41
|
end
|
data/lib/inflect/director.rb
CHANGED
@@ -1,20 +1,11 @@
|
|
1
|
-
require 'inflect/loader'
|
2
|
-
require 'inflect/configuration'
|
3
1
|
require 'inflect/request'
|
2
|
+
require 'inflect/service_provider_methods'
|
4
3
|
|
5
4
|
module Inflect
|
6
5
|
# The class in charge of managing the access
|
7
6
|
# and selection of the services.
|
8
7
|
class Director
|
9
|
-
|
10
|
-
attr_reader :services
|
11
|
-
|
12
|
-
# @param services_path [String]
|
13
|
-
def initialize(services_path = nil)
|
14
|
-
@services = Loader.services(
|
15
|
-
services_path || Inflect.configuration.services_path
|
16
|
-
)
|
17
|
-
end
|
8
|
+
include ServiceProviderMethods
|
18
9
|
|
19
10
|
# Finds the first Service that is able
|
20
11
|
# to handle the request and lets him do
|
@@ -30,6 +21,10 @@ module Inflect
|
|
30
21
|
end
|
31
22
|
end
|
32
23
|
|
24
|
+
def reload
|
25
|
+
service_provider.reload
|
26
|
+
end
|
27
|
+
|
33
28
|
private
|
34
29
|
|
35
30
|
def select_service(request)
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'inflect/loader'
|
2
|
+
require 'inflect/configuration'
|
3
|
+
|
4
|
+
module Inflect
|
5
|
+
# The class in charge of providing common access to available services across
|
6
|
+
# all the application.
|
7
|
+
class ServiceProvider
|
8
|
+
include Singleton
|
9
|
+
|
10
|
+
# Access available services.
|
11
|
+
attr_reader :services
|
12
|
+
|
13
|
+
# Load all services on provider instantiation.
|
14
|
+
def initialize
|
15
|
+
@services = load
|
16
|
+
end
|
17
|
+
|
18
|
+
def reload
|
19
|
+
@services = load
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def load
|
25
|
+
Loader.services(Inflect.configuration.services_path)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'inflect/service_provider'
|
2
|
+
require 'pry'
|
3
|
+
|
4
|
+
module Inflect
|
5
|
+
module ServiceProviderMethods
|
6
|
+
def self.included(base)
|
7
|
+
base.send :include, InstanceMethods
|
8
|
+
end
|
9
|
+
|
10
|
+
module InstanceMethods
|
11
|
+
# Provide the Service Provider as a virtual attribute.
|
12
|
+
def service_provider
|
13
|
+
Inflect::ServiceProvider.instance
|
14
|
+
end
|
15
|
+
|
16
|
+
# Access available services through a virtual attribute.
|
17
|
+
# @return [Enumerable[AbstractService]]
|
18
|
+
def services
|
19
|
+
service_provider.services
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/inflect/version.rb
CHANGED
data/lib/inflect.rb
CHANGED
@@ -13,11 +13,16 @@ module Inflect
|
|
13
13
|
director.handle(words)
|
14
14
|
end
|
15
15
|
|
16
|
+
def reload
|
17
|
+
director.reload
|
18
|
+
end
|
19
|
+
|
16
20
|
private
|
21
|
+
|
17
22
|
# Method that returns the Director instance reference.
|
18
23
|
# @return [Inflect::Director]
|
19
24
|
def director
|
20
|
-
@@director ||= Director.new
|
25
|
+
@@director ||= Director.new
|
21
26
|
end
|
22
27
|
end
|
23
28
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: inflect
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Santiago Figueiras
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2016-09-
|
12
|
+
date: 2016-09-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -113,8 +113,9 @@ files:
|
|
113
113
|
- lib/inflect/request.rb
|
114
114
|
- lib/inflect/response.rb
|
115
115
|
- lib/inflect/responsive.rb
|
116
|
+
- lib/inflect/service_provider.rb
|
117
|
+
- lib/inflect/service_provider_methods.rb
|
116
118
|
- lib/inflect/version.rb
|
117
|
-
- lib/services/random_service.rb
|
118
119
|
homepage: https://github.com/InflectProject/inflect
|
119
120
|
licenses:
|
120
121
|
- MIT
|
@@ -1,27 +0,0 @@
|
|
1
|
-
class RandomService < Inflect::AbstractService
|
2
|
-
# A WORDS Array constant with the key words of the Service,
|
3
|
-
# the first word of the array represents the keyword for the
|
4
|
-
# service, the second represents the action (method), and all the rest are
|
5
|
-
# the arguments to that action.
|
6
|
-
# @example Array for New York Times service
|
7
|
-
# words = %W[NEWS TODAY NEW\ YORK\ TIMES]
|
8
|
-
#
|
9
|
-
# This would mean that the service would handle request with the
|
10
|
-
# keyword NEWS, in it's #today method with the arguments "new york times".
|
11
|
-
|
12
|
-
# In case there are modules that provide similar contents the
|
13
|
-
# one with most PRIORITY is picked.
|
14
|
-
# Float::Infinity is the lowest priority.
|
15
|
-
def initialize
|
16
|
-
@priority = 1
|
17
|
-
@words = %W[RANDOM].freeze
|
18
|
-
end
|
19
|
-
|
20
|
-
# This method is the only one needed for Inflect to work.
|
21
|
-
# Implements how the service responds to a request that has no
|
22
|
-
# action attribute.
|
23
|
-
# @return Inflect::Response
|
24
|
-
def default
|
25
|
-
super
|
26
|
-
end
|
27
|
-
end
|