bronze 0.0.1.alpha → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'date'
4
+
5
+ require 'bronze/transform'
6
+ require 'bronze/transforms/attributes'
7
+
8
+ module Bronze::Transforms::Attributes
9
+ # Transform class that normalizes a Date to a formatted string representation.
10
+ class DateTransform < Bronze::Transform
11
+ # Format string for ISO 8601 date format. Equivalent to YYYY-MM-DD.
12
+ ISO_8601 = '%F'
13
+
14
+ # @return [DateTransform] a memoized instance of DateTransform.
15
+ def self.instance
16
+ @instance ||= new
17
+ end
18
+
19
+ # @param format [String] The format string used to normalize and denormalize
20
+ # dates. The default is ISO 8601 format.
21
+ def initialize(format = ISO_8601)
22
+ @format = format
23
+ end
24
+
25
+ # @return [String] the format string.
26
+ attr_reader :format
27
+
28
+ # Converts a formatted Date string to a Date instance.
29
+ #
30
+ # @param value [String] The normalized string.
31
+ #
32
+ # @return [Date] the parsed date.
33
+ def denormalize(value)
34
+ return value if value.is_a?(Date)
35
+
36
+ return nil if value.nil? || value.empty?
37
+
38
+ Date.strptime(value, read_format)
39
+ end
40
+
41
+ # Converts a Date to a formatted string.
42
+ #
43
+ # @param value [Date] The Date to format.
44
+ #
45
+ # @return [String] the formatted string.
46
+ def normalize(value)
47
+ return nil if value.nil?
48
+
49
+ value.strftime(format)
50
+ end
51
+
52
+ private
53
+
54
+ def read_format
55
+ @read_format ||= format.gsub('%-', '%')
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bronze/transform'
4
+ require 'bronze/transforms/attributes'
5
+
6
+ module Bronze::Transforms::Attributes
7
+ # Transform class that converts a Symbol to a string.
8
+ class SymbolTransform < Bronze::Transform
9
+ # @return [SymbolTransform] a memoized instance of SymbolTransform.
10
+ def self.instance
11
+ @instance ||= new
12
+ end
13
+
14
+ # Converts a normalized String to a Symbol.
15
+ #
16
+ # @param value [String] The normalized string.
17
+ #
18
+ # @return [Symbol] the denormalized symbol.
19
+ def denormalize(value)
20
+ return nil if value.nil?
21
+
22
+ value.intern
23
+ end
24
+
25
+ # Converts a Symbol to a string.
26
+ #
27
+ # @param value [Symbol] The Symbol to normalize.
28
+ #
29
+ # @return [String] the string representation.
30
+ def normalize(value)
31
+ return nil if value.nil?
32
+
33
+ value.to_s
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'date'
4
+
5
+ require 'bronze/transform'
6
+ require 'bronze/transforms/attributes'
7
+
8
+ module Bronze::Transforms::Attributes
9
+ # Transform class that converts a Time to an integer timestamp.
10
+ class TimeTransform < Bronze::Transform
11
+ # @return [TimeTransform] a memoized instance of TimeTransform.
12
+ def self.instance
13
+ @instance ||= new
14
+ end
15
+
16
+ # Converts an integer timestamp to a Time.
17
+ #
18
+ # @param value [Integer] The integer timestamp.
19
+ #
20
+ # @return [Time] the Time corresponding to the timestamp.
21
+ def denormalize(value)
22
+ return nil if value.nil?
23
+
24
+ Time.at(value).utc
25
+ end
26
+
27
+ # Converts a Time to an integer timestamp.
28
+ #
29
+ # @param value [Time] The Time to normalize.
30
+ #
31
+ # @return [Integer] the integer timestamp.
32
+ def normalize(value)
33
+ return nil if value.nil?
34
+
35
+ value.to_i
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bronze/transforms'
4
+
5
+ module Bronze::Transforms
6
+ # Namespace for defining entity transforms, which convert entities to a
7
+ # normalized form
8
+ module Entities; end
9
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bronze/transform'
4
+ require 'bronze/transforms/entities'
5
+
6
+ module Bronze::Transforms::Entities
7
+ # Transform class that maps an entity to a normal representation.
8
+ class NormalizeTransform < Bronze::Transform
9
+ # @param [Class] entity_class The entity class to normalize.
10
+ # @param [Array<Class>] permit An optional array of types to normalize
11
+ # as-is, rather than applying a transform. Only default transforms can be
12
+ # permitted, i.e. the built-in default transforms for BigDecimal, Date,
13
+ # DateTime, Symbol, and Time, or for an attribute with the
14
+ # :default_transform flag set to true.
15
+ def initialize(entity_class, permit: [])
16
+ @entity_class = entity_class
17
+ @permitted_types = permit
18
+ end
19
+
20
+ # @return [Class] the entity class to normalize.
21
+ attr_reader :entity_class
22
+
23
+ # @return [Array<Class>] array of types to normalize as-is.
24
+ attr_reader :permitted_types
25
+
26
+ # Returns an entity instance.
27
+ #
28
+ # @param attributes [Hash] The normal representation of an entity.
29
+ #
30
+ # @return [Bronze::Entity] the entity instance.
31
+ #
32
+ # @see [Bronze::Entities::Normalization::denormalize]
33
+ def denormalize(attributes)
34
+ return nil if attributes.nil?
35
+
36
+ entity_class.denormalize(attributes)
37
+ end
38
+
39
+ # Returns a normalized representation of the entity.
40
+ #
41
+ # @param entity [Bronze::Entity] The entity to normalize.
42
+ #
43
+ # @return [Hash] the normal representation.
44
+ #
45
+ # @see [Bronze::Entities::Normalization#normalize]
46
+ def normalize(entity)
47
+ return nil if entity.nil?
48
+
49
+ entity.normalize(permit: permitted_types)
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bronze/transform'
4
+
5
+ module Bronze::Transforms
6
+ # Transform class that maps an object to itself.
7
+ class IdentityTransform < Bronze::Transform
8
+ # @return [IdentityTransform] a memoized instance of IdentityTransform.
9
+ def self.instance
10
+ @instance ||= new
11
+ end
12
+
13
+ # Returns the object that was passed in to the method.
14
+ #
15
+ # @param object [Object] The object to denormalize.
16
+ #
17
+ # @return [Object] the object passed in.
18
+ def denormalize(object)
19
+ object
20
+ end
21
+
22
+ # Returns the object that was passed in to the method.
23
+ #
24
+ # @param object [Object] The object to normalize.
25
+ #
26
+ # @return [Object] the object passed in.
27
+ def normalize(object)
28
+ object
29
+ end
30
+ end
31
+ end
@@ -1,4 +1,4 @@
1
- # lib/bronze/version.rb
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Bronze
4
4
  # @api private
@@ -10,11 +10,11 @@ module Bronze
10
10
  # Major version.
11
11
  MAJOR = 0
12
12
  # Minor version.
13
- MINOR = 0
13
+ MINOR = 1
14
14
  # Patch version.
15
- PATCH = 1
15
+ PATCH = 0
16
16
  # Prerelease version.
17
- PRERELEASE = :alpha
17
+ PRERELEASE = nil
18
18
  # Build metadata.
19
19
  BUILD = nil
20
20
 
@@ -27,7 +27,7 @@ module Bronze
27
27
  #
28
28
  # @see SleepingKingStudios::Tools::SemanticVersion#to_gem_version
29
29
  def to_gem_version
30
- str = "#{MAJOR}.#{MINOR}.#{PATCH}"
30
+ str = +"#{MAJOR}.#{MINOR}.#{PATCH}"
31
31
 
32
32
  prerelease = value_of(:PRERELEASE)
33
33
  str << ".#{prerelease}" if prerelease
@@ -36,11 +36,11 @@ module Bronze
36
36
  str << ".#{build}" if build
37
37
 
38
38
  str
39
- end # class method to_version
39
+ end
40
40
 
41
41
  private
42
42
 
43
- def value_of constant
43
+ def value_of(constant)
44
44
  return nil unless const_defined?(constant)
45
45
 
46
46
  value = const_get(constant)
@@ -48,9 +48,10 @@ module Bronze
48
48
  return nil if value.respond_to?(:empty?) && value.empty?
49
49
 
50
50
  value
51
- end # method value_of
52
- end # eigenclass
53
- end # module
51
+ end
52
+ end
53
+ end
54
54
 
55
+ # The current version of the gem.
55
56
  VERSION = Version.to_gem_version
56
- end # module
57
+ end
metadata CHANGED
@@ -1,144 +1,174 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bronze
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.alpha
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rob "Merlin" Smith
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-11-10 00:00:00.000000000 Z
11
+ date: 2019-02-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: sysrandom
14
+ name: sleeping_king_studios-tools
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 1.0.0
19
+ version: '0.7'
20
20
  - - ">="
21
21
  - !ruby/object:Gem::Version
22
- version: 1.0.2
22
+ version: 0.7.1
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
27
  - - "~>"
28
28
  - !ruby/object:Gem::Version
29
- version: 1.0.0
29
+ version: '0.7'
30
30
  - - ">="
31
31
  - !ruby/object:Gem::Version
32
- version: 1.0.2
32
+ version: 0.7.1
33
33
  - !ruby/object:Gem::Dependency
34
- name: sleeping_king_studios-tools
34
+ name: rspec
35
35
  requirement: !ruby/object:Gem::Requirement
36
36
  requirements:
37
37
  - - "~>"
38
38
  - !ruby/object:Gem::Version
39
- version: 0.5.0
40
- type: :runtime
39
+ version: '3.8'
40
+ type: :development
41
41
  prerelease: false
42
42
  version_requirements: !ruby/object:Gem::Requirement
43
43
  requirements:
44
44
  - - "~>"
45
45
  - !ruby/object:Gem::Version
46
- version: 0.5.0
46
+ version: '3.8'
47
47
  - !ruby/object:Gem::Dependency
48
- name: thor
48
+ name: rspec-sleeping_king_studios
49
49
  requirement: !ruby/object:Gem::Requirement
50
50
  requirements:
51
51
  - - "~>"
52
52
  - !ruby/object:Gem::Version
53
- version: '0.19'
54
- - - ">="
55
- - !ruby/object:Gem::Version
56
- version: 0.19.1
53
+ version: '2.5'
57
54
  type: :development
58
55
  prerelease: false
59
56
  version_requirements: !ruby/object:Gem::Requirement
60
57
  requirements:
61
58
  - - "~>"
62
59
  - !ruby/object:Gem::Version
63
- version: '0.19'
64
- - - ">="
65
- - !ruby/object:Gem::Version
66
- version: 0.19.1
60
+ version: '2.5'
67
61
  - !ruby/object:Gem::Dependency
68
- name: rspec
62
+ name: rubocop
69
63
  requirement: !ruby/object:Gem::Requirement
70
64
  requirements:
71
65
  - - "~>"
72
66
  - !ruby/object:Gem::Version
73
- version: '3.5'
67
+ version: 0.63.0
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: 0.63.1
74
71
  type: :development
75
72
  prerelease: false
76
73
  version_requirements: !ruby/object:Gem::Requirement
77
74
  requirements:
78
75
  - - "~>"
79
76
  - !ruby/object:Gem::Version
80
- version: '3.5'
77
+ version: 0.63.0
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: 0.63.1
81
81
  - !ruby/object:Gem::Dependency
82
- name: rubocop
82
+ name: rubocop-rspec
83
83
  requirement: !ruby/object:Gem::Requirement
84
84
  requirements:
85
85
  - - "~>"
86
86
  - !ruby/object:Gem::Version
87
- version: '0.43'
87
+ version: 1.32.0
88
88
  type: :development
89
89
  prerelease: false
90
90
  version_requirements: !ruby/object:Gem::Requirement
91
91
  requirements:
92
92
  - - "~>"
93
93
  - !ruby/object:Gem::Version
94
- version: '0.43'
94
+ version: 1.32.0
95
95
  - !ruby/object:Gem::Dependency
96
96
  name: simplecov
97
97
  requirement: !ruby/object:Gem::Requirement
98
98
  requirements:
99
99
  - - "~>"
100
100
  - !ruby/object:Gem::Version
101
- version: '0.12'
101
+ version: '0.16'
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: 0.16.1
102
105
  type: :development
103
106
  prerelease: false
104
107
  version_requirements: !ruby/object:Gem::Requirement
105
108
  requirements:
106
109
  - - "~>"
107
110
  - !ruby/object:Gem::Version
108
- version: '0.12'
111
+ version: '0.16'
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: 0.16.1
109
115
  - !ruby/object:Gem::Dependency
110
- name: rspec-sleeping_king_studios
116
+ name: thor
111
117
  requirement: !ruby/object:Gem::Requirement
112
118
  requirements:
113
119
  - - "~>"
114
120
  - !ruby/object:Gem::Version
115
- version: '2.2'
121
+ version: '0.20'
116
122
  - - ">="
117
123
  - !ruby/object:Gem::Version
118
- version: 2.2.1
124
+ version: 0.20.3
119
125
  type: :development
120
126
  prerelease: false
121
127
  version_requirements: !ruby/object:Gem::Requirement
122
128
  requirements:
123
129
  - - "~>"
124
130
  - !ruby/object:Gem::Version
125
- version: '2.2'
131
+ version: '0.20'
126
132
  - - ">="
127
133
  - !ruby/object:Gem::Version
128
- version: 2.2.1
129
- description: A set of objects and tools for building composable applications without
130
- the conceptual overhead of common frameworks. Allows the developer to incorporate
131
- other architectures by composition without forcing foreign constraints on your business
132
- logic.
134
+ version: 0.20.3
135
+ description: |-
136
+ A composable application toolkit, providing data entities and collections,
137
+ transforms, contract-based validations and pre-built operations.
138
+ Architecture agnostic for easy integration with other toolkits or
139
+ frameworks.
133
140
  email:
134
141
  - merlin@sleepingkingstudios.com
135
142
  executables: []
136
143
  extensions: []
137
144
  extra_rdoc_files: []
138
145
  files:
146
+ - CHANGELOG.md
147
+ - DEVELOPMENT.md
139
148
  - LICENSE
140
149
  - README.md
141
150
  - lib/bronze.rb
151
+ - lib/bronze/entities.rb
152
+ - lib/bronze/entities/attributes.rb
153
+ - lib/bronze/entities/attributes/builder.rb
154
+ - lib/bronze/entities/attributes/metadata.rb
155
+ - lib/bronze/entities/normalization.rb
156
+ - lib/bronze/entities/primary_key.rb
157
+ - lib/bronze/entities/primary_keys.rb
158
+ - lib/bronze/entities/primary_keys/uuid.rb
159
+ - lib/bronze/entity.rb
160
+ - lib/bronze/not_implemented_error.rb
161
+ - lib/bronze/transform.rb
162
+ - lib/bronze/transforms.rb
163
+ - lib/bronze/transforms/attributes.rb
164
+ - lib/bronze/transforms/attributes/big_decimal_transform.rb
165
+ - lib/bronze/transforms/attributes/date_time_transform.rb
166
+ - lib/bronze/transforms/attributes/date_transform.rb
167
+ - lib/bronze/transforms/attributes/symbol_transform.rb
168
+ - lib/bronze/transforms/attributes/time_transform.rb
169
+ - lib/bronze/transforms/entities.rb
170
+ - lib/bronze/transforms/entities/normalize_transform.rb
171
+ - lib/bronze/transforms/identity_transform.rb
142
172
  - lib/bronze/version.rb
143
173
  homepage: http://sleepingkingstudios.com
144
174
  licenses:
@@ -155,12 +185,11 @@ required_ruby_version: !ruby/object:Gem::Requirement
155
185
  version: '0'
156
186
  required_rubygems_version: !ruby/object:Gem::Requirement
157
187
  requirements:
158
- - - ">"
188
+ - - ">="
159
189
  - !ruby/object:Gem::Version
160
- version: 1.3.1
190
+ version: '0'
161
191
  requirements: []
162
- rubyforge_project:
163
- rubygems_version: 2.5.1
192
+ rubygems_version: 3.0.1
164
193
  signing_key:
165
194
  specification_version: 4
166
195
  summary: A composable application framework.