garcun 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (139) hide show
  1. checksums.yaml +7 -0
  2. data/.gitattributes +17 -0
  3. data/.gitignore +197 -0
  4. data/.rspec +2 -0
  5. data/Gemfile +22 -0
  6. data/LICENSE +201 -0
  7. data/README.md +521 -0
  8. data/Rakefile +47 -0
  9. data/garcun.gemspec +83 -0
  10. data/lib/garcon.rb +290 -0
  11. data/lib/garcon/chef/chef_helpers.rb +343 -0
  12. data/lib/garcon/chef/coerce/coercer.rb +134 -0
  13. data/lib/garcon/chef/coerce/coercions/boolean_definitions.rb +34 -0
  14. data/lib/garcon/chef/coerce/coercions/date_definitions.rb +32 -0
  15. data/lib/garcon/chef/coerce/coercions/date_time_definitions.rb +32 -0
  16. data/lib/garcon/chef/coerce/coercions/fixnum_definitions.rb +34 -0
  17. data/lib/garcon/chef/coerce/coercions/float_definitions.rb +32 -0
  18. data/lib/garcon/chef/coerce/coercions/hash_definitions.rb +29 -0
  19. data/lib/garcon/chef/coerce/coercions/integer_definitions.rb +31 -0
  20. data/lib/garcon/chef/coerce/coercions/string_definitions.rb +45 -0
  21. data/lib/garcon/chef/coerce/coercions/time_definitions.rb +32 -0
  22. data/lib/garcon/chef/handler/devreporter.rb +127 -0
  23. data/lib/garcon/chef/log.rb +64 -0
  24. data/lib/garcon/chef/node.rb +100 -0
  25. data/lib/garcon/chef/provider/civilize.rb +209 -0
  26. data/lib/garcon/chef/provider/development.rb +159 -0
  27. data/lib/garcon/chef/provider/download.rb +420 -0
  28. data/lib/garcon/chef/provider/house_keeping.rb +265 -0
  29. data/lib/garcon/chef/provider/node_cache.rb +31 -0
  30. data/lib/garcon/chef/provider/partial.rb +183 -0
  31. data/lib/garcon/chef/provider/recovery.rb +80 -0
  32. data/lib/garcon/chef/provider/zip_file.rb +271 -0
  33. data/lib/garcon/chef/resource/attribute.rb +52 -0
  34. data/lib/garcon/chef/resource/base_dsl.rb +174 -0
  35. data/lib/garcon/chef/resource/blender.rb +140 -0
  36. data/lib/garcon/chef/resource/lazy_eval.rb +66 -0
  37. data/lib/garcon/chef/resource/resource_name.rb +109 -0
  38. data/lib/garcon/chef/secret_bag.rb +204 -0
  39. data/lib/garcon/chef/validations.rb +76 -0
  40. data/lib/garcon/chef_inclusions.rb +151 -0
  41. data/lib/garcon/configuration.rb +138 -0
  42. data/lib/garcon/core_ext.rb +39 -0
  43. data/lib/garcon/core_ext/array.rb +27 -0
  44. data/lib/garcon/core_ext/binding.rb +64 -0
  45. data/lib/garcon/core_ext/boolean.rb +66 -0
  46. data/lib/garcon/core_ext/duration.rb +271 -0
  47. data/lib/garcon/core_ext/enumerable.rb +34 -0
  48. data/lib/garcon/core_ext/file.rb +127 -0
  49. data/lib/garcon/core_ext/filetest.rb +62 -0
  50. data/lib/garcon/core_ext/hash.rb +279 -0
  51. data/lib/garcon/core_ext/kernel.rb +159 -0
  52. data/lib/garcon/core_ext/lazy.rb +222 -0
  53. data/lib/garcon/core_ext/method_access.rb +243 -0
  54. data/lib/garcon/core_ext/module.rb +92 -0
  55. data/lib/garcon/core_ext/nil.rb +53 -0
  56. data/lib/garcon/core_ext/numeric.rb +44 -0
  57. data/lib/garcon/core_ext/object.rb +342 -0
  58. data/lib/garcon/core_ext/pathname.rb +152 -0
  59. data/lib/garcon/core_ext/process.rb +41 -0
  60. data/lib/garcon/core_ext/random.rb +497 -0
  61. data/lib/garcon/core_ext/string.rb +312 -0
  62. data/lib/garcon/core_ext/struct.rb +49 -0
  63. data/lib/garcon/core_ext/symbol.rb +170 -0
  64. data/lib/garcon/core_ext/time.rb +234 -0
  65. data/lib/garcon/exceptions.rb +101 -0
  66. data/lib/garcon/inflections.rb +237 -0
  67. data/lib/garcon/inflections/defaults.rb +79 -0
  68. data/lib/garcon/inflections/inflections.rb +182 -0
  69. data/lib/garcon/inflections/rules_collection.rb +37 -0
  70. data/lib/garcon/secret.rb +271 -0
  71. data/lib/garcon/stash/format.rb +114 -0
  72. data/lib/garcon/stash/journal.rb +226 -0
  73. data/lib/garcon/stash/queue.rb +83 -0
  74. data/lib/garcon/stash/serializer.rb +86 -0
  75. data/lib/garcon/stash/store.rb +435 -0
  76. data/lib/garcon/task.rb +31 -0
  77. data/lib/garcon/task/atomic.rb +151 -0
  78. data/lib/garcon/task/atomic_boolean.rb +127 -0
  79. data/lib/garcon/task/condition.rb +99 -0
  80. data/lib/garcon/task/copy_on_notify_observer_set.rb +154 -0
  81. data/lib/garcon/task/copy_on_write_observer_set.rb +153 -0
  82. data/lib/garcon/task/count_down_latch.rb +92 -0
  83. data/lib/garcon/task/delay.rb +196 -0
  84. data/lib/garcon/task/dereferenceable.rb +144 -0
  85. data/lib/garcon/task/event.rb +119 -0
  86. data/lib/garcon/task/executor.rb +275 -0
  87. data/lib/garcon/task/executor_options.rb +59 -0
  88. data/lib/garcon/task/future.rb +107 -0
  89. data/lib/garcon/task/immediate_executor.rb +84 -0
  90. data/lib/garcon/task/ivar.rb +171 -0
  91. data/lib/garcon/task/lazy_reference.rb +74 -0
  92. data/lib/garcon/task/monotonic_time.rb +69 -0
  93. data/lib/garcon/task/obligation.rb +256 -0
  94. data/lib/garcon/task/observable.rb +101 -0
  95. data/lib/garcon/task/priority_queue.rb +234 -0
  96. data/lib/garcon/task/processor_count.rb +128 -0
  97. data/lib/garcon/task/read_write_lock.rb +304 -0
  98. data/lib/garcon/task/safe_task_executor.rb +58 -0
  99. data/lib/garcon/task/single_thread_executor.rb +97 -0
  100. data/lib/garcon/task/thread_pool/cached.rb +71 -0
  101. data/lib/garcon/task/thread_pool/executor.rb +294 -0
  102. data/lib/garcon/task/thread_pool/fixed.rb +61 -0
  103. data/lib/garcon/task/thread_pool/worker.rb +90 -0
  104. data/lib/garcon/task/timer.rb +44 -0
  105. data/lib/garcon/task/timer_set.rb +194 -0
  106. data/lib/garcon/task/timer_task.rb +377 -0
  107. data/lib/garcon/task/waitable_list.rb +58 -0
  108. data/lib/garcon/utility/ansi.rb +199 -0
  109. data/lib/garcon/utility/at_random.rb +77 -0
  110. data/lib/garcon/utility/crypto.rb +292 -0
  111. data/lib/garcon/utility/equalizer.rb +146 -0
  112. data/lib/garcon/utility/faker/extensions/array.rb +22 -0
  113. data/lib/garcon/utility/faker/extensions/symbol.rb +9 -0
  114. data/lib/garcon/utility/faker/faker.rb +164 -0
  115. data/lib/garcon/utility/faker/faker/company.rb +17 -0
  116. data/lib/garcon/utility/faker/faker/hacker.rb +30 -0
  117. data/lib/garcon/utility/faker/faker/version.rb +3 -0
  118. data/lib/garcon/utility/faker/locales/en-US.yml +83 -0
  119. data/lib/garcon/utility/faker/locales/en.yml +21 -0
  120. data/lib/garcon/utility/file_helper.rb +170 -0
  121. data/lib/garcon/utility/hookers.rb +178 -0
  122. data/lib/garcon/utility/interpolation.rb +90 -0
  123. data/lib/garcon/utility/memstash.rb +364 -0
  124. data/lib/garcon/utility/misc.rb +54 -0
  125. data/lib/garcon/utility/msg_from_god.rb +62 -0
  126. data/lib/garcon/utility/retry.rb +238 -0
  127. data/lib/garcon/utility/timeout.rb +58 -0
  128. data/lib/garcon/utility/uber/builder.rb +91 -0
  129. data/lib/garcon/utility/uber/callable.rb +7 -0
  130. data/lib/garcon/utility/uber/delegates.rb +13 -0
  131. data/lib/garcon/utility/uber/inheritable_attr.rb +37 -0
  132. data/lib/garcon/utility/uber/options.rb +101 -0
  133. data/lib/garcon/utility/uber/uber_version.rb +3 -0
  134. data/lib/garcon/utility/uber/version.rb +33 -0
  135. data/lib/garcon/utility/url_helper.rb +100 -0
  136. data/lib/garcon/utils.rb +29 -0
  137. data/lib/garcon/version.rb +62 -0
  138. data/lib/garcun.rb +24 -0
  139. metadata +680 -0
@@ -0,0 +1,134 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Author: Stefano Harding <riddopic@gmail.com>
4
+ # License: Apache License, Version 2.0
5
+ # Copyright: (C) 2014-2015 Stefano Harding
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #
19
+
20
+ module Garcon
21
+ def self.coercer
22
+ return @coercer if @coercer
23
+ @coercer = Garcon::Coercer.new
24
+ Garcon::Coercions.bind_to(@coercer)
25
+ @coercer
26
+ end
27
+
28
+ def self.coercer=(coercer)
29
+ @coercer = coercer
30
+ end
31
+
32
+ class Coercer
33
+ def initialize
34
+ @coercions = Hash.new do |hash, origin|
35
+ hash[origin] = Hash.new do |h, target|
36
+ h[target] = Coercion.new(origin, target)
37
+ end
38
+ end
39
+ @mutex = Mutex.new
40
+ end
41
+
42
+ # Registers a coercion with the Garcon library.
43
+ #
44
+ # @param [Class] origin
45
+ # The class to convert.
46
+ #
47
+ # @param [Class] target
48
+ # What the origin will be converted to.
49
+ #
50
+ def register(origin, target, &block)
51
+ raise(ArgumentError, 'block is required') unless block_given?
52
+
53
+ @mutex.synchronize do
54
+ @coercions[origin][target] = Coercion.new(origin, target, &block)
55
+ end
56
+ end
57
+
58
+ # Removes a coercion from the library
59
+ #
60
+ # @param [Class] origin
61
+ #
62
+ # @param [Class] target
63
+ #
64
+ def unregister(origin, target)
65
+ @mutex.synchronize do
66
+ @coercions[origin].delete(target)
67
+ end
68
+ end
69
+
70
+ # @param [Object] object
71
+ # The object to coerce.
72
+ #
73
+ # @param [Class] target
74
+ # What you want the object to turn in to.
75
+ #
76
+ def coerce(object, target)
77
+ @mutex.synchronize do
78
+ @coercions[object.class][target].call(object)
79
+ end
80
+ end
81
+ end
82
+
83
+ # This wraps the block that is provided when you register a coercion.
84
+ #
85
+ class Coercion
86
+ # Passes the object on through.
87
+ PASS_THROUGH = ->(obj, _) { obj }
88
+
89
+ # @param [Class] origin
90
+ # The class that the object is.
91
+ #
92
+ # @param [Class] target
93
+ # The class you wish to coerce to.
94
+ #
95
+ def initialize(origin, target, &block)
96
+ @origin = origin
97
+ @target = target
98
+ @block = block_given? ? block : PASS_THROUGH
99
+ end
100
+
101
+ # Calls the coercion.
102
+ #
103
+ # @return [Object]
104
+ def call(object)
105
+ @block.call(object, @target)
106
+ end
107
+ end
108
+ end
109
+
110
+ require_relative 'coercions/date_definitions'
111
+ require_relative 'coercions/date_time_definitions'
112
+ require_relative 'coercions/fixnum_definitions'
113
+ require_relative 'coercions/float_definitions'
114
+ require_relative 'coercions/integer_definitions'
115
+ require_relative 'coercions/string_definitions'
116
+ require_relative 'coercions/time_definitions'
117
+ require_relative 'coercions/hash_definitions'
118
+ require_relative 'coercions/boolean_definitions'
119
+
120
+ module Garcon
121
+ module Coercions
122
+ def self.bind_to(coercer)
123
+ Garcon::Coercions::DateDefinitions.bind_to(coercer)
124
+ Garcon::Coercions::DateTimeDefinitions.bind_to(coercer)
125
+ Garcon::Coercions::FixnumDefinitions.bind_to(coercer)
126
+ Garcon::Coercions::FloatDefinitions.bind_to(coercer)
127
+ Garcon::Coercions::IntegerDefinitions.bind_to(coercer)
128
+ Garcon::Coercions::StringDefinitions.bind_to(coercer)
129
+ Garcon::Coercions::TimeDefinitions.bind_to(coercer)
130
+ Garcon::Coercions::HashDefinitions.bind_to(coercer)
131
+ Garcon::Coercions::BooleanDefinitions.bind_to(coercer)
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,34 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Author: Stefano Harding <riddopic@gmail.com>
4
+ # License: Apache License, Version 2.0
5
+ # Copyright: (C) 2014-2015 Stefano Harding
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #
19
+
20
+ unless defined?(Boolean)
21
+ class Boolean
22
+ end
23
+ end
24
+
25
+ module Garcon
26
+ module Coercions
27
+ class BooleanDefinitions
28
+ def self.bind_to(coercer)
29
+ coercer.register(TrueClass, String) { |o, _| 'true' }
30
+ coercer.register(FalseClass, String) { |o, _| 'false' }
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,32 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Author: Stefano Harding <riddopic@gmail.com>
4
+ # License: Apache License, Version 2.0
5
+ # Copyright: (C) 2014-2015 Stefano Harding
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #
19
+
20
+ module Garcon
21
+ module Coercions
22
+ class DateDefinitions
23
+ def self.bind_to(coercer)
24
+ coercer.register(Date, Time) { |obj, _| obj.to_time }
25
+ coercer.register(Date, DateTime) { |obj, _| obj.to_datetime }
26
+ coercer.register(Date, Integer) { |obj, _| obj.to_time.to_i }
27
+ coercer.register(Date, Float) { |obj, _| obj.to_time.to_f }
28
+ coercer.register(Date, String) { |obj, _| obj.to_s }
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,32 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Author: Stefano Harding <riddopic@gmail.com>
4
+ # License: Apache License, Version 2.0
5
+ # Copyright: (C) 2014-2015 Stefano Harding
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #
19
+
20
+ module Garcon
21
+ module Coercions
22
+ class DateTimeDefinitions
23
+ def self.bind_to(coercer)
24
+ coercer.register(DateTime, Time) { |obj, _| obj.to_time }
25
+ coercer.register(DateTime, Date) { |obj, _| obj.to_date }
26
+ coercer.register(DateTime, Integer) { |obj, _| obj.to_time.to_i }
27
+ coercer.register(DateTime, Float) { |obj, _| obj.to_time.to_f }
28
+ coercer.register(DateTime, String) { |obj, _| obj.to_s }
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,34 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Author: Stefano Harding <riddopic@gmail.com>
4
+ # License: Apache License, Version 2.0
5
+ # Copyright: (C) 2014-2015 Stefano Harding
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #
19
+
20
+ module Garcon
21
+ module Coercions
22
+ class FixnumDefinitions
23
+ def self.bind_to(coercer)
24
+ coercer.register(Fixnum, String) { |obj, _| obj.to_s }
25
+ coercer.register(Fixnum, Time) { |obj, _| Time.at(obj) }
26
+ coercer.register(Fixnum, Date) { |obj, _| Time.at(obj).to_date }
27
+ coercer.register(Fixnum, DateTime) { |obj, _| Time.at(obj).to_datetime }
28
+ coercer.register(Fixnum, String) { |obj, _| obj.to_s }
29
+ coercer.register(Fixnum, Integer) { |obj, _| obj.to_i }
30
+ coercer.register(Fixnum, Float) { |obj, _| obj.to_f }
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,32 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Author: Stefano Harding <riddopic@gmail.com>
4
+ # License: Apache License, Version 2.0
5
+ # Copyright: (C) 2014-2015 Stefano Harding
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #
19
+
20
+ module Garcon
21
+ module Coercions
22
+ class FloatDefinitions
23
+ def self.bind_to(coercer)
24
+ coercer.register(Float, Time) { |obj, _| Time.at(obj) }
25
+ coercer.register(Float, Date) { |obj, _| Time.at(obj).to_date }
26
+ coercer.register(Float, DateTime) { |obj, _| Time.at(obj).to_datetime }
27
+ coercer.register(Float, String) { |obj, _| obj.to_s }
28
+ coercer.register(Float, Integer) { |obj, _| obj.to_i }
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,29 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Author: Stefano Harding <riddopic@gmail.com>
4
+ # License: Apache License, Version 2.0
5
+ # Copyright: (C) 2014-2015 Stefano Harding
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #
19
+
20
+ module Garcon
21
+ module Coercions
22
+ class HashDefinitions
23
+ def self.bind_to(coercer)
24
+ coercer.register(Hash, String) { |obj, _| obj.to_s }
25
+ coercer.register(Hash, Array) { |obj, _| obj.to_a }
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,31 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Author: Stefano Harding <riddopic@gmail.com>
4
+ # License: Apache License, Version 2.0
5
+ # Copyright: (C) 2014-2015 Stefano Harding
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #
19
+
20
+ module Garcon
21
+ module Coercions
22
+ class IntegerDefinitions
23
+ def self.bind_to(coercer)
24
+ coercer.register(Integer, Time) { |obj, _| Time.at(obj) }
25
+ coercer.register(Integer, Date) { |obj, _| Time.at(obj).to_date }
26
+ coercer.register(Integer, DateTime) { |obj, _| Time.at(obj).to_datetime }
27
+ coercer.register(Integer, String) { |obj, _| obj.to_s }
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,45 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Author: Stefano Harding <riddopic@gmail.com>
4
+ # License: Apache License, Version 2.0
5
+ # Copyright: (C) 2014-2015 Stefano Harding
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #
19
+
20
+ module Garcon
21
+ module Coercions
22
+ class StringDefinitions
23
+ def self.bind_to(coercer)
24
+ # Attempt to parse the date. If it can't be parsed just return nil.
25
+ # Silently failing is about the only thing I can think of.
26
+ type_parser = -> (obj, target) do
27
+ begin
28
+ target.parse(obj)
29
+ rescue ArgumentError
30
+ nil
31
+ end
32
+ end
33
+
34
+ coercer.register(String, Time, &type_parser)
35
+ coercer.register(String, Date, &type_parser)
36
+ coercer.register(String, DateTime, &type_parser)
37
+ coercer.register(String, Integer) { |obj, _| obj.to_i }
38
+ coercer.register(String, Float) { |obj, _| obj.to_f }
39
+ coercer.register(String, Boolean) do |string, _|
40
+ %w(1 on t true y yes).include?(string.downcase)
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,32 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Author: Stefano Harding <riddopic@gmail.com>
4
+ # License: Apache License, Version 2.0
5
+ # Copyright: (C) 2014-2015 Stefano Harding
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #
19
+
20
+ module Garcon
21
+ module Coercions
22
+ class TimeDefinitions
23
+ def self.bind_to(coercer)
24
+ coercer.register(Time, Date) { |obj, _| obj.to_date }
25
+ coercer.register(Time, DateTime) { |obj, _| obj.to_datetime }
26
+ coercer.register(Time, Integer) { |obj, _| obj.to_i }
27
+ coercer.register(Time, Float) { |obj, _| obj.to_f }
28
+ coercer.register(Time, String) { |obj, _| obj.to_s }
29
+ end
30
+ end
31
+ end
32
+ end