lotus-utils 0.4.3 → 0.5.0

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: 119e1e01ae231b8dd402f6cda19aa9354663e439
4
- data.tar.gz: 5e28d0a9c8e1b06a66acc3ff881ac08908a5b0a3
3
+ metadata.gz: 93d211db34674dbdfa9728cf23e72e6d3236f754
4
+ data.tar.gz: c88e28e49ec323d6e74a01c6f0371d29e97e0680
5
5
  SHA512:
6
- metadata.gz: ea4c280a6b7c8b693d69bf132b85de349e0c459a4ac259f33d58313244ca97300aae686743f4f15fd00314c41d07b792151f78a02a2e2c75b9d157d4d3f744a3
7
- data.tar.gz: 95432308fb5689d4a61292cd24553df16a37e039ac79760debe4813d961dadff081e8ccb993946891e86c57fe77d2e7b85822cd3d2096111031390c1a5d7af6a
6
+ metadata.gz: 2335cb45570fc4f67b6f5b1db5f8a938d9b94e1e3d49338d68f717f39fc046fb2dc573fd6070ccd5e7aaf47f5b01092b167d2f108ef3d9308e46a029d865a5b8
7
+ data.tar.gz: 4d2c770981d67118df3723dd306a20e3644ae454c114894c6dd4bd3f78974bc04bd575af1c8089e2b53d724d6d99147a72fe175be824302c6c6c1e5ff1f91996
data/CHANGELOG.md CHANGED
@@ -1,6 +1,13 @@
1
1
  # Lotus::Utils
2
2
  Ruby core extentions and class utilities for Lotus
3
3
 
4
+ ## v0.5.0 - 2015-06-23
5
+ ### Added
6
+ - [Luca Guidi] Extracted `Lotus::Logger` from `lotusrb`
7
+
8
+ ### Changed
9
+ - [Luca Guidi] `Lotus::Interactor::Result` contains only objects explicitly exposed via `Lotus::Interactor.expose`.
10
+
4
11
  ## v0.4.3 - 2015-05-22
5
12
  ### Added
6
13
  - [François Beausoleil] Improved `Lotus::Utils::Kernel` messages for `TypeError`.
@@ -1,4 +1,5 @@
1
1
  require 'lotus/utils/basic_object'
2
+ require 'lotus/utils/class_attribute'
2
3
  require 'lotus/utils/hash'
3
4
 
4
5
  module Lotus
@@ -28,6 +29,7 @@ module Lotus
28
29
  # @api private
29
30
  def initialize(payload = {})
30
31
  @payload = _payload(payload)
32
+ @errors = []
31
33
  @success = true
32
34
  end
33
35
 
@@ -58,7 +60,15 @@ module Lotus
58
60
  # @see Lotus::Interactor#error
59
61
  # @see Lotus::Interactor#error!
60
62
  def errors
61
- @payload.fetch(:_errors) { [] }
63
+ @errors.dup
64
+ end
65
+
66
+ # @since 0.5.0
67
+ # @api private
68
+ def add_error(*errors)
69
+ @errors << errors
70
+ @errors.flatten!
71
+ nil
62
72
  end
63
73
 
64
74
  # Returns the first errors collected during an operation
@@ -122,6 +132,7 @@ module Lotus
122
132
 
123
133
  base.class_eval do
124
134
  prepend Interface
135
+ extend ClassMethods
125
136
  end
126
137
  end
127
138
 
@@ -146,8 +157,8 @@ module Lotus
146
157
  # class UpdateProfile
147
158
  # include Lotus::Interactor
148
159
  #
149
- # def initialize(person, params)
150
- # @person = person
160
+ # def initialize(user, params)
161
+ # @user = user
151
162
  # @params = params
152
163
  # end
153
164
  #
@@ -159,7 +170,6 @@ module Lotus
159
170
  super
160
171
  ensure
161
172
  @__result = ::Lotus::Interactor::Result.new
162
- @_errors = []
163
173
  end
164
174
 
165
175
  # Triggers the operation and return a result.
@@ -172,11 +182,12 @@ module Lotus
172
182
  #
173
183
  # @raise [NoMethodError] if this isn't implemented by the including class.
174
184
  #
175
- # @example Instance variables in result payload
185
+ # @example Expose instance variables in result payload
176
186
  # require 'lotus/interactor'
177
187
  #
178
188
  # class Signup
179
189
  # include Lotus::Interactor
190
+ # expose :user, :params
180
191
  #
181
192
  # def initialize(params)
182
193
  # @params = params
@@ -194,13 +205,14 @@ module Lotus
194
205
  #
195
206
  # result.user # => #<User:0x007fa311105778 @id=1 @name="Luca">
196
207
  # result.params # => { :name=>"Luca" }
197
- # result.foo # => "Bar"
208
+ # result.foo # => raises NoMethodError
198
209
  #
199
210
  # @example Failed precondition
200
211
  # require 'lotus/interactor'
201
212
  #
202
213
  # class Signup
203
214
  # include Lotus::Interactor
215
+ # expose :user
204
216
  #
205
217
  # def initialize(params)
206
218
  # @params = params
@@ -308,6 +320,7 @@ module Lotus
308
320
  #
309
321
  # class CreateRecord
310
322
  # include Lotus::Interactor
323
+ # expose :logger
311
324
  #
312
325
  # def initialize
313
326
  # @logger = []
@@ -341,7 +354,7 @@ module Lotus
341
354
  # result.errors # => ["Prepare data error", "Persist error"]
342
355
  # result.logger # => [:prepare_data!, :persist!, :sync!]
343
356
  def error(message)
344
- @_errors << message
357
+ @__result.add_error message
345
358
  false
346
359
  end
347
360
 
@@ -360,6 +373,7 @@ module Lotus
360
373
  #
361
374
  # class CreateRecord
362
375
  # include Lotus::Interactor
376
+ # expose :logger
363
377
  #
364
378
  # def initialize
365
379
  # @logger = []
@@ -417,20 +431,67 @@ module Lotus
417
431
  # @since 0.3.5
418
432
  # @api private
419
433
  def _prepare!
420
- @__result.prepare!(_instance_variables)
434
+ @__result.prepare!(_exposures)
421
435
  end
422
436
 
423
- # @since 0.3.5
437
+ # @since 0.5.0
424
438
  # @api private
425
- def _instance_variables
439
+ def _exposures
426
440
  Hash[].tap do |result|
427
- instance_variables.each do |iv|
428
- name = iv.to_s.sub(/\A@/, '')
429
- next if name.match(/\A__/)
430
-
431
- result[name.to_sym] = instance_variable_get(iv)
441
+ self.class.exposures.each do |name, ivar|
442
+ result[name] = instance_variable_get(ivar)
432
443
  end
433
444
  end
434
445
  end
435
446
  end
447
+
448
+ # @since 0.5.0
449
+ # @api private
450
+ module ClassMethods
451
+ # @since 0.5.0
452
+ # @api private
453
+ def self.extended(interactor)
454
+ interactor.class_eval do
455
+ include Utils::ClassAttribute
456
+
457
+ class_attribute :exposures
458
+ self.exposures = Utils::Hash.new
459
+ end
460
+ end
461
+
462
+ # Expose local instance variables into the returing value of <tt>#call</tt>
463
+ #
464
+ # @param instance_variable_names [Symbol,Array<Symbol>] one or more instance
465
+ # variable names
466
+ #
467
+ # @since 0.5.0
468
+ #
469
+ # @see Lotus::Interactor::Result
470
+ #
471
+ # @example Expose instance variable
472
+ #
473
+ # class Signup
474
+ # include Lotus::Interactor
475
+ # expose :user
476
+ #
477
+ # def initialize(params)
478
+ # @params = params
479
+ # @user = User.new(@params[:user])
480
+ # end
481
+ #
482
+ # def call
483
+ # # ...
484
+ # end
485
+ # end
486
+ #
487
+ # result = Signup.new(user: { name: "Luca" }).call
488
+ #
489
+ # result.user # => #<User:0x007fa85c58ccd8 @name="Luca">
490
+ # result.params # => NoMethodError
491
+ def expose(*instance_variable_names)
492
+ instance_variable_names.each do |name|
493
+ exposures[name] = "@#{ name }"
494
+ end
495
+ end
496
+ end
436
497
  end
@@ -0,0 +1,141 @@
1
+ require 'logger'
2
+ require 'lotus/utils/string'
3
+
4
+ module Lotus
5
+ # Lotus logger
6
+ #
7
+ # Implement with the same interface of Ruby std lib `Logger`.
8
+ # It uses `STDOUT` as output device.
9
+ #
10
+ #
11
+ #
12
+ # When a Lotus application is initialized, it creates a logger for that specific application.
13
+ # For instance for a `Bookshelf::Application` a `Bookshelf::Logger` will be available.
14
+ #
15
+ # This is useful for auto-tagging the output. Eg (`[Booshelf]`).
16
+ #
17
+ # When used stand alone (eg. `Lotus::Logger.info`), it tags lines with `[Shared]`.
18
+ #
19
+ #
20
+ #
21
+ # The available severity levels are the same of `Logger`:
22
+ #
23
+ # * debug
24
+ # * error
25
+ # * fatal
26
+ # * info
27
+ # * unknown
28
+ # * warn
29
+ #
30
+ # Those levels are available both as class and instance methods.
31
+ #
32
+ # @since 0.5.0
33
+ #
34
+ # @see http://www.ruby-doc.org/stdlib/libdoc/logger/rdoc/Logger.html
35
+ # @see http://www.ruby-doc.org/stdlib/libdoc/logger/rdoc/Logger/Severity.html
36
+ #
37
+ # @example Basic usage
38
+ # require 'lotus'
39
+ #
40
+ # module Bookshelf
41
+ # class Application < Lotus::Application
42
+ # end
43
+ # end
44
+ #
45
+ # # Initialize the application with the following code:
46
+ # Bookshelf::Application.load!
47
+ # # or
48
+ # Bookshelf::Application.new
49
+ #
50
+ # Bookshelf::Logger.info('Hello')
51
+ # # => I, [2015-01-10T21:55:12.727259 #80487] INFO -- [Bookshelf] : Hello
52
+ #
53
+ # Bookshelf::Logger.new.info('Hello')
54
+ # # => I, [2015-01-10T21:55:12.727259 #80487] INFO -- [Bookshelf] : Hello
55
+ #
56
+ # @example Standalone usage
57
+ # require 'lotus'
58
+ #
59
+ # Lotus::Logger.info('Hello')
60
+ # # => I, [2015-01-10T21:55:12.727259 #80487] INFO -- [Lotus] : Hello
61
+ #
62
+ # Lotus::Logger.new.info('Hello')
63
+ # # => I, [2015-01-10T21:55:12.727259 #80487] INFO -- [Lotus] : Hello
64
+ #
65
+ # @example Custom tagging
66
+ # require 'lotus'
67
+ #
68
+ # Lotus::Logger.new('FOO').info('Hello')
69
+ # # => I, [2015-01-10T21:55:12.727259 #80487] INFO -- [FOO] : Hello
70
+ class Logger < ::Logger
71
+ # Lotus::Logger default formatter
72
+ #
73
+ # @since 0.5.0
74
+ # @api private
75
+ #
76
+ # @see http://www.ruby-doc.org/stdlib/libdoc/logger/rdoc/Logger/Formatter.html
77
+ class Formatter < ::Logger::Formatter
78
+ # @since 0.5.0
79
+ # @api private
80
+ attr_writer :application_name
81
+
82
+ # @since 0.5.0
83
+ # @api private
84
+ #
85
+ # @see http://www.ruby-doc.org/stdlib/libdoc/logger/rdoc/Logger/Formatter.html#method-i-call
86
+ def call(severity, time, progname, msg)
87
+ progname = "[#{@application_name}] #{progname}"
88
+ super(severity, time.utc, progname, msg)
89
+ end
90
+ end
91
+
92
+ # Default application name.
93
+ # This is used as a fallback for tagging purposes.
94
+ #
95
+ # @since 0.5.0
96
+ # @api private
97
+ DEFAULT_APPLICATION_NAME = 'Lotus'.freeze
98
+
99
+ # @since 0.5.0
100
+ # @api private
101
+ attr_writer :application_name
102
+
103
+ # Initialize a logger
104
+ #
105
+ # @param application_name [String] an optional application name used for
106
+ # tagging purposes
107
+ #
108
+ # @since 0.5.0
109
+ def initialize(application_name = nil)
110
+ super(STDOUT)
111
+
112
+ @application_name = application_name
113
+ @formatter = Lotus::Logger::Formatter.new.tap { |f| f.application_name = self.application_name }
114
+ end
115
+
116
+ # Returns the current application name, this is used for tagging purposes
117
+ #
118
+ # @return [String] the application name
119
+ #
120
+ # @since 0.5.0
121
+ def application_name
122
+ @application_name || _application_name_from_namespace || _default_application_name
123
+ end
124
+
125
+ private
126
+ # @since 0.5.0
127
+ # @api private
128
+ def _application_name_from_namespace
129
+ class_name = self.class.name
130
+ namespace = Utils::String.new(class_name).namespace
131
+
132
+ class_name != namespace and return namespace
133
+ end
134
+
135
+ # @since 0.5.0
136
+ # @api private
137
+ def _default_application_name
138
+ DEFAULT_APPLICATION_NAME
139
+ end
140
+ end
141
+ end
@@ -3,6 +3,6 @@ module Lotus
3
3
  # Defines the version
4
4
  #
5
5
  # @since 0.1.0
6
- VERSION = '0.4.3'.freeze
6
+ VERSION = '0.5.0'.freeze
7
7
  end
8
8
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lotus-utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luca Guidi
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-05-22 00:00:00.000000000 Z
12
+ date: 2015-06-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -65,6 +65,7 @@ files:
65
65
  - LICENSE.md
66
66
  - README.md
67
67
  - lib/lotus/interactor.rb
68
+ - lib/lotus/logger.rb
68
69
  - lib/lotus/utils.rb
69
70
  - lib/lotus/utils/attributes.rb
70
71
  - lib/lotus/utils/basic_object.rb
@@ -102,7 +103,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
102
103
  version: '0'
103
104
  requirements: []
104
105
  rubyforge_project:
105
- rubygems_version: 2.4.5
106
+ rubygems_version: 2.4.8
106
107
  signing_key:
107
108
  specification_version: 4
108
109
  summary: Ruby core extentions and Louts utilities