Skip to content

Commit

Permalink
feat(entry): allow to use instance method, just like field in graphql…
Browse files Browse the repository at this point in the history
…-ruby
  • Loading branch information
marian13 committed Dec 5, 2023
1 parent 521d685 commit ad445fa
Show file tree
Hide file tree
Showing 15 changed files with 305 additions and 69 deletions.
4 changes: 3 additions & 1 deletion lib/convenient_service/examples/dry/gemfile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ module Dry
class Gemfile
include ConvenientService::Feature::Standard::Config

entry :format do |path|
entry :format

def format(path)
Services::Format[path: path]
end
end
Expand Down
4 changes: 3 additions & 1 deletion lib/convenient_service/examples/dry/v1/gemfile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ module V1
class Gemfile
include ConvenientService::Feature::Standard::Config

entry :format do |path|
entry :format

def format(path)
Services::Format[path: path]
end
end
Expand Down
4 changes: 3 additions & 1 deletion lib/convenient_service/examples/rails/gemfile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ module Rails
class Gemfile
include ConvenientService::Feature::Standard::Config

entry :format do |path|
entry :format

def format(path)
Services::Format[path: path]
end
end
Expand Down
4 changes: 3 additions & 1 deletion lib/convenient_service/examples/rails/v1/gemfile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ module V1
class Gemfile
include ConvenientService::Feature::Standard::Config

entry :format do |path|
entry :format

def format(path)
Services::Format[path: path]
end
end
Expand Down
4 changes: 3 additions & 1 deletion lib/convenient_service/examples/standard/cowsay.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ module Standard
class Cowsay
include ConvenientService::Feature::Standard::Config

entry :print do |text = "Hello World!", out: $stdout|
entry :print

def print(text = "Hello World!", out: $stdout)
Services::Print[text: text, out: out]
end
end
Expand Down
4 changes: 3 additions & 1 deletion lib/convenient_service/examples/standard/date_time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ module Standard
class DateTime
include ConvenientService::Feature::Standard::Config

entry :safe_parse do |string, format|
entry :safe_parse

def safe_parse(string, format)
Services::SafeParse[string: string, format: format]
end
end
Expand Down
4 changes: 3 additions & 1 deletion lib/convenient_service/examples/standard/factorial.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ module Standard
class Factorial
include ConvenientService::Feature::Standard::Config

entry :calculate do |number|
entry :calculate

def calculate(number)
Services::Calculate[number: number]
end
end
Expand Down
4 changes: 3 additions & 1 deletion lib/convenient_service/examples/standard/gemfile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ module Standard
class Gemfile
include ConvenientService::Feature::Standard::Config

entry :format do |path|
entry :format

def format(path)
Services::Format[path: path]
end
end
Expand Down
4 changes: 3 additions & 1 deletion lib/convenient_service/examples/standard/request_params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ module Standard
class RequestParams
include ConvenientService::Feature::Standard::Config

entry :prepare do |request|
entry :prepare

def prepare(request)
Services::Prepare[request: request]
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/convenient_service/feature/plugins/can_have_entries.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@

require_relative "can_have_entries/commands"
require_relative "can_have_entries/concern"
require_relative "can_have_entries/exceptions"
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ def call
define_singleton_method(name) { |*args, **kwargs, &block| new.public_send(name, *args, **kwargs, &block) }
end

feature_class.define_method(name, &body)
if body
feature_class.define_method(name, &body)
else
feature_class.define_method(name) { raise ::ConvenientService::Feature::Plugins::CanHaveEntries::Exceptions::NotDefinedEntryMethod.new(name: __method__, feature: self) }
end

name
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module Concern
class_methods do
##
# @param name [String, Symbol]
# @param body [Proc]
# @param body [Proc, nil]
# @return [String, Symbol]
#
def entry(name, &body)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

module ConvenientService
module Feature
module Plugins
module CanHaveEntries
module Exceptions
class NotDefinedEntryMethod < ::ConvenientService::Exception
def initialize(name:, feature:)
message = <<~TEXT
Entry for `#{name}` is registered inside `#{feature.class}` feature, but its corresponding method is NOT defined.
Did you forget to define it? For example:
class #{feature.class}
entry :#{name}
# ...
def #{name}
# ...
end
# ...
end
TEXT

super(message)
end
end
end
end
end
end
end
Loading

0 comments on commit ad445fa

Please sign in to comment.