mime_actor 0.5.3 → 0.5.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 709cab49cc347653da124ec9147c7527b96f71df08059b6eab45a5d9b5b06888
4
- data.tar.gz: ac9a75000c8711550971fa0d6e4f0ba3bc2b9e706ebd1654702df1b56d3e0f5d
3
+ metadata.gz: 5eb8de0fc146859794812ae3cb81cb99a1a775668af9681e22409fd8df0b245e
4
+ data.tar.gz: b39aa38f9a21c2b8a794ba1edaf01e184fbe0277cc256721e61df9f2fa98a3c4
5
5
  SHA512:
6
- metadata.gz: 40fe1d6ba3560b836c12ec423d8285497f2576f231a385ecf910942cedd8be7c36ea1d20077283f2aa336c0aa1beab92a6472b998a63fde4a3b9de1e71f5a9c3
7
- data.tar.gz: 5dc2dcc8697d65e386bf47f898b4ed85b8fe7d364ddfce84be873b966653c713e788c994a57e879e439ae1f6401e69092905dd88319a52c94f8c1be817daa1ca
6
+ metadata.gz: c4c8f432f1cd6d7a1678b9ed1da3cada46a28ed9406003f4fa9cac78375567dbc15f600c11cf3ef10acd76c8d4449b24385db6397be8f5dab6fc9f1ab6b9c3d9
7
+ data.tar.gz: bbe236716e0a3969737388f9390349563d088581486b17b8601b150bf88ccaf8a68beadb913c1496cfad0652727a17689a0f2ad9db46c4917bac6449e36e8a21
data/.yardopts ADDED
@@ -0,0 +1,2 @@
1
+ --markup markdown
2
+ lib/**/*.rb
data/COMPARE.md CHANGED
@@ -27,7 +27,7 @@ class EventsController < ActionController::Base
27
27
  before_action only: :index { @events = Event.all }
28
28
 
29
29
  # dynamically defines the action method according to on: argument
30
- compose_scene :html, :json, on: :index
30
+ act_on_format :html, :json, on: :index
31
31
 
32
32
  def index_html
33
33
  @event_categories = EventCategory.all
@@ -92,7 +92,7 @@ class EventsController < ActionController::Base
92
92
  # AbstractController::Callbacks here to load model with params
93
93
  before_action only: [:show, :update] { @event = Event.find(params.require(:event_id)) }
94
94
 
95
- compose_scene :html, :json, on: [:show, :update]
95
+ act_on_format :html, :json, on: [:show, :update]
96
96
 
97
97
  rescue_actor_from ActiveRecord::RecordNotFound, format: :json do |ex|
98
98
  render status: :bad_request, json: { error: ex.message }
data/README.md CHANGED
@@ -16,8 +16,8 @@ class EventsController < ActionController::Base
16
16
  before_action only: :index { @events = Event.all }
17
17
  before_action only: [:show, :update] { @event = Event.find(params.require(:event_id)) }
18
18
 
19
- compose_scene :html, :json, on: :index
20
- compose_scene :html, :json, on: [:show, :update]
19
+ act_on_format :html, :json, on: :index
20
+ act_on_format :html, :json, on: [:show, :update]
21
21
 
22
22
  rescue_actor_from ActiveRecord::RecordNotFound, format: :json do |ex|
23
23
  render status: :bad_request, json: { error: "Resouce not found" }
@@ -12,9 +12,9 @@ require "action_controller/metal/mime_responds"
12
12
  module MimeActor
13
13
  # # MimeActor Action
14
14
  #
15
- # Action is the recommended Module to be included in ActionController.
15
+ # `Action` is the recommended `Module` to be included in the `ActionController`.
16
16
  #
17
- # Provides intuitive way of action rendering for a specific MIME type with rescue handlers.
17
+ # Provides intuitive way of `action` rendering for a specific MIME type with rescue handlers.
18
18
  #
19
19
  module Action
20
20
  extend ActiveSupport::Concern
@@ -27,8 +27,21 @@ module MimeActor
27
27
  include Rescue
28
28
  include Logging
29
29
 
30
- ##
31
- # The core logic where rendering logics are collected as Proc through configuration and passed over to ActionController::MimeResponds
30
+ # The core logic where rendering logics are collected as `Proc` through configuration and passed over to `ActionController::MimeResponds`
31
+ #
32
+ # @param action the `action` of the controller
33
+ #
34
+ # @example process `create` action
35
+ # start_scene(:create)
36
+ #
37
+ # # it is equivalent to the following if `create` action is configured with `html` and `json` formats
38
+ # def create
39
+ # respond_to |format|
40
+ # format.html { public_send(:create_html) }
41
+ # format.json { public_send(:create_json) }
42
+ # end
43
+ # end
44
+ #
32
45
  def start_scene(action)
33
46
  action = action&.to_sym
34
47
  formats = acting_scenes.fetch(action, Set.new)
@@ -10,7 +10,7 @@ require "active_support/tagged_logging"
10
10
 
11
11
  module MimeActor
12
12
  # # MimeActor Logging
13
- #
13
+ #
14
14
  # Provides a configurable logger.
15
15
  module Logging
16
16
  extend ActiveSupport::Concern
@@ -13,7 +13,10 @@ require "active_support/core_ext/string/inflections"
13
13
  module MimeActor
14
14
  # # MimeActor Rescue
15
15
  #
16
- # Simillar to ActionController::Rescue but with additional filtering logic on Action/Format.
16
+ # Simillar to `ActionController::Rescue` but with additional filtering logic on `action`/`format`.
17
+ #
18
+ # @example Rescue RuntimeError when raised for any action with `json` format
19
+ # rescue_actor_from RuntimeError, format: :json, with: :handle_json_error
17
20
  #
18
21
  module Rescue
19
22
  extend ActiveSupport::Concern
@@ -26,14 +29,21 @@ module MimeActor
26
29
  end
27
30
 
28
31
  module ClassMethods
29
- ##
30
- # Registers a rescue handler for the given error classes with action/format filter
32
+ # Registers a rescue handler for the given error classes with `action`/`format` filter
33
+ #
34
+ # @param klazzes the error classes to rescue
35
+ # @param action the `action` filter
36
+ # @param format the `format` filter
37
+ # @param with the rescue hanlder when `block` is not provided
38
+ # @param block the `block` to evaluate when `with` is not provided
31
39
  #
32
- # rescue_actor_from StandardError, format: :json, action: :show do |ex|
33
- # render status: :bad_request, json: { error: ex.message }
34
- # end
40
+ # @example Rescue StandardError when raised for any action with `html` format
41
+ # rescue_actor_from StandardError, format: :html, with: :handle_html_error
35
42
  #
36
- # rescue_actor_from StandardError, format: :html, with: :handle_html_error
43
+ # @example Rescue StandardError when raised for `show` action with `json` format
44
+ # rescue_actor_from StandardError, format: :json, action: :show do |ex|
45
+ # render status: :bad_request, json: { error: ex.message }
46
+ # end
37
47
  #
38
48
  def rescue_actor_from(*klazzes, action: nil, format: nil, with: nil, &block)
39
49
  raise ArgumentError, "error filter is required" if klazzes.empty?
@@ -73,9 +83,16 @@ module MimeActor
73
83
  end
74
84
  end
75
85
 
76
- ##
77
86
  # Resolve the error provided with the registered handlers.
78
- # The handled error will be returned to indicate successful handling
87
+ #
88
+ # The handled error will be returned to indicate successful handling.
89
+ #
90
+ # @param error the error instance to rescue
91
+ # @param action the `action` filter
92
+ # @param format the `format` filter
93
+ # @param context the context to evaluate for rescue handler
94
+ # @param visited the errors to skip after no rescue handler matched the filter
95
+ #
79
96
  def rescue_actor(error, action: nil, format: nil, context: self, visited: [])
80
97
  return if visited.include?(error)
81
98
 
@@ -13,12 +13,14 @@ require "active_support/core_ext/module/attribute_accessors"
13
13
  module MimeActor
14
14
  # # MimeActor Scene
15
15
  #
16
- # Scene provides configuration for Action + Format definitions
16
+ # Scene provides configuration for `action` + `format` definitions
17
17
  #
18
- # compose_scene :html, on: :index
19
- # compose_scene :html, :json , on: [:index, :show]
18
+ # @example register a `html` format on action `index`
19
+ # act_on_format :html, on: :index
20
+ # @example register `html`, `json` formats on actions `index`, `show`
21
+ # act_on_format :html, :json , on: [:index, :show]
20
22
  #
21
- # NOTE: Calling the same action/format multiple times will overwrite previous Action + Format definitions.
23
+ # NOTE: Calling the same `action`/`format` multiple times will overwrite previous `action` + `format` definitions.
22
24
  #
23
25
  module Scene
24
26
  extend ActiveSupport::Concern
@@ -30,10 +32,18 @@ module MimeActor
30
32
  end
31
33
 
32
34
  module ClassMethods
33
- ##
34
- # Register Action + Format definitions.
35
+ # Register `action` + `format` definitions.
35
36
  #
36
- # For each unique action being registered, it will have a corresponding method being defined.
37
+ # @param options [Array]
38
+ # @option options [Array] klazzes the collection of `format`
39
+ # @option options [Hash] on the collection of `action`
40
+ #
41
+ # @example register a `html` format on action `index`
42
+ # compose_scene :html, on: :index
43
+ # @example register `html`, `json` formats on actions `index`, `show`
44
+ # compose_scene :html, :json , on: [:index, :show]
45
+ #
46
+ # For each unique `action` being registered, it will have a corresponding `action` method being defined.
37
47
  def compose_scene(*options)
38
48
  config = options.extract_options!
39
49
  validate!(:formats, options)
@@ -70,6 +80,8 @@ module MimeActor
70
80
  end
71
81
  end
72
82
  end
83
+
84
+ alias act_on_format compose_scene
73
85
  end
74
86
  end
75
87
  end
@@ -11,7 +11,7 @@ require "active_support/core_ext/module/attribute_accessors"
11
11
  module MimeActor
12
12
  # # MimeActor Stage
13
13
  #
14
- # Stage provides helpers for actor lookup and invocation.
14
+ # Stage provides helper methods for actor lookup and invocation.
15
15
  #
16
16
  module Stage
17
17
  extend ActiveSupport::Concern
@@ -23,8 +23,10 @@ module MimeActor
23
23
  end
24
24
 
25
25
  module ClassMethods
26
- ##
27
- # Determine if the name belongs to a public instance method excluding methods inherited from ancesstors
26
+ # Determine if the `actor_name` belongs to a public instance method excluding methods inherited from ancestors
27
+ #
28
+ # @param actor_name
29
+ #
28
30
  def actor?(actor_name)
29
31
  # exclude public methods from ancestors
30
32
  found = public_instance_methods(false).include?(actor_name.to_sym)
@@ -35,8 +37,20 @@ module MimeActor
35
37
  found
36
38
  end
37
39
 
38
- ##
39
- # Wraps the given block with lambda, rescue any error raised from the block via #rescue_actor if defined, otherwise, error will be re-raised
40
+ # Wraps the given `block` with a `lambda`, rescue any error raised from the `block` via `rescue_actor` if defined, otherwise, error will be re-raised
41
+ #
42
+ # @param action the `action` to be passed on to `rescue_actor`
43
+ # @param format the `format` to be passed on to `rescue_actor`
44
+ # @param context the context for the block evaluation
45
+ # @param block the `block` to be evaluated
46
+ #
47
+ # @example Dispatch a cue that prints out a text
48
+ # dispatch = self.class.dispatch_cue(action: :create, format: :json, context: self) do
49
+ # puts "completed the dispatch"
50
+ # end
51
+ #
52
+ # dispatch.call == "completed the dispatch" # true
53
+ #
40
54
  def dispatch_cue(action: nil, format: nil, context: self, &block)
41
55
  raise ArgumentError, "block must be provided" unless block_given?
42
56
 
@@ -48,10 +62,11 @@ module MimeActor
48
62
  end
49
63
  end
50
64
 
51
- ##
52
- # Call the actor method if defined, supports passing arguments to the actor method.
53
- #
54
- # If a block is given, the result from the actor method will be yieled to the block
65
+ # Calls the `actor` method if defined, supports passing arguments to the `actor` method.
66
+ #
67
+ # If a block is given, the result from the `actor` method will be yieled to the block.
68
+ #
69
+ # @param actor_name
55
70
  def cue_actor(actor_name, *args)
56
71
  unless self.class.actor?(actor_name)
57
72
  raise MimeActor::ActorNotFound, actor_name if raise_on_missing_actor
@@ -9,14 +9,14 @@ require "action_dispatch/http/mime_type"
9
9
  module MimeActor
10
10
  # # MimeActor Validator
11
11
  #
12
- # Validator provides validation rules for action, format, and rescue handlers by default.
12
+ # Validator provides validation rules for `action`, `format`, and `with` handlers by default.
13
13
  #
14
- # # raise error when rule is violated
15
- # validate!(:action, action_param)
14
+ # @example Raise error when rule is violated
15
+ # validate!(:action, action_param)
16
16
  #
17
- # # return the error when rule is violated
18
- # ex = validate_action(action_param)
19
- # raise ex if error
17
+ # @example Return the error when rule is violated
18
+ # ex = validate_action(action_param)
19
+ # raise ex if error
20
20
  #
21
21
  module Validator
22
22
  extend ActiveSupport::Concern
@@ -26,8 +26,9 @@ module MimeActor
26
26
  end
27
27
 
28
28
  module ClassMethods
29
- ##
30
- # Raise the error returned by validator if present.
29
+ # Raise the error returned by validator if any.
30
+ #
31
+ # @param rule the name of validator
31
32
  def validate!(rule, *args)
32
33
  validator = "validate_#{rule}"
33
34
  raise NameError, "Validator not found, got: #{validator}" unless respond_to?(validator, true)
@@ -36,29 +37,33 @@ module MimeActor
36
37
  raise error if error
37
38
  end
38
39
 
39
- ##
40
- # validate :action must be a Symbol
40
+ # Validate `action` must be a Symbol
41
+ #
42
+ # @param unchecked the `action` to be validated
41
43
  def validate_action(unchecked)
42
44
  ArgumentError.new("action must be a Symbol") unless unchecked.is_a?(Symbol)
43
45
  end
44
46
 
45
- ##
46
- # validate :actions must be an Array of Symbol
47
+ # Validate `actions` must be a collection of Symbol
48
+ #
49
+ # @param unchecked the `actions` to be validated
47
50
  def validate_actions(unchecked)
48
51
  rejected = unchecked.reject { |action| action.is_a?(Symbol) }
49
52
  NameError.new("invalid actions, got: #{rejected.join(", ")}") if rejected.size.positive?
50
53
  end
51
54
 
52
- ##
53
- # validate :format must be a Symbol + a valid MIME type
55
+ # Validate `format` must be a Symbol and a valid MIME type
56
+ #
57
+ # @param unchecked the `format` to be validated
54
58
  def validate_format(unchecked)
55
59
  return ArgumentError.new("format must be a Symbol") unless unchecked.is_a?(Symbol)
56
60
 
57
61
  NameError.new("invalid format, got: #{unchecked}") unless scene_formats.include?(unchecked)
58
62
  end
59
63
 
60
- ##
61
- # validate :formats must be an Array of Symbol which each of them is a valid MIME type
64
+ # Validate `formats` must be an collection of Symbol which each of them is a valid MIME type
65
+ #
66
+ # @param unchecked the `formats` to be validated
62
67
  def validate_formats(unchecked)
63
68
  unfiltered = unchecked.to_set
64
69
  filtered = unfiltered & scene_formats
@@ -67,8 +72,10 @@ module MimeActor
67
72
  NameError.new("invalid formats, got: #{rejected.join(", ")}") if rejected.size.positive?
68
73
  end
69
74
 
70
- ##
71
- # validate :with or &block must be provided + :with must be a Symbol or Proc
75
+ # Validate `with` or `block` must be provided and if `with` is provided, it must be a Symbol or Proc
76
+ #
77
+ # @param unchecked the `with` to be validated
78
+ # @param block the `block` to be valiated
72
79
  def validate_with(unchecked, block)
73
80
  if unchecked.present? && block.present?
74
81
  return ArgumentError.new("provide either the with: keyword argument or a block")
@@ -12,7 +12,7 @@ module MimeActor
12
12
  module VERSION
13
13
  MAJOR = 0
14
14
  MINOR = 5
15
- BUILD = 3
15
+ BUILD = 4
16
16
  PRE = nil
17
17
 
18
18
  STRING = [MAJOR, MINOR, BUILD, PRE].compact.join(".")
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mime_actor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.3
4
+ version: 0.5.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Chang
@@ -63,6 +63,7 @@ files:
63
63
  - ".rspec"
64
64
  - ".rubocop.yml"
65
65
  - ".ruby-version"
66
+ - ".yardopts"
66
67
  - COMPARE.md
67
68
  - LICENSE
68
69
  - README.md