shale 0.3.1 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +37 -0
  3. data/README.md +398 -41
  4. data/exe/shaleb +108 -36
  5. data/lib/shale/adapter/nokogiri/document.rb +97 -0
  6. data/lib/shale/adapter/nokogiri/node.rb +100 -0
  7. data/lib/shale/adapter/nokogiri.rb +11 -151
  8. data/lib/shale/adapter/ox/document.rb +90 -0
  9. data/lib/shale/adapter/ox/node.rb +97 -0
  10. data/lib/shale/adapter/ox.rb +9 -134
  11. data/lib/shale/adapter/rexml/document.rb +98 -0
  12. data/lib/shale/adapter/rexml/node.rb +99 -0
  13. data/lib/shale/adapter/rexml.rb +9 -150
  14. data/lib/shale/adapter/toml_rb.rb +34 -0
  15. data/lib/shale/attribute.rb +6 -0
  16. data/lib/shale/error.rb +56 -0
  17. data/lib/shale/mapper.rb +67 -13
  18. data/lib/shale/mapping/descriptor/xml.rb +10 -1
  19. data/lib/shale/mapping/dict.rb +18 -0
  20. data/lib/shale/mapping/xml.rb +40 -5
  21. data/lib/shale/schema/compiler/boolean.rb +21 -0
  22. data/lib/shale/schema/compiler/complex.rb +88 -0
  23. data/lib/shale/schema/compiler/date.rb +21 -0
  24. data/lib/shale/schema/compiler/float.rb +21 -0
  25. data/lib/shale/schema/compiler/integer.rb +21 -0
  26. data/lib/shale/schema/compiler/property.rb +70 -0
  27. data/lib/shale/schema/compiler/string.rb +21 -0
  28. data/lib/shale/schema/compiler/time.rb +21 -0
  29. data/lib/shale/schema/compiler/value.rb +21 -0
  30. data/lib/shale/schema/compiler/xml_complex.rb +50 -0
  31. data/lib/shale/schema/compiler/xml_property.rb +73 -0
  32. data/lib/shale/schema/json_compiler.rb +331 -0
  33. data/lib/shale/schema/{json → json_generator}/base.rb +2 -2
  34. data/lib/shale/schema/{json → json_generator}/boolean.rb +1 -1
  35. data/lib/shale/schema/{json → json_generator}/collection.rb +2 -2
  36. data/lib/shale/schema/{json → json_generator}/date.rb +1 -1
  37. data/lib/shale/schema/{json → json_generator}/float.rb +1 -1
  38. data/lib/shale/schema/{json → json_generator}/integer.rb +1 -1
  39. data/lib/shale/schema/{json → json_generator}/object.rb +5 -2
  40. data/lib/shale/schema/{json → json_generator}/ref.rb +1 -1
  41. data/lib/shale/schema/{json → json_generator}/schema.rb +6 -4
  42. data/lib/shale/schema/{json → json_generator}/string.rb +1 -1
  43. data/lib/shale/schema/{json → json_generator}/time.rb +1 -1
  44. data/lib/shale/schema/json_generator/value.rb +23 -0
  45. data/lib/shale/schema/{json.rb → json_generator.rb} +36 -36
  46. data/lib/shale/schema/xml_compiler.rb +919 -0
  47. data/lib/shale/schema/{xml → xml_generator}/attribute.rb +1 -1
  48. data/lib/shale/schema/{xml → xml_generator}/complex_type.rb +5 -2
  49. data/lib/shale/schema/{xml → xml_generator}/element.rb +1 -1
  50. data/lib/shale/schema/{xml → xml_generator}/import.rb +1 -1
  51. data/lib/shale/schema/{xml → xml_generator}/ref_attribute.rb +1 -1
  52. data/lib/shale/schema/{xml → xml_generator}/ref_element.rb +1 -1
  53. data/lib/shale/schema/{xml → xml_generator}/schema.rb +5 -5
  54. data/lib/shale/schema/{xml → xml_generator}/typed_attribute.rb +1 -1
  55. data/lib/shale/schema/{xml → xml_generator}/typed_element.rb +1 -1
  56. data/lib/shale/schema/{xml.rb → xml_generator.rb} +25 -26
  57. data/lib/shale/schema.rb +44 -5
  58. data/lib/shale/type/{composite.rb → complex.rb} +156 -51
  59. data/lib/shale/type/value.rb +31 -2
  60. data/lib/shale/utils.rb +42 -7
  61. data/lib/shale/version.rb +1 -1
  62. data/lib/shale.rb +22 -19
  63. data/shale.gemspec +3 -3
  64. metadata +50 -29
data/lib/shale/error.rb CHANGED
@@ -1,6 +1,38 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Shale
4
+ # Error message displayed when TOML adapter is not set
5
+ # @api private
6
+ TOML_ADAPTER_NOT_SET_MESSAGE = <<~MSG
7
+ TOML Adapter is not set.
8
+ To use Shale with TOML documents you have to install parser and set adapter.
9
+
10
+ # Make sure toml-rb is installed eg. execute: gem install toml-rb
11
+ require 'shale/adapter/toml_rb'
12
+ Shale.toml_adapter = Shale::Adapter::TomlRB
13
+ MSG
14
+
15
+ # Error message displayed when XML adapter is not set
16
+ # @api private
17
+ XML_ADAPTER_NOT_SET_MESSAGE = <<~MSG
18
+ XML Adapter is not set.
19
+ To use Shale with XML documents you have to install parser and set adapter.
20
+
21
+ To use REXML:
22
+ require 'shale/adapter/rexml'
23
+ Shale.xml_adapter = Shale::Adapter::REXML
24
+
25
+ To use Nokogiri:
26
+ # Make sure Nokogiri is installed eg. execute: gem install nokogiri
27
+ require 'shale/adapter/nokogiri'
28
+ Shale.xml_adapter = Shale::Adapter::Nokogiri
29
+
30
+ To use OX:
31
+ # Make sure Ox is installed eg. execute: gem install ox
32
+ require 'shale/adapter/ox'
33
+ Shale.xml_adapter = Shale::Adapter::Ox
34
+ MSG
35
+
4
36
  # Error for assigning value to not existing attribute
5
37
  #
6
38
  # @api private
@@ -31,6 +63,12 @@ module Shale
31
63
  end
32
64
  end
33
65
 
66
+ # Error for passing incorrect model type
67
+ #
68
+ # @api private
69
+ class IncorrectModelError < StandardError
70
+ end
71
+
34
72
  # Error for passing incorrect arguments to map functions
35
73
  #
36
74
  # @api private
@@ -42,4 +80,22 @@ module Shale
42
80
  # @api private
43
81
  class NotAShaleMapperError < StandardError
44
82
  end
83
+
84
+ # Schema compilation error
85
+ #
86
+ # @api private
87
+ class SchemaError < StandardError
88
+ end
89
+
90
+ # Parsing error
91
+ #
92
+ # @api private
93
+ class ParseError < StandardError
94
+ end
95
+
96
+ # Adapter error
97
+ #
98
+ # @api private
99
+ class AdapterError < StandardError
100
+ end
45
101
  end
data/lib/shale/mapper.rb CHANGED
@@ -5,7 +5,7 @@ require_relative 'error'
5
5
  require_relative 'utils'
6
6
  require_relative 'mapping/dict'
7
7
  require_relative 'mapping/xml'
8
- require_relative 'type/composite'
8
+ require_relative 'type/complex'
9
9
 
10
10
  module Shale
11
11
  # Base class used for mapping
@@ -42,11 +42,13 @@ module Shale
42
42
  # person.to_json
43
43
  #
44
44
  # @api public
45
- class Mapper < Type::Composite
45
+ class Mapper < Type::Complex
46
+ @model = nil
46
47
  @attributes = {}
47
48
  @hash_mapping = Mapping::Dict.new
48
49
  @json_mapping = Mapping::Dict.new
49
50
  @yaml_mapping = Mapping::Dict.new
51
+ @toml_mapping = Mapping::Dict.new
50
52
  @xml_mapping = Mapping::Xml.new
51
53
 
52
54
  class << self
@@ -78,6 +80,13 @@ module Shale
78
80
  # @api public
79
81
  attr_reader :yaml_mapping
80
82
 
83
+ # Return TOML mapping object
84
+ #
85
+ # @return [Shale::Mapping::Dict]
86
+ #
87
+ # @api public
88
+ attr_reader :toml_mapping
89
+
81
90
  # Return XML mapping object
82
91
  #
83
92
  # @return [Shale::Mapping::XML]
@@ -88,16 +97,20 @@ module Shale
88
97
  # @api private
89
98
  def inherited(subclass)
90
99
  super
100
+
101
+ subclass.instance_variable_set('@model', subclass)
91
102
  subclass.instance_variable_set('@attributes', @attributes.dup)
92
103
 
93
104
  subclass.instance_variable_set('@__hash_mapping_init', @hash_mapping.dup)
94
105
  subclass.instance_variable_set('@__json_mapping_init', @json_mapping.dup)
95
106
  subclass.instance_variable_set('@__yaml_mapping_init', @yaml_mapping.dup)
107
+ subclass.instance_variable_set('@__toml_mapping_init', @toml_mapping.dup)
96
108
  subclass.instance_variable_set('@__xml_mapping_init', @xml_mapping.dup)
97
109
 
98
110
  subclass.instance_variable_set('@hash_mapping', @hash_mapping.dup)
99
111
  subclass.instance_variable_set('@json_mapping', @json_mapping.dup)
100
112
  subclass.instance_variable_set('@yaml_mapping', @yaml_mapping.dup)
113
+ subclass.instance_variable_set('@toml_mapping', @toml_mapping.dup)
101
114
 
102
115
  xml_mapping = @xml_mapping.dup
103
116
  xml_mapping.root(Utils.underscore(subclass.name || ''))
@@ -105,6 +118,15 @@ module Shale
105
118
  subclass.instance_variable_set('@xml_mapping', xml_mapping.dup)
106
119
  end
107
120
 
121
+ def model(klass = nil)
122
+ if klass
123
+ @model = klass
124
+ xml_mapping.root(Utils.underscore(@model.name))
125
+ else
126
+ @model
127
+ end
128
+ end
129
+
108
130
  # Define attribute on class
109
131
  #
110
132
  # @param [Symbol] name Name of the attribute
@@ -143,10 +165,11 @@ module Shale
143
165
 
144
166
  @attributes[name] = Attribute.new(name, type, collection, default)
145
167
 
146
- @hash_mapping.map(name.to_s, to: name)
147
- @json_mapping.map(name.to_s, to: name)
148
- @yaml_mapping.map(name.to_s, to: name)
149
- @xml_mapping.map_element(name.to_s, to: name)
168
+ @hash_mapping.map(name.to_s, to: name) unless @hash_mapping.finalized?
169
+ @json_mapping.map(name.to_s, to: name) unless @json_mapping.finalized?
170
+ @yaml_mapping.map(name.to_s, to: name) unless @yaml_mapping.finalized?
171
+ @toml_mapping.map(name.to_s, to: name) unless @toml_mapping.finalized?
172
+ @xml_mapping.map_element(name.to_s, to: name) unless @xml_mapping.finalized?
150
173
 
151
174
  class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
152
175
  attr_reader :#{name}
@@ -168,7 +191,7 @@ module Shale
168
191
  # attribute :age, Shale::Type::Integer
169
192
  #
170
193
  # hsh do
171
- # map 'firatName', to: :first_name
194
+ # map 'firstName', to: :first_name
172
195
  # map 'lastName', to: :last_name
173
196
  # map 'age', to: :age
174
197
  # end
@@ -177,6 +200,7 @@ module Shale
177
200
  # @api public
178
201
  def hsh(&block)
179
202
  @hash_mapping = @__hash_mapping_init.dup
203
+ @hash_mapping.finalize!
180
204
  @hash_mapping.instance_eval(&block)
181
205
  end
182
206
 
@@ -191,7 +215,7 @@ module Shale
191
215
  # attribute :age, Shale::Type::Integer
192
216
  #
193
217
  # json do
194
- # map 'firatName', to: :first_name
218
+ # map 'firstName', to: :first_name
195
219
  # map 'lastName', to: :last_name
196
220
  # map 'age', to: :age
197
221
  # end
@@ -200,6 +224,7 @@ module Shale
200
224
  # @api public
201
225
  def json(&block)
202
226
  @json_mapping = @__json_mapping_init.dup
227
+ @json_mapping.finalize!
203
228
  @json_mapping.instance_eval(&block)
204
229
  end
205
230
 
@@ -214,7 +239,7 @@ module Shale
214
239
  # attribute :age, Shale::Type::Integer
215
240
  #
216
241
  # yaml do
217
- # map 'firat_name', to: :first_name
242
+ # map 'first_name', to: :first_name
218
243
  # map 'last_name', to: :last_name
219
244
  # map 'age', to: :age
220
245
  # end
@@ -223,9 +248,34 @@ module Shale
223
248
  # @api public
224
249
  def yaml(&block)
225
250
  @yaml_mapping = @__yaml_mapping_init.dup
251
+ @yaml_mapping.finalize!
226
252
  @yaml_mapping.instance_eval(&block)
227
253
  end
228
254
 
255
+ # Define TOML mapping
256
+ #
257
+ # @param [Proc] block
258
+ #
259
+ # @example
260
+ # calss Person < Shale::Mapper
261
+ # attribute :first_name, Shale::Type::String
262
+ # attribute :last_name, Shale::Type::String
263
+ # attribute :age, Shale::Type::Integer
264
+ #
265
+ # toml do
266
+ # map 'first_name', to: :first_name
267
+ # map 'last_name', to: :last_name
268
+ # map 'age', to: :age
269
+ # end
270
+ # end
271
+ #
272
+ # @api public
273
+ def toml(&block)
274
+ @toml_mapping = @__toml_mapping_init.dup
275
+ @toml_mapping.finalize!
276
+ @toml_mapping.instance_eval(&block)
277
+ end
278
+
229
279
  # Define XML mapping
230
280
  #
231
281
  # @param [Proc] block
@@ -247,6 +297,8 @@ module Shale
247
297
  # @api public
248
298
  def xml(&block)
249
299
  @xml_mapping = @__xml_mapping_init.dup
300
+ @xml_mapping.finalize!
301
+ @xml_mapping.root('')
250
302
  @xml_mapping.instance_eval(&block)
251
303
  end
252
304
  end
@@ -272,9 +324,11 @@ module Shale
272
324
  def initialize(**props)
273
325
  super()
274
326
 
275
- props.each_key do |name|
276
- unless self.class.attributes.keys.include?(name)
277
- raise UnknownAttributeError.new(self.class.to_s, name.to_s)
327
+ unless props.empty?
328
+ unknown_attributes = props.keys - self.class.attributes.keys
329
+
330
+ unless unknown_attributes.empty?
331
+ raise UnknownAttributeError.new(self.class.to_s, unknown_attributes[0].to_s)
278
332
  end
279
333
  end
280
334
 
@@ -285,7 +339,7 @@ module Shale
285
339
  value = attribute.default.call
286
340
  end
287
341
 
288
- public_send("#{name}=", value)
342
+ send(attribute.setter, value)
289
343
  end
290
344
  end
291
345
  end
@@ -16,17 +16,26 @@ module Shale
16
16
  # @api private
17
17
  attr_reader :namespace
18
18
 
19
+ # Return cdata
20
+ #
21
+ # @return [true, false]
22
+ #
23
+ # @api private
24
+ attr_reader :cdata
25
+
19
26
  # Initialize instance
20
27
  #
21
28
  # @param [String] name
22
29
  # @param [Symbol, String] attribute
23
30
  # @param [Hash, nil] methods
24
31
  # @param [Shale::Mapping::XmlNamespace] namespace
32
+ # @param [true, false] cdata
25
33
  #
26
34
  # @api private
27
- def initialize(name:, attribute:, methods:, namespace:)
35
+ def initialize(name:, attribute:, methods:, namespace:, cdata:)
28
36
  super(name: name, attribute: attribute, methods: methods)
29
37
  @namespace = namespace
38
+ @cdata = cdata
30
39
  end
31
40
 
32
41
  # Return name with XML prefix
@@ -22,6 +22,7 @@ module Shale
22
22
  def initialize
23
23
  super
24
24
  @keys = {}
25
+ @finalized = false
25
26
  end
26
27
 
27
28
  # Map key to attribute
@@ -38,9 +39,26 @@ module Shale
38
39
  @keys[key] = Descriptor::Dict.new(name: key, attribute: to, methods: using)
39
40
  end
40
41
 
42
+ # Set the "finalized" instance variable to true
43
+ #
44
+ # @api private
45
+ def finalize!
46
+ @finalized = true
47
+ end
48
+
49
+ # Query the "finalized" instance variable
50
+ #
51
+ # @return [truem false]
52
+ #
53
+ # @api private
54
+ def finalized?
55
+ @finalized
56
+ end
57
+
41
58
  # @api private
42
59
  def initialize_dup(other)
43
60
  @keys = other.instance_variable_get('@keys').dup
61
+ @finalized = false
44
62
  super
45
63
  end
46
64
  end
@@ -63,6 +63,7 @@ module Shale
63
63
  @content = nil
64
64
  @root = ''
65
65
  @default_namespace = Descriptor::XmlNamespace.new
66
+ @finalized = false
66
67
  end
67
68
 
68
69
  # Map element to attribute
@@ -76,7 +77,14 @@ module Shale
76
77
  # @raise [IncorrectMappingArgumentsError] when arguments are incorrect
77
78
  #
78
79
  # @api private
79
- def map_element(element, to: nil, using: nil, namespace: :undefined, prefix: :undefined)
80
+ def map_element(
81
+ element,
82
+ to: nil,
83
+ using: nil,
84
+ namespace: :undefined,
85
+ prefix: :undefined,
86
+ cdata: false
87
+ )
80
88
  Validator.validate_arguments(element, to, using)
81
89
  Validator.validate_namespace(element, namespace, prefix)
82
90
 
@@ -94,7 +102,8 @@ module Shale
94
102
  name: element,
95
103
  attribute: to,
96
104
  methods: using,
97
- namespace: Descriptor::XmlNamespace.new(nsp, pfx)
105
+ namespace: Descriptor::XmlNamespace.new(nsp, pfx),
106
+ cdata: cdata
98
107
  )
99
108
  end
100
109
 
@@ -119,7 +128,8 @@ module Shale
119
128
  name: attribute,
120
129
  attribute: to,
121
130
  methods: using,
122
- namespace: Descriptor::XmlNamespace.new(namespace, prefix)
131
+ namespace: Descriptor::XmlNamespace.new(namespace, prefix),
132
+ cdata: false
123
133
  )
124
134
  end
125
135
 
@@ -128,8 +138,16 @@ module Shale
128
138
  # @param [Symbol] to Object's attribute
129
139
  #
130
140
  # @api private
131
- def map_content(to:)
132
- @content = to
141
+ def map_content(to: nil, using: nil, cdata: false)
142
+ Validator.validate_arguments('content', to, using)
143
+
144
+ @content = Descriptor::Xml.new(
145
+ name: nil,
146
+ attribute: to,
147
+ methods: using,
148
+ namespace: nil,
149
+ cdata: cdata
150
+ )
133
151
  end
134
152
 
135
153
  # Set the name for root element
@@ -152,6 +170,22 @@ module Shale
152
170
  @default_namespace.prefix = prefix
153
171
  end
154
172
 
173
+ # Set the "finalized" instance variable to true
174
+ #
175
+ # @api private
176
+ def finalize!
177
+ @finalized = true
178
+ end
179
+
180
+ # Query the "finalized" instance variable
181
+ #
182
+ # @return [truem false]
183
+ #
184
+ # @api private
185
+ def finalized?
186
+ @finalized
187
+ end
188
+
155
189
  # @api private
156
190
  def initialize_dup(other)
157
191
  @elements = other.instance_variable_get('@elements').dup
@@ -159,6 +193,7 @@ module Shale
159
193
  @content = other.instance_variable_get('@content').dup
160
194
  @root = other.instance_variable_get('@root').dup
161
195
  @default_namespace = other.instance_variable_get('@default_namespace').dup
196
+ @finalized = false
162
197
 
163
198
  super
164
199
  end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Shale
4
+ module Schema
5
+ module Compiler
6
+ # Class that maps Schema type to Shale Boolean type
7
+ #
8
+ # @api private
9
+ class Boolean
10
+ # Return name of the Shale type
11
+ #
12
+ # @return [String]
13
+ #
14
+ # @api private
15
+ def name
16
+ 'Shale::Type::Boolean'
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../utils'
4
+
5
+ module Shale
6
+ module Schema
7
+ module Compiler
8
+ # Class representing Shale's complex type
9
+ #
10
+ # @api private
11
+ class Complex
12
+ # Return id
13
+ #
14
+ # @return [String]
15
+ #
16
+ # @api private
17
+ attr_reader :id
18
+
19
+ # Return properties
20
+ #
21
+ # @return [Array<Shale::Schema::Compiler::Property>]
22
+ #
23
+ # @api private
24
+ attr_reader :properties
25
+
26
+ # Name setter
27
+ #
28
+ # @param [String] val
29
+ #
30
+ # @api private
31
+ attr_writer :name
32
+
33
+ # Initialize object
34
+ #
35
+ # @param [String] id
36
+ # @param [String] name
37
+ #
38
+ # @api private
39
+ def initialize(id, name)
40
+ @id = id
41
+ @name = name
42
+ @properties = []
43
+ end
44
+
45
+ # Return name
46
+ #
47
+ # @return [String]
48
+ #
49
+ # @api private
50
+ def name
51
+ Utils.classify(@name)
52
+ end
53
+
54
+ # Return file name
55
+ #
56
+ # @return [String]
57
+ #
58
+ # @api private
59
+ def file_name
60
+ Utils.snake_case(@name)
61
+ end
62
+
63
+ # Return references
64
+ #
65
+ # @return [Array<Shale::Schema::Compiler::Property>]
66
+ #
67
+ # @api private
68
+ def references
69
+ @properties
70
+ .filter { |e| e.type.is_a?(self.class) && e.type != self }
71
+ .uniq { |e| e.type.id }
72
+ .sort { |a, b| a.type.file_name <=> b.type.file_name }
73
+ end
74
+
75
+ # Add property to Object
76
+ #
77
+ # @param [Shale::Schema::Compiler::Property] property
78
+ #
79
+ # @api private
80
+ def add_property(property)
81
+ unless @properties.find { |e| e.mapping_name == property.mapping_name }
82
+ @properties << property
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Shale
4
+ module Schema
5
+ module Compiler
6
+ # Class that maps Schema type to Shale Date type
7
+ #
8
+ # @api private
9
+ class Date
10
+ # Return name of the Shale type
11
+ #
12
+ # @return [String]
13
+ #
14
+ # @api private
15
+ def name
16
+ 'Shale::Type::Date'
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Shale
4
+ module Schema
5
+ module Compiler
6
+ # Class that maps Schema type to Shale Float type
7
+ #
8
+ # @api private
9
+ class Float
10
+ # Return name of the Shale type
11
+ #
12
+ # @return [String]
13
+ #
14
+ # @api private
15
+ def name
16
+ 'Shale::Type::Float'
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Shale
4
+ module Schema
5
+ module Compiler
6
+ # Class that maps Schema type to Shale Integer type
7
+ #
8
+ # @api private
9
+ class Integer
10
+ # Return name of the Shale type
11
+ #
12
+ # @return [String]
13
+ #
14
+ # @api private
15
+ def name
16
+ 'Shale::Type::Integer'
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../utils'
4
+
5
+ module Shale
6
+ module Schema
7
+ module Compiler
8
+ # Class representing Shale's property
9
+ #
10
+ # @api private
11
+ class Property
12
+ # Return mapping's name
13
+ #
14
+ # @return [String]
15
+ #
16
+ # @api private
17
+ attr_reader :mapping_name
18
+
19
+ # Return attribute's name
20
+ #
21
+ # @return [String]
22
+ #
23
+ # @api private
24
+ attr_reader :attribute_name
25
+
26
+ # Return types's name
27
+ #
28
+ # @return [String]
29
+ #
30
+ # @api private
31
+ attr_reader :type
32
+
33
+ # Initialize Property object
34
+ #
35
+ # @param [String] name
36
+ # @param [Shale::Schema::Compiler::Type] type
37
+ # @param [true, false] collection
38
+ # @param [Object] default
39
+ #
40
+ # @api private
41
+ def initialize(name, type, collection, default)
42
+ @mapping_name = name
43
+ @attribute_name = Utils.snake_case(name)
44
+ @type = type
45
+ @collection = collection
46
+ @default = default
47
+ end
48
+
49
+ # Return whether property is a collection
50
+ #
51
+ # @return [true, false]
52
+ #
53
+ # @api private
54
+ def collection?
55
+ @collection
56
+ end
57
+
58
+ # Return default value
59
+ #
60
+ # @return [nil, Object]
61
+ #
62
+ # @api private
63
+ def default
64
+ return if collection?
65
+ @default.is_a?(::String) ? @default.dump : @default
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Shale
4
+ module Schema
5
+ module Compiler
6
+ # Class that maps Schema type to Shale String type
7
+ #
8
+ # @api private
9
+ class String
10
+ # Return name of the Shale type
11
+ #
12
+ # @return [String]
13
+ #
14
+ # @api private
15
+ def name
16
+ 'Shale::Type::String'
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end