spectre-core 1.12.0 → 1.12.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,39 +1,39 @@
1
- require_relative '../spectre'
2
-
3
- module Spectre
4
- module Diagnostic
5
- module Stopwatch
6
- @@duration = 0.0
7
-
8
- class << self
9
- def start_watch
10
- @@start_time = Time.now
11
- end
12
-
13
- def stop_watch
14
- @@end_time = Time.now
15
- end
16
-
17
- def measure
18
- start_watch
19
- yield
20
- stop_watch
21
- end
22
-
23
- def duration
24
- @@end_time - @@start_time
25
- end
26
-
27
- def started_at
28
- @@start_time
29
- end
30
-
31
- def finished_at
32
- @@end_time
33
- end
34
- end
35
-
36
- Spectre.delegate :start_watch, :stop_watch, :duration, :measure, to: self
37
- end
38
- end
39
- end
1
+ require_relative '../spectre'
2
+
3
+ module Spectre
4
+ module Diagnostic
5
+ module Stopwatch
6
+ @@duration = 0.0
7
+
8
+ class << self
9
+ def start_watch
10
+ @@start_time = Time.now
11
+ end
12
+
13
+ def stop_watch
14
+ @@end_time = Time.now
15
+ end
16
+
17
+ def measure
18
+ start_watch
19
+ yield
20
+ stop_watch
21
+ end
22
+
23
+ def duration
24
+ @@end_time - @@start_time
25
+ end
26
+
27
+ def started_at
28
+ @@start_time
29
+ end
30
+
31
+ def finished_at
32
+ @@end_time
33
+ end
34
+ end
35
+
36
+ Spectre.delegate :start_watch, :stop_watch, :duration, :measure, to: self
37
+ end
38
+ end
39
+ end
@@ -1,30 +1,30 @@
1
- require_relative '../spectre'
2
-
3
- require 'ostruct'
4
-
5
- def to_recursive_ostruct(hash)
6
- OpenStruct.new(
7
- hash.each_with_object({}) do |(key, val), memo|
8
- memo[key] = val.is_a?(Hash) ? to_recursive_ostruct(val) : val
9
- end
10
- )
11
- end
12
-
13
- module Spectre
14
- module Environment
15
- class << self
16
- @@environment = OpenStruct.new
17
-
18
- def env
19
- @@environment
20
- end
21
- end
22
-
23
- Spectre.register do |config|
24
- @@environment = to_recursive_ostruct(config)
25
- @@environment.freeze
26
- end
27
-
28
- Spectre.delegate :env, to: self
29
- end
30
- end
1
+ require_relative '../spectre'
2
+
3
+ require 'ostruct'
4
+
5
+ def to_recursive_ostruct(hash)
6
+ OpenStruct.new(
7
+ hash.each_with_object({}) do |(key, val), memo|
8
+ memo[key] = val.is_a?(Hash) ? to_recursive_ostruct(val) : val
9
+ end
10
+ )
11
+ end
12
+
13
+ module Spectre
14
+ module Environment
15
+ class << self
16
+ @@environment = OpenStruct.new
17
+
18
+ def env
19
+ @@environment
20
+ end
21
+ end
22
+
23
+ Spectre.register do |config|
24
+ @@environment = to_recursive_ostruct(config)
25
+ @@environment.freeze
26
+ end
27
+
28
+ Spectre.delegate :env, to: self
29
+ end
30
+ end
@@ -1,133 +1,133 @@
1
- require_relative '../spectre'
2
-
3
- require 'securerandom'
4
- require 'json'
5
- require 'date'
6
- require 'ostruct'
7
- require 'jsonpath'
8
-
9
- class ::String
10
- def as_json
11
- JSON.parse(self, object_class: OpenStruct)
12
- end
13
-
14
- def as_date
15
- DateTime.parse(self)
16
- end
17
-
18
- def as_timestamp
19
- DateTime.parse(self).to_time.to_i
20
- end
21
-
22
- def with mapping
23
- return self unless mapping and mapping.is_a? Hash
24
-
25
- new_string = self
26
-
27
- mapping.each do |key, value|
28
- new_string = new_string.gsub('#{' + key.to_s + '}', value.to_s)
29
- end
30
-
31
- new_string
32
- end
33
-
34
- def trim size = 50
35
- if (self.length + 3) > size
36
- return self[0..size-4] + '...'
37
- end
38
-
39
- self
40
- end
41
-
42
- def pick path
43
- raise ArgumentError.new("`path' must not be nil or empty") if path.nil? or path.empty?
44
-
45
- begin
46
- JsonPath.on(self, path)
47
- rescue MultiJson::ParseError
48
- # do nothing and return nil
49
- end
50
- end
51
-
52
- # File helpers
53
-
54
- def content with: nil
55
- fail "'#{self}' is not a file path, or the file does not exist." unless File.exists? self
56
-
57
- file_content = File.read(self)
58
-
59
- if with
60
- file_content.with(with)
61
- else
62
- file_content
63
- end
64
- end
65
-
66
- def file_size
67
- fail "'#{self}' is not a file path, or the file does not exist." unless File.exists? self
68
-
69
- File.size(self)
70
- end
71
-
72
- def exists?
73
- File.exists? self
74
- end
75
-
76
- def remove!
77
- fail "'#{self}' is not a file path, or the file does not exist." unless File.exists? self
78
-
79
- File.delete self
80
- end
81
- end
82
-
83
- class ::OpenStruct
84
- def to_json *args, **kwargs
85
- self.to_h.inject({}) { |memo, (k,v)| memo[k] = v.is_a?(OpenStruct) ? v.to_h : v; memo }.to_json(*args, **kwargs)
86
- end
87
-
88
- def pick path
89
- raise ArgumentError.new("`path' must not be nil or empty") if path.nil? or path.empty?
90
-
91
- JsonPath.on(self, path)
92
- end
93
-
94
- def default_to! defaults
95
- defaults.each_key do |key|
96
- unless self[key] != nil
97
- self[key] = defaults[key]
98
- end
99
- end
100
- end
101
-
102
- alias :defaults_to! :default_to!
103
- end
104
-
105
- class ::Hash
106
- def symbolize_keys
107
- self.inject({}) { |memo, (k,v)| memo[k.to_sym] = v; memo }
108
- end
109
-
110
- def default_to! defaults
111
- defaults.each_key do |key|
112
- unless self[key] != nil
113
- self[key] = defaults[key]
114
- end
115
- end
116
- end
117
-
118
- alias :defaults_to! :default_to!
119
- end
120
-
121
- class ::Array
122
- def last_element
123
- self[-1]
124
- end
125
- end
126
-
127
- def uuid length = 5
128
- SecureRandom.uuid().gsub('-', '')[0..length-1]
129
- end
130
-
131
- def now
132
- Time.now
133
- end
1
+ require_relative '../spectre'
2
+
3
+ require 'securerandom'
4
+ require 'json'
5
+ require 'date'
6
+ require 'ostruct'
7
+ require 'jsonpath'
8
+
9
+ class ::String
10
+ def as_json
11
+ JSON.parse(self, object_class: OpenStruct)
12
+ end
13
+
14
+ def as_date
15
+ DateTime.parse(self)
16
+ end
17
+
18
+ def as_timestamp
19
+ DateTime.parse(self).to_time.to_i
20
+ end
21
+
22
+ def with mapping
23
+ return self unless mapping and mapping.is_a? Hash
24
+
25
+ new_string = self
26
+
27
+ mapping.each do |key, value|
28
+ new_string = new_string.gsub('#{' + key.to_s + '}', value.to_s)
29
+ end
30
+
31
+ new_string
32
+ end
33
+
34
+ def trim size = 50
35
+ if (self.length + 3) > size
36
+ return self[0..size-4] + '...'
37
+ end
38
+
39
+ self
40
+ end
41
+
42
+ def pick path
43
+ raise ArgumentError.new("`path' must not be nil or empty") if path.nil? or path.empty?
44
+
45
+ begin
46
+ JsonPath.on(self, path)
47
+ rescue MultiJson::ParseError
48
+ # do nothing and return nil
49
+ end
50
+ end
51
+
52
+ # File helpers
53
+
54
+ def content with: nil
55
+ fail "'#{self}' is not a file path, or the file does not exist." unless File.exists? self
56
+
57
+ file_content = File.read(self)
58
+
59
+ if with
60
+ file_content.with(with)
61
+ else
62
+ file_content
63
+ end
64
+ end
65
+
66
+ def file_size
67
+ fail "'#{self}' is not a file path, or the file does not exist." unless File.exists? self
68
+
69
+ File.size(self)
70
+ end
71
+
72
+ def exists?
73
+ File.exists? self
74
+ end
75
+
76
+ def remove!
77
+ fail "'#{self}' is not a file path, or the file does not exist." unless File.exists? self
78
+
79
+ File.delete self
80
+ end
81
+ end
82
+
83
+ class ::OpenStruct
84
+ def to_json *args, **kwargs
85
+ self.to_h.inject({}) { |memo, (k,v)| memo[k] = v.is_a?(OpenStruct) ? v.to_h : v; memo }.to_json(*args, **kwargs)
86
+ end
87
+
88
+ def pick path
89
+ raise ArgumentError.new("`path' must not be nil or empty") if path.nil? or path.empty?
90
+
91
+ JsonPath.on(self, path)
92
+ end
93
+
94
+ def default_to! defaults
95
+ defaults.each_key do |key|
96
+ unless self[key] != nil
97
+ self[key] = defaults[key]
98
+ end
99
+ end
100
+ end
101
+
102
+ alias :defaults_to! :default_to!
103
+ end
104
+
105
+ class ::Hash
106
+ def symbolize_keys
107
+ self.inject({}) { |memo, (k,v)| memo[k.to_sym] = v; memo }
108
+ end
109
+
110
+ def default_to! defaults
111
+ defaults.each_key do |key|
112
+ unless self[key] != nil
113
+ self[key] = defaults[key]
114
+ end
115
+ end
116
+ end
117
+
118
+ alias :defaults_to! :default_to!
119
+ end
120
+
121
+ class ::Array
122
+ def last_element
123
+ self[-1]
124
+ end
125
+ end
126
+
127
+ def uuid length = 5
128
+ SecureRandom.uuid().gsub('-', '')[0..length-1]
129
+ end
130
+
131
+ def now
132
+ Time.now
133
+ end