roda-endpoints 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c4c0a8fcc37a1cbc7990da886c1222bac0409c40
4
- data.tar.gz: 05d751cf1cbb8ee521d75e31a74d4fbcd431c8b4
3
+ metadata.gz: d27908a4d47343597580a44f31b0b70ffe4f4989
4
+ data.tar.gz: a3133effde967ed1f347b7f0d32193c3535d78c5
5
5
  SHA512:
6
- metadata.gz: 97d1319b4411dae6b2b2e8b3945e5931d8c66f1b287eded402807dd35432038db8ba1b0d6ef13dcd5f943596e1d1334d95571a71ddcfc191fa754dd866366332
7
- data.tar.gz: '05785933922272fb8d9bec0e39e73358be4472bc4a09434ca8923b5ddc0122984c4ae6b5ffb04f09af3845c17059f6cbdc7230d9a2739de90773573a0ad985e3'
6
+ metadata.gz: 48fc583f0aa6e9e8b2ffa32df5767f3cffde042f4f75aa3a8f1d11a98854e27c988c65fecf969ec7bbec6f8ccf2d646f7ae6d8bd4b78cf497eb44a9a4a0de98b
7
+ data.tar.gz: 62f7f1602928453c90ad979f695940c041aefc1f507971d0972ac9e0d273a70ad10700a5379425101b9972f4ad467f0cbae59afd9a226297f0dba5b972bedd70
data/.rubocop.yml CHANGED
@@ -13,6 +13,10 @@ Metrics/BlockLength:
13
13
  - spec/**/*.rb
14
14
  - vendor/**/*.rb
15
15
 
16
+ Metrics/ModuleLength:
17
+ Exclude:
18
+ - lib/roda/plugins/endpoints.rb
19
+
16
20
  # It is 2017, UTF time!
17
21
  Style/AsciiComments:
18
22
  Enabled: false
@@ -38,13 +38,6 @@ class Roda
38
38
  child.defaults = defaults.dup
39
39
  child.statuses = statuses.dup
40
40
  child.verbs = verbs.dup
41
- # child.verbs.each do |verb|
42
- # key = "operations.#{child.type}.#{verb}"
43
- # implementation = "operations.#{type}.#{verb}"
44
- # container.register key do
45
- # Endpoints.container.resolve implementation
46
- # end
47
- # end
48
41
  child.transactions = transactions.dup
49
42
  child.route(&@route_block)
50
43
  super
@@ -76,7 +69,8 @@ class Roda
76
69
  end
77
70
  end
78
71
  define_method(verb, &block)
79
- container.register "operations.#{type}.#{verb}", block
72
+ key = "operations.#{type}.#{verb}"
73
+ container.register key, block
80
74
  end
81
75
 
82
76
  # @param [String, Symbol] key
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'roda/endpoints/endpoint'
3
4
  require 'roda/endpoints/endpoint/data'
4
5
  require 'roda/endpoints/endpoint/caching'
5
- require 'roda/endpoints/endpoint'
6
6
  require 'inflecto'
7
7
  require 'rom/sql'
8
8
 
@@ -11,8 +11,8 @@ class Roda
11
11
  class Endpoint
12
12
  # HTTP endpoint representing a collection of items of the same type.
13
13
  class Collection < Roda::Endpoints::Endpoint
14
- prepend Data
15
- prepend Caching
14
+ include Data
15
+ include Caching
16
16
 
17
17
  self.attributes += %i(item)
18
18
  self.defaults = defaults.merge(
@@ -1,5 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'roda/endpoints'
4
+ require 'inflecto'
5
+
3
6
  class Roda
4
7
  module Endpoints
5
8
  # Generic HTTP endpoint abstraction.
@@ -21,12 +24,11 @@ class Roda
21
24
 
22
25
  # @return [ROM::Repository]
23
26
  def repository
24
- container[@repository_key] if @repository_key
25
- end
26
-
27
- # @return [{Symbol=>Object}]
28
- def to_hash
29
- super.merge(repository: @repository_key)
27
+ if @repository_key.is_a?(String)
28
+ container[@repository_key]
29
+ else
30
+ @repository_key
31
+ end
30
32
  end
31
33
  end
32
34
  end
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'roda/endpoints/endpoint/singleton'
4
- require 'roda/endpoints/endpoint/data'
5
4
  require 'forwardable'
6
5
 
7
6
  class Roda
@@ -12,23 +11,6 @@ class Roda
12
11
  class Item < Singleton
13
12
  extend Forwardable
14
13
 
15
- prepend Data
16
-
17
- self.attributes += %i(id by on)
18
- self.defaults = defaults.merge(
19
- by: :fetch,
20
- on: :id,
21
- finder: -> { repository.public_send(by, id) }
22
- )
23
-
24
- # @return [Symbol]
25
- attr_reader :by
26
-
27
- # @return [Integer]
28
- def id
29
- @id ||= captures.first
30
- end
31
-
32
14
  # @return [Endpoint::Collection]
33
15
  def collection
34
16
  parent
@@ -10,15 +10,31 @@ class Roda
10
10
  # HTTP endpoint representing a specific item of collection uniquely
11
11
  # identified by some parameter.
12
12
  class Singleton < Roda::Endpoints::Endpoint
13
- prepend Data
14
- prepend Caching
13
+ include Data
14
+ include Caching
15
15
 
16
16
  self.attributes += %i(finder last_modified)
17
- self.defaults = defaults.merge(last_modified: :updated_at)
17
+ self.defaults = defaults.merge(
18
+ last_modified: :updated_at,
19
+ by: :fetch,
20
+ on: :id,
21
+ finder: -> { repository.public_send(by, id) }
22
+ )
23
+
24
+ # @return [Symbol]
25
+ attr_reader :by
26
+
27
+ # @return [Symbol]
28
+ attr_reader :on
18
29
 
19
30
  # @return [Symbol]
20
31
  attr_reader :finder
21
32
 
33
+ # @return [Integer]
34
+ def id
35
+ @id ||= captures.first
36
+ end
37
+
22
38
  # @return [ROM::Struct]
23
39
  def entity
24
40
  @entity ||= fetch_entity
@@ -10,19 +10,30 @@ class Roda
10
10
  module Transactions
11
11
  # @return [Endpoints::Transactions]
12
12
  def transactions
13
- endpoint = self
14
- @transactions ||=
15
- begin
16
- transactions = Endpoints::Transactions.new(endpoint: self)
17
- self.class.transactions.each do |(name, block)|
18
- transactions.define(name) { instance_exec(endpoint, &block) }
19
- end
20
- transactions
21
- end
13
+ @transactions ||= Endpoints::Transactions.new(endpoint: self)
22
14
  yield @transaction if block_given?
23
15
  @transactions
24
16
  end
25
17
 
18
+ def prepare_transactions!
19
+ return if @transactions_prepared
20
+ endpoint = self
21
+ self.class.transactions.each do |(name, block)|
22
+ transactions.define(name) do
23
+ instance_exec(endpoint, &block)
24
+ end
25
+ end
26
+ verbs.each do |verb|
27
+ key = "operations.#{ns}.#{verb}"
28
+ next if container.key?(key)
29
+ operation = method(verb)
30
+ container.register key do |*args|
31
+ endpoint.instance_exec(*args, &operation)
32
+ end
33
+ end
34
+ @transactions_prepared = true
35
+ end
36
+
26
37
  # @param [Symbol, String] name
27
38
  # @param [Proc] block
28
39
  def step(name, only: [], **kwargs, &block)
@@ -7,6 +7,8 @@ require 'roda/endpoints/endpoint/namespace'
7
7
  require 'roda/endpoints/endpoint/transactions'
8
8
  require 'roda/endpoints/endpoint/validations'
9
9
  require 'roda/endpoints/endpoint/verbs'
10
+ require 'roda/endpoints/endpoint/data'
11
+ require 'roda/endpoints/endpoint/caching'
10
12
  require 'dry/monads'
11
13
 
12
14
  class Roda
@@ -40,13 +42,16 @@ class Roda
40
42
  end
41
43
  end
42
44
 
43
- prepend Namespace
45
+ include Namespace
44
46
  prepend Verbs
45
- prepend Validations
47
+ include Validations
46
48
  include Transactions
47
- prepend Lookup
49
+ include Lookup
48
50
 
49
- attr_accessor :captures
51
+ # @return [Array]
52
+ attr_reader :captures
53
+ # @return [String, Symbol]
54
+ attr_reader :on
50
55
 
51
56
  # @return [Symbol]
52
57
  def type
@@ -74,6 +79,7 @@ class Roda
74
79
  def route
75
80
  prepare_validations!
76
81
  prepare_verbs!
82
+ prepare_transactions!
77
83
  self.class.route
78
84
  end
79
85
 
@@ -13,13 +13,13 @@ class Roda
13
13
 
14
14
  # @return [Time]
15
15
  def last_modified
16
- order(Sequel.desc(:updated_at)).first.updated_at
16
+ root.order(Sequel.desc(:updated_at)).first.updated_at
17
17
  end
18
18
 
19
19
  # @param [Integer] id
20
20
  # @return [ROM::Struct]
21
21
  def fetch(id)
22
- root.fetch(id)
22
+ root.fetch(id.to_i)
23
23
  end
24
24
  end
25
25
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  class Roda
4
4
  module Endpoints
5
- VERSION = '0.3.0'
5
+ VERSION = '0.3.1'
6
6
  end
7
7
  end
@@ -159,6 +159,7 @@ class Roda
159
159
  name: name,
160
160
  entity: entity,
161
161
  type: type,
162
+ on: name.to_s,
162
163
  **kwargs
163
164
  ) do |endpoint|
164
165
  yield endpoint if block_given?
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: roda-endpoints
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Semyonov