serf 0.11.0 → 0.12.0

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.
Files changed (42) hide show
  1. data/Gemfile +1 -0
  2. data/Guardfile +1 -2
  3. data/README.md +131 -15
  4. data/example/components/logger.serf +7 -0
  5. data/example/serfs/create_widget.serf +24 -0
  6. data/lib/serf/builder.rb +7 -6
  7. data/lib/serf/loader.rb +16 -0
  8. data/lib/serf/loader/loader.rb +103 -0
  9. data/lib/serf/loader/registry.rb +86 -0
  10. data/lib/serf/middleware/error_handler.rb +4 -4
  11. data/lib/serf/middleware/parcel_freezer.rb +3 -6
  12. data/lib/serf/middleware/parcel_masher.rb +3 -6
  13. data/lib/serf/middleware/policy_checker.rb +3 -5
  14. data/lib/serf/middleware/request_timer.rb +47 -0
  15. data/lib/serf/middleware/uuid_tagger.rb +3 -5
  16. data/lib/serf/parcel_builder.rb +3 -6
  17. data/lib/serf/serfer.rb +5 -7
  18. data/lib/serf/util/uuidable.rb +3 -6
  19. data/lib/serf/version.rb +1 -1
  20. data/serf.gemspec +1 -0
  21. data/spec/serf/builder_spec.rb +4 -5
  22. data/spec/serf/errors/policy_failure_spec.rb +1 -1
  23. data/spec/serf/loader/loader_spec.rb +73 -0
  24. data/spec/serf/loader/registry_spec.rb +62 -0
  25. data/spec/serf/loader_spec.rb +57 -0
  26. data/spec/serf/middleware/error_handler_spec.rb +2 -2
  27. data/spec/serf/middleware/parcel_freezer_spec.rb +2 -2
  28. data/spec/serf/middleware/parcel_masher_spec.rb +5 -5
  29. data/spec/serf/middleware/policy_checker_spec.rb +3 -3
  30. data/spec/serf/middleware/request_timer_spec.rb +43 -0
  31. data/spec/serf/middleware/uuid_tagger_spec.rb +4 -4
  32. data/spec/serf/parcel_builder_spec.rb +7 -7
  33. data/spec/serf/serfer_spec.rb +4 -4
  34. data/spec/serf/util/error_handling_spec.rb +3 -3
  35. data/spec/serf/util/null_object_spec.rb +4 -4
  36. data/spec/serf/util/protected_call_spec.rb +4 -4
  37. data/spec/serf/util/uuidable_spec.rb +15 -15
  38. data/spec/support/factories.rb +7 -0
  39. metadata +34 -9
  40. data/lib/serf/util/options_extraction.rb +0 -117
  41. data/spec/serf/util/options_extraction_spec.rb +0 -62
  42. data/spec/support/options_extraction_wrapper.rb +0 -10
@@ -1,117 +0,0 @@
1
- require 'hashie'
2
-
3
- module Serf
4
- module Util
5
-
6
- ##
7
- # Module that provides helpers for dealing with options hash passed
8
- # to initializers.
9
- #
10
- # class Example
11
- # include Serf::Util::OptionsExtraction
12
- #
13
- # def initialize(*args, &block)
14
- # extract_options! args
15
- # puts args # Rest of args w/o the options hash.
16
- # end
17
- #
18
- # def do_work
19
- # my_option = opts :my_option, 'Default Value'
20
- # puts my_option
21
- # end
22
- # end
23
- #
24
- # example = Example.new my_option: 'Another Value'
25
- # example.do_work
26
- # # => 'Another Value'
27
- #
28
- module OptionsExtraction
29
-
30
- ##
31
- # Reader method for the options hash.
32
- #
33
- def options
34
- return @options || Hashie::Mash.new
35
- end
36
-
37
- ##
38
- # Helper method to lookup an option from our options hash.
39
- #
40
- # Examples:
41
- #
42
- # # Optional parameter whose default value is nil.
43
- # do_extra = opts :do_extra
44
- #
45
- # # Optional params that defaults to [1,2,3]
46
- # start_array = opts :start_array, [1,2,3]
47
- #
48
- # Returns default value when:
49
- # * Key is non-existent in Options Hash.
50
- # * OR when the value of the key in the Options Hash is nil.
51
- #
52
- # Returns nil when:
53
- # * Default is nil and the Options Hash lookup returns a nil.
54
- # Options Hash returns a nil because of either a
55
- # non-existent key or a nil value in said hash for said key.
56
- #
57
- def opts(key, default=nil, &block)
58
- value = options[key]
59
- value = default if value.nil?
60
- value = block.call if value.nil? && block
61
- return value
62
- end
63
-
64
- ##
65
- # Use this lookup helper if a nil value is unacceptable for processing.
66
- #
67
- # There are cases where an option may have a default value for a feature,
68
- # but the caller may just want to disable said feature. To do so,
69
- # users of this module should allow for the caller to pass in 'false'
70
- # as an option value instead of nil to disable said feature. The
71
- # implementer will have to do a boolean check of the returned option value
72
- # and disable accordingly.
73
- #
74
- # Examples:
75
- #
76
- # # Has a default logger, but we can disable logging by passing false.
77
- # # If caller had passed in nil for :logger, it would have
78
- # # defaulted to ::Log4r['my_logger'].
79
- # logger = opts! :logger, ::Log4r['my_logger']
80
- # logger = Serf::NullObject.new unless logger
81
- #
82
- # # Here we force the end user to pass in a Non-nil option as a
83
- # # mandatory parameter.
84
- # max_threads = opts! :max_threads
85
- #
86
- # Raises error when `opts` returns a nil.
87
- #
88
- def opts!(key, default=nil, &block)
89
- value = opts key, default, &block
90
- raise "Nil value found for option: #{key}, #{default}" if value.nil?
91
- return value
92
- end
93
-
94
- protected
95
-
96
- ##
97
- # Extracts the options from the arguments list, BUT does not save it to
98
- # an instance variable for use by the opts helpr methods.
99
- #
100
- def pop_options!(args)
101
- args.last.is_a?(::Hash) ?
102
- [true, Hashie::Mash.new(args.pop)] :
103
- [false, Hashie::Mash.new]
104
- end
105
-
106
- ##
107
- # Extracts the options from the arguments list, and saves it to the
108
- # an instance variable for use by the opts helpr methods.
109
- #
110
- def extract_options!(args)
111
- _, @options = pop_options! args
112
- end
113
-
114
- end
115
-
116
- end
117
- end
@@ -1,62 +0,0 @@
1
- require 'spec_helper'
2
-
3
- shared_examples '#opts with nonexistent option' do
4
- context 'with missing option' do
5
-
6
- it 'returns default data' do
7
- subject.opts(:nonexistent_option, 'DEFAULT').should == 'DEFAULT'
8
- end
9
-
10
- it 'returns nil data on no default' do
11
- subject.opts(:nonexistent_option).should be_nil
12
- end
13
-
14
- it 'returns block data' do
15
- calculator = double('calculator')
16
- calculator.should_receive(:calculate).and_return('DEFAULT')
17
- subject.opts(:nonexistent_option) {
18
- calculator.calculate
19
- }.should == 'DEFAULT'
20
- end
21
-
22
- end
23
-
24
- end
25
-
26
- describe Serf::Util::OptionsExtraction do
27
- let(:given_options) {
28
- FactoryGirl.create :random_options
29
- }
30
- subject { OptionsExtractionWrapper.new given_options }
31
-
32
- describe '#opts!' do
33
-
34
- it 'returns given data' do
35
- subject.opts!(:option_a).should == given_options.option_a
36
- end
37
-
38
- it 'fails for missing option' do
39
- expect {
40
- subject.opts! :nonexistent_option
41
- }.to raise_error
42
- end
43
-
44
- end
45
-
46
- describe '#opts' do
47
-
48
- it 'returns given data' do
49
- subject.opts(:option_a).should == given_options.option_a
50
- end
51
-
52
- end
53
-
54
- it_behaves_like '#opts with nonexistent option'
55
-
56
- context 'with no options given' do
57
- subject { OptionsExtractionWrapper.new }
58
-
59
- it_behaves_like '#opts with nonexistent option'
60
- end
61
-
62
- end
@@ -1,10 +0,0 @@
1
- require 'serf/util/options_extraction'
2
-
3
- class OptionsExtractionWrapper
4
- include Serf::Util::OptionsExtraction
5
-
6
- def initialize(*args)
7
- extract_options! args
8
- end
9
-
10
- end