croesus 0.1.3

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 (73) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +121 -0
  3. data/.ruby-version +1 -0
  4. data/API_operation.txt +197 -0
  5. data/CHANGELOG.md +0 -0
  6. data/Gemfile +3 -0
  7. data/LICENSE.txt +0 -0
  8. data/README.md +146 -0
  9. data/Rakefile +26 -0
  10. data/bin/console_cmd.rb +133 -0
  11. data/croesus.gemspec +39 -0
  12. data/lib/croesus/associations.rb +46 -0
  13. data/lib/croesus/attribute.rb +41 -0
  14. data/lib/croesus/attributes.rb +93 -0
  15. data/lib/croesus/coerce.rb +110 -0
  16. data/lib/croesus/coercions/boolean_definitions.rb +32 -0
  17. data/lib/croesus/coercions/date_definitions.rb +30 -0
  18. data/lib/croesus/coercions/date_time_definitions.rb +30 -0
  19. data/lib/croesus/coercions/fixnum_definitions.rb +32 -0
  20. data/lib/croesus/coercions/float_definitions.rb +30 -0
  21. data/lib/croesus/coercions/hash_definitions.rb +27 -0
  22. data/lib/croesus/coercions/integer_definitions.rb +29 -0
  23. data/lib/croesus/coercions/string_definitions.rb +43 -0
  24. data/lib/croesus/coercions/time_definitions.rb +30 -0
  25. data/lib/croesus/core_ext/blank.rb +123 -0
  26. data/lib/croesus/core_ext/hash.rb +185 -0
  27. data/lib/croesus/dsl/dsl.rb +35 -0
  28. data/lib/croesus/dsl/helpers.rb +43 -0
  29. data/lib/croesus/dsl/mod_factory.rb +191 -0
  30. data/lib/croesus/dsl/resource_dsl.rb +59 -0
  31. data/lib/croesus/dsl/route_dsl.rb +42 -0
  32. data/lib/croesus/identity_map.rb +98 -0
  33. data/lib/croesus/platform.rb +47 -0
  34. data/lib/croesus/querying.rb +63 -0
  35. data/lib/croesus/resources/about.rb +15 -0
  36. data/lib/croesus/resources/basic_methods.rb +36 -0
  37. data/lib/croesus/resources/connectivity.rb +42 -0
  38. data/lib/croesus/resources/container.rb +135 -0
  39. data/lib/croesus/resources/fault.rb +38 -0
  40. data/lib/croesus/resources/fault_effect.rb +24 -0
  41. data/lib/croesus/resources/group.rb +37 -0
  42. data/lib/croesus/resources/host.rb +26 -0
  43. data/lib/croesus/resources/job.rb +27 -0
  44. data/lib/croesus/resources/namespace.rb +39 -0
  45. data/lib/croesus/resources/policy.rb +38 -0
  46. data/lib/croesus/resources/source.rb +86 -0
  47. data/lib/croesus/resources/source_config.rb +54 -0
  48. data/lib/croesus/resources/source_environment.rb +58 -0
  49. data/lib/croesus/resources/source_repository.rb +14 -0
  50. data/lib/croesus/resources/system_info.rb +21 -0
  51. data/lib/croesus/resources/timeflow.rb +40 -0
  52. data/lib/croesus/resources/timeflow_snapshot.rb +38 -0
  53. data/lib/croesus/utils.rb +262 -0
  54. data/lib/croesus/validations/many.rb +27 -0
  55. data/lib/croesus/validations/optional.rb +27 -0
  56. data/lib/croesus/validations.rb +91 -0
  57. data/lib/croesus/validators/base.rb +47 -0
  58. data/lib/croesus/validators/boolean_validator.rb +32 -0
  59. data/lib/croesus/validators/email_validator.rb +36 -0
  60. data/lib/croesus/validators/enumerable_validator.rb +40 -0
  61. data/lib/croesus/validators/hash_validator.rb +50 -0
  62. data/lib/croesus/validators/lambda_validator.rb +54 -0
  63. data/lib/croesus/validators/many_validator.rb +57 -0
  64. data/lib/croesus/validators/optional_validator.rb +41 -0
  65. data/lib/croesus/validators/presence_validator.rb +36 -0
  66. data/lib/croesus/validators/simple_type_validators.rb +38 -0
  67. data/lib/croesus/validators/simple_validator.rb +40 -0
  68. data/lib/croesus/version.rb +43 -0
  69. data/lib/croesus/web_client/web_client.rb +153 -0
  70. data/lib/croesus/web_client/web_request.rb +77 -0
  71. data/lib/croesus/web_client/web_response.rb +70 -0
  72. data/lib/croesus.rb +250 -0
  73. metadata +325 -0
@@ -0,0 +1,46 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Author: Stefano Harding <riddopic@gmail.com>
4
+ #
5
+ # Copyright (C) 2014 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
16
+ # implied. See the License for the specific language governing
17
+ # permissions and limitations under the License.
18
+ #
19
+
20
+ module Croesus::Associations
21
+
22
+ def has_many(resource, options = {})
23
+ class_name = options[:class_name] || "Croesus::#{resource.to_s.classify}"
24
+
25
+ class_eval do
26
+ define_method resource do
27
+ instance_variable_get("@#{resource}") ||
28
+ instance_variable_set("@#{resource}", Croesus::ResourceCollection.new(
29
+ class_name.constantize, self.satisfaction, options[:url]))
30
+ end
31
+ end
32
+ end
33
+
34
+ def belongs_to(resource, options = {})
35
+ class_name = options[:class_name] || "Croesus::#{resource.to_s.classify}"
36
+ parent_id = options[:parent_attribute] || "#{resource}_id"
37
+
38
+ class_eval do
39
+ define_method resource do
40
+ instance_variable_get("@#{resource}") ||
41
+ instance_variable_set("@#{resource}",
42
+ class_name.constantize.new(parent_id, self.satisfaction))
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,41 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Author: Stefano Harding <riddopic@gmail.com>
4
+ #
5
+ # Copyright (C) 2014 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
16
+ # implied. See the License for the specific language governing
17
+ # permissions and limitations under the License.
18
+ #
19
+
20
+ module Croesus
21
+ # A single attribute that is attached to a model
22
+ class Attribute
23
+ attr_reader :type
24
+
25
+ # @param type [Class] the class this attribute represents
26
+ # @param options [Hash] extra options that can be prescribed
27
+ def initialize(type = String, options = {})
28
+ @type = type
29
+
30
+ if options[:default].is_a?(Proc)
31
+ @default = options[:default]
32
+ else
33
+ @default = -> { options[:default] }
34
+ end
35
+ end
36
+
37
+ def default
38
+ @default.call
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,93 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Author: Stefano Harding <riddopic@gmail.com>
4
+ #
5
+ # Copyright (C) 2014 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
16
+ # implied. See the License for the specific language governing
17
+ # permissions and limitations under the License.
18
+ #
19
+
20
+ module Croesus
21
+ class Attributes
22
+ # @param params [Hash] the parameters you wish to initialize the model
23
+ # with. If the model does not have an accessor set, it will ignore
24
+ # the attribute passed.
25
+ def initialize(attributes = {})
26
+ self.attributes = attributes
27
+ end
28
+
29
+ # Sets the attributes on the model
30
+ # @param attributes [Hash]
31
+ def attributes=(attributes = {})
32
+ attributes.each do |attr, value|
33
+ self.send("#{attr}=", value) if self.respond_to?("#{attr}=")
34
+ end
35
+ end
36
+
37
+ # Get and ser the attributes on the model.
38
+ #
39
+ # @return [Hash]
40
+ def attributes
41
+ hash = {}
42
+ self.class.attributes.keys.each do |k|
43
+ hash[k] = send(k) if respond_to?(k)
44
+ end
45
+ hash
46
+ end
47
+
48
+ def to_h
49
+ self.attributes
50
+ end
51
+
52
+ def self.attributes
53
+ @attributes ||= {}
54
+ end
55
+
56
+ # @override
57
+ def self.attr_accessor(*args)
58
+ args.each { |name| self.attributes[name] = Attribute.new(Object) }
59
+ super
60
+ end
61
+
62
+ # Declares an attribute on the model
63
+ #
64
+ # @param name [String]
65
+ # @param type [Class] a class that represents the type
66
+ # @param options [Hash] extra options to apply to the attribute
67
+ def self.attribute(name, type=String, options={})
68
+ config = { writer: true, reader: true }.merge(options)
69
+
70
+ attr = Attribute.new(type, options)
71
+ self.attributes[name] = attr
72
+ variable = "@#{name}"
73
+
74
+ if config[:writer]
75
+ define_method("#{name}=") do |object|
76
+ val = Croesus.coercer.coerce(object, attr.type)
77
+ instance_variable_set(variable, val)
78
+ end
79
+ end
80
+
81
+ if config[:reader]
82
+ define_method(name) do
83
+ val = instance_variable_get(variable)
84
+ unless val
85
+ val = attr.default
86
+ instance_variable_set(variable, val)
87
+ end
88
+ val
89
+ end
90
+ end
91
+ end # def self.attribute
92
+ end # class Attributes
93
+ end # module Croesus
@@ -0,0 +1,110 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Author: Stefano Harding <riddopic@gmail.com>
4
+ #
5
+ # Copyright (C) 2014 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
16
+ # implied. See the License for the specific language governing
17
+ # permissions and limitations under the License.
18
+ #
19
+
20
+ module Croesus
21
+ # The class that coerces objects based on the definitions that are registered
22
+ # with it.
23
+ class Coercer
24
+ def initialize
25
+ @coercions = Hash.new do |hash, origin|
26
+ hash[origin] = Hash.new do |h, target|
27
+ h[target] = Coercion.new(origin, target)
28
+ end
29
+ end
30
+ @mutex = Mutex.new
31
+ end
32
+
33
+ # Registers a coercion with the Croesus library
34
+ #
35
+ # @param origin [Class] the class to convert
36
+ # @param target [Class] what the origin will be converted to
37
+ def register(origin, target, &block)
38
+ raise(ArgumentError, 'block is required') unless block_given?
39
+
40
+ @mutex.synchronize do
41
+ @coercions[origin][target] = Coercion.new(origin, target, &block)
42
+ end
43
+ end
44
+
45
+ # Removes a coercion from the library
46
+ #
47
+ # @param origin [Class]
48
+ # @param target [Class]
49
+ def unregister(origin, target)
50
+ @mutex.synchronize do
51
+ @coercions[origin].delete(target)
52
+ end
53
+ end
54
+
55
+ # @param object [Object] the object to coerce
56
+ # @param target [Class] what you want the object to turn in to
57
+ def coerce(object, target)
58
+ @mutex.synchronize do
59
+ @coercions[object.class][target].call(object)
60
+ end
61
+ end
62
+ end
63
+
64
+ # This wraps the block that is provided when you register a coercion.
65
+ class Coercion
66
+ # Just passes the object on through
67
+ PASS_THROUGH = ->(obj, _) { obj }
68
+
69
+ # @param origin [Class] the class that the object is
70
+ # @param target [Class] the class you wish to coerce to
71
+ def initialize(origin, target, &block)
72
+ @origin = origin
73
+ @target = target
74
+ @block = block_given? ? block : PASS_THROUGH
75
+ end
76
+
77
+ # Calls the coercion
78
+ #
79
+ # @return [Object]
80
+ def call(object)
81
+ @block.call(object, @target)
82
+ end
83
+ end
84
+ end
85
+
86
+ %w(
87
+ date_definitions
88
+ date_time_definitions
89
+ fixnum_definitions
90
+ float_definitions
91
+ integer_definitions
92
+ string_definitions
93
+ time_definitions
94
+ hash_definitions
95
+ boolean_definitions
96
+ ).each { |file| require File.join('croesus/coercions/', file) }
97
+
98
+ module Croesus::Coercions
99
+ def self.bind_to(coercer)
100
+ Croesus::Coercions::DateDefinitions.bind_to(coercer)
101
+ Croesus::Coercions::DateTimeDefinitions.bind_to(coercer)
102
+ Croesus::Coercions::FixnumDefinitions.bind_to(coercer)
103
+ Croesus::Coercions::FloatDefinitions.bind_to(coercer)
104
+ Croesus::Coercions::IntegerDefinitions.bind_to(coercer)
105
+ Croesus::Coercions::StringDefinitions.bind_to(coercer)
106
+ Croesus::Coercions::TimeDefinitions.bind_to(coercer)
107
+ Croesus::Coercions::HashDefinitions.bind_to(coercer)
108
+ Croesus::Coercions::BooleanDefinitions.bind_to(coercer)
109
+ end
110
+ end
@@ -0,0 +1,32 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Author: Stefano Harding <riddopic@gmail.com>
4
+ #
5
+ # Copyright (C) 2014 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
16
+ # implied. See the License for the specific language governing
17
+ # permissions and limitations under the License.
18
+ #
19
+
20
+ unless defined?(Boolean)
21
+ class Boolean
22
+ end
23
+ end
24
+
25
+ module Croesus::Coercions
26
+ class BooleanDefinitions
27
+ def self.bind_to(coercer)
28
+ coercer.register(TrueClass, String) { |o, _| 'true' }
29
+ coercer.register(FalseClass, String) { |o, _| 'false' }
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,30 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Author: Stefano Harding <riddopic@gmail.com>
4
+ #
5
+ # Copyright (C) 2014 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
16
+ # implied. See the License for the specific language governing
17
+ # permissions and limitations under the License.
18
+ #
19
+
20
+ module Croesus::Coercions
21
+ class DateDefinitions
22
+ def self.bind_to(coercer)
23
+ coercer.register(Date, Time) { |obj, _| obj.to_time }
24
+ coercer.register(Date, DateTime) { |obj, _| obj.to_datetime }
25
+ coercer.register(Date, Integer) { |obj, _| obj.to_time.to_i }
26
+ coercer.register(Date, Float) { |obj, _| obj.to_time.to_f }
27
+ coercer.register(Date, String) { |obj, _| obj.to_s }
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,30 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Author: Stefano Harding <riddopic@gmail.com>
4
+ #
5
+ # Copyright (C) 2014 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
16
+ # implied. See the License for the specific language governing
17
+ # permissions and limitations under the License.
18
+ #
19
+
20
+ module Croesus::Coercions
21
+ class DateTimeDefinitions
22
+ def self.bind_to(coercer)
23
+ coercer.register(DateTime, Time) { |obj, _| obj.to_time }
24
+ coercer.register(DateTime, Date) { |obj, _| obj.to_date }
25
+ coercer.register(DateTime, Integer) { |obj, _| obj.to_time.to_i }
26
+ coercer.register(DateTime, Float) { |obj, _| obj.to_time.to_f }
27
+ coercer.register(DateTime, String) { |obj, _| obj.to_s }
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,32 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Author: Stefano Harding <riddopic@gmail.com>
4
+ #
5
+ # Copyright (C) 2014 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
16
+ # implied. See the License for the specific language governing
17
+ # permissions and limitations under the License.
18
+ #
19
+
20
+ module Croesus::Coercions
21
+ class FixnumDefinitions
22
+ def self.bind_to(coercer)
23
+ coercer.register(Fixnum, String) { |obj, _| obj.to_s }
24
+ coercer.register(Fixnum, Time) { |obj, _| Time.at(obj) }
25
+ coercer.register(Fixnum, Date) { |obj, _| Time.at(obj).to_date }
26
+ coercer.register(Fixnum, DateTime) { |obj, _| Time.at(obj).to_datetime }
27
+ coercer.register(Fixnum, String) { |obj, _| obj.to_s }
28
+ coercer.register(Fixnum, Integer) { |obj, _| obj.to_i }
29
+ coercer.register(Fixnum, Float) { |obj, _| obj.to_f }
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,30 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Author: Stefano Harding <riddopic@gmail.com>
4
+ #
5
+ # Copyright (C) 2014 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
16
+ # implied. See the License for the specific language governing
17
+ # permissions and limitations under the License.
18
+ #
19
+
20
+ module Croesus::Coercions
21
+ class FloatDefinitions
22
+ def self.bind_to(coercer)
23
+ coercer.register(Float, Time) { |obj, _| Time.at(obj) }
24
+ coercer.register(Float, Date) { |obj, _| Time.at(obj).to_date }
25
+ coercer.register(Float, DateTime) { |obj, _| Time.at(obj).to_datetime }
26
+ coercer.register(Float, String) { |obj, _| obj.to_s }
27
+ coercer.register(Float, Integer) { |obj, _| obj.to_i }
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,27 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Author: Stefano Harding <riddopic@gmail.com>
4
+ #
5
+ # Copyright (C) 2014 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
16
+ # implied. See the License for the specific language governing
17
+ # permissions and limitations under the License.
18
+ #
19
+
20
+ module Croesus::Coercions
21
+ class HashDefinitions
22
+ def self.bind_to(coercer)
23
+ coercer.register(Hash, String) { |obj, _| obj.to_s }
24
+ coercer.register(Hash, Array) { |obj, _| obj.to_a }
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,29 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Author: Stefano Harding <riddopic@gmail.com>
4
+ #
5
+ # Copyright (C) 2014 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
16
+ # implied. See the License for the specific language governing
17
+ # permissions and limitations under the License.
18
+ #
19
+
20
+ module Croesus::Coercions
21
+ class IntegerDefinitions
22
+ def self.bind_to(coercer)
23
+ coercer.register(Integer, Time) { |obj, _| Time.at(obj) }
24
+ coercer.register(Integer, Date) { |obj, _| Time.at(obj).to_date }
25
+ coercer.register(Integer, DateTime) { |obj, _| Time.at(obj).to_datetime }
26
+ coercer.register(Integer, String) { |obj, _| obj.to_s }
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,43 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Author: Stefano Harding <riddopic@gmail.com>
4
+ #
5
+ # Copyright (C) 2014 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
16
+ # implied. See the License for the specific language governing
17
+ # permissions and limitations under the License.
18
+ #
19
+
20
+ module Croesus::Coercions
21
+ class StringDefinitions
22
+ def self.bind_to(coercer)
23
+ # Attempt to parse the date. If it can't be parsed just return nil.
24
+ # Silently failing is about the only thing I can think of.
25
+ type_parser = -> (obj, target) do
26
+ begin
27
+ target.parse(obj)
28
+ rescue ArgumentError
29
+ nil
30
+ end
31
+ end
32
+
33
+ coercer.register(String, Time, &type_parser)
34
+ coercer.register(String, Date, &type_parser)
35
+ coercer.register(String, DateTime, &type_parser)
36
+ coercer.register(String, Integer) { |obj, _| obj.to_i }
37
+ coercer.register(String, Float) { |obj, _| obj.to_f }
38
+ coercer.register(String, Boolean) do |string, _|
39
+ %w(1 on t true y yes).include?(string.downcase)
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,30 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Author: Stefano Harding <riddopic@gmail.com>
4
+ #
5
+ # Copyright (C) 2014 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
16
+ # implied. See the License for the specific language governing
17
+ # permissions and limitations under the License.
18
+ #
19
+
20
+ module Croesus::Coercions
21
+ class TimeDefinitions
22
+ def self.bind_to(coercer)
23
+ coercer.register(Time, Date) { |obj, _| obj.to_date }
24
+ coercer.register(Time, DateTime) { |obj, _| obj.to_datetime }
25
+ coercer.register(Time, Integer) { |obj, _| obj.to_i }
26
+ coercer.register(Time, Float) { |obj, _| obj.to_f }
27
+ coercer.register(Time, String) { |obj, _| obj.to_s }
28
+ end
29
+ end
30
+ end