shale 0.3.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +31 -0
  3. data/README.md +200 -21
  4. data/exe/shaleb +108 -36
  5. data/lib/shale/adapter/nokogiri/document.rb +87 -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 +80 -0
  9. data/lib/shale/adapter/ox/node.rb +88 -0
  10. data/lib/shale/adapter/ox.rb +9 -134
  11. data/lib/shale/adapter/rexml/document.rb +88 -0
  12. data/lib/shale/adapter/rexml/node.rb +99 -0
  13. data/lib/shale/adapter/rexml.rb +9 -150
  14. data/lib/shale/attribute.rb +6 -0
  15. data/lib/shale/error.rb +39 -0
  16. data/lib/shale/mapper.rb +8 -6
  17. data/lib/shale/schema/compiler/boolean.rb +21 -0
  18. data/lib/shale/schema/compiler/complex.rb +88 -0
  19. data/lib/shale/schema/compiler/date.rb +21 -0
  20. data/lib/shale/schema/compiler/float.rb +21 -0
  21. data/lib/shale/schema/compiler/integer.rb +21 -0
  22. data/lib/shale/schema/compiler/property.rb +70 -0
  23. data/lib/shale/schema/compiler/string.rb +21 -0
  24. data/lib/shale/schema/compiler/time.rb +21 -0
  25. data/lib/shale/schema/compiler/value.rb +21 -0
  26. data/lib/shale/schema/compiler/xml_complex.rb +50 -0
  27. data/lib/shale/schema/compiler/xml_property.rb +73 -0
  28. data/lib/shale/schema/json_compiler.rb +331 -0
  29. data/lib/shale/schema/{json → json_generator}/base.rb +2 -2
  30. data/lib/shale/schema/{json → json_generator}/boolean.rb +1 -1
  31. data/lib/shale/schema/{json → json_generator}/collection.rb +2 -2
  32. data/lib/shale/schema/{json → json_generator}/date.rb +1 -1
  33. data/lib/shale/schema/{json → json_generator}/float.rb +1 -1
  34. data/lib/shale/schema/{json → json_generator}/integer.rb +1 -1
  35. data/lib/shale/schema/{json → json_generator}/object.rb +5 -2
  36. data/lib/shale/schema/{json → json_generator}/ref.rb +1 -1
  37. data/lib/shale/schema/{json → json_generator}/schema.rb +7 -5
  38. data/lib/shale/schema/{json → json_generator}/string.rb +1 -1
  39. data/lib/shale/schema/{json → json_generator}/time.rb +1 -1
  40. data/lib/shale/schema/json_generator/value.rb +23 -0
  41. data/lib/shale/schema/{json.rb → json_generator.rb} +36 -36
  42. data/lib/shale/schema/xml_compiler.rb +919 -0
  43. data/lib/shale/schema/{xml → xml_generator}/attribute.rb +1 -1
  44. data/lib/shale/schema/{xml → xml_generator}/complex_type.rb +5 -2
  45. data/lib/shale/schema/{xml → xml_generator}/element.rb +1 -1
  46. data/lib/shale/schema/{xml → xml_generator}/import.rb +1 -1
  47. data/lib/shale/schema/{xml → xml_generator}/ref_attribute.rb +1 -1
  48. data/lib/shale/schema/{xml → xml_generator}/ref_element.rb +1 -1
  49. data/lib/shale/schema/{xml → xml_generator}/schema.rb +5 -5
  50. data/lib/shale/schema/{xml → xml_generator}/typed_attribute.rb +1 -1
  51. data/lib/shale/schema/{xml → xml_generator}/typed_element.rb +1 -1
  52. data/lib/shale/schema/{xml.rb → xml_generator.rb} +25 -26
  53. data/lib/shale/schema.rb +44 -5
  54. data/lib/shale/type/{composite.rb → complex.rb} +34 -22
  55. data/lib/shale/utils.rb +42 -7
  56. data/lib/shale/version.rb +1 -1
  57. data/lib/shale.rb +8 -19
  58. data/shale.gemspec +1 -1
  59. metadata +47 -27
@@ -1,32 +1,33 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative '../../shale'
4
- require_relative 'json/schema'
5
- require_relative 'json/boolean'
6
- require_relative 'json/collection'
7
- require_relative 'json/date'
8
- require_relative 'json/float'
9
- require_relative 'json/integer'
10
- require_relative 'json/object'
11
- require_relative 'json/ref'
12
- require_relative 'json/string'
13
- require_relative 'json/time'
4
+ require_relative 'json_generator/schema'
5
+ require_relative 'json_generator/boolean'
6
+ require_relative 'json_generator/collection'
7
+ require_relative 'json_generator/date'
8
+ require_relative 'json_generator/float'
9
+ require_relative 'json_generator/integer'
10
+ require_relative 'json_generator/object'
11
+ require_relative 'json_generator/ref'
12
+ require_relative 'json_generator/string'
13
+ require_relative 'json_generator/time'
14
+ require_relative 'json_generator/value'
14
15
 
15
16
  module Shale
16
17
  module Schema
17
- # Class for handling JSON schema
18
+ # Class for generating JSON schema
18
19
  #
19
20
  # @api public
20
- class JSON
21
- @json_types = Hash.new(Shale::Schema::JSON::String)
21
+ class JSONGenerator
22
+ @json_types = Hash.new(Shale::Schema::JSONGenerator::String)
22
23
 
23
24
  # Register Shale to JSON type mapping
24
25
  #
25
26
  # @param [Shale::Type::Value] shale_type
26
- # @param [Shale::Schema::JSON::Base] json_type
27
+ # @param [Shale::Schema::JSONGenerator::Base] json_type
27
28
  #
28
29
  # @example
29
- # Shale::Schema::JSON.register_json_type(Shale::Type::String, MyCustomJsonType)
30
+ # Shale::Schema::JSONGenerator.register_json_type(Shale::Type::String, MyCustomJsonType)
30
31
  #
31
32
  # @api public
32
33
  def self.register_json_type(shale_type, json_type)
@@ -37,10 +38,10 @@ module Shale
37
38
  #
38
39
  # @param [Shale::Type::Value] shale_type
39
40
  #
40
- # @return [Shale::Schema::JSON::Base]
41
+ # @return [Shale::Schema::JSONGenerator::Base]
41
42
  #
42
43
  # @example
43
- # Shale::Schema::JSON.get_json_type(Shale::Type::String)
44
+ # Shale::Schema::JSONGenerator.get_json_type(Shale::Type::String)
44
45
  # # => Shale::Schema::JSON::String
45
46
  #
46
47
  # @api private
@@ -48,11 +49,12 @@ module Shale
48
49
  @json_types[shale_type]
49
50
  end
50
51
 
51
- register_json_type(Shale::Type::Boolean, Shale::Schema::JSON::Boolean)
52
- register_json_type(Shale::Type::Date, Shale::Schema::JSON::Date)
53
- register_json_type(Shale::Type::Float, Shale::Schema::JSON::Float)
54
- register_json_type(Shale::Type::Integer, Shale::Schema::JSON::Integer)
55
- register_json_type(Shale::Type::Time, Shale::Schema::JSON::Time)
52
+ register_json_type(Shale::Type::Boolean, Shale::Schema::JSONGenerator::Boolean)
53
+ register_json_type(Shale::Type::Date, Shale::Schema::JSONGenerator::Date)
54
+ register_json_type(Shale::Type::Float, Shale::Schema::JSONGenerator::Float)
55
+ register_json_type(Shale::Type::Integer, Shale::Schema::JSONGenerator::Integer)
56
+ register_json_type(Shale::Type::Time, Shale::Schema::JSONGenerator::Time)
57
+ register_json_type(Shale::Type::Value, Shale::Schema::JSONGenerator::Value)
56
58
 
57
59
  # Generate JSON Schema from Shale model and return it as a Ruby Hash
58
60
  #
@@ -65,15 +67,16 @@ module Shale
65
67
  # @return [Hash]
66
68
  #
67
69
  # @example
68
- # Shale::Schema::JSON.new.as_schema(Person)
70
+ # Shale::Schema::JSONGenerator.new.as_schema(Person)
69
71
  #
70
72
  # @api public
71
- def as_schema(klass, id: nil, description: nil)
73
+ def as_schema(klass, id: nil, title: nil, description: nil)
72
74
  unless mapper_type?(klass)
73
75
  raise NotAShaleMapperError, "JSON Shema can't be generated for '#{klass}' type"
74
76
  end
75
77
 
76
- types = collect_composite_types(klass)
78
+ types = []
79
+ collect_complex_types(types, klass)
77
80
  objects = []
78
81
 
79
82
  types.each do |type|
@@ -103,7 +106,7 @@ module Shale
103
106
  objects << Object.new(type.name, properties)
104
107
  end
105
108
 
106
- Schema.new(objects, id: id, description: description).as_json
109
+ Schema.new(objects, id: id, title: title, description: description).as_json
107
110
  end
108
111
 
109
112
  # Generate JSON Schema from Shale model
@@ -116,11 +119,11 @@ module Shale
116
119
  # @return [String]
117
120
  #
118
121
  # @example
119
- # Shale::Schema::JSON.new.to_schema(Person)
122
+ # Shale::Schema::JSONGenerator.new.to_schema(Person)
120
123
  #
121
124
  # @api public
122
- def to_schema(klass, id: nil, description: nil, pretty: false)
123
- schema = as_schema(klass, id: id, description: description)
125
+ def to_schema(klass, id: nil, title: nil, description: nil, pretty: false)
126
+ schema = as_schema(klass, id: id, title: title, description: description)
124
127
  options = pretty ? :pretty : nil
125
128
 
126
129
  Shale.json_adapter.dump(schema, options)
@@ -141,24 +144,21 @@ module Shale
141
144
 
142
145
  # Collect recursively Shale::Mapper types
143
146
  #
147
+ # @param [Array<Shale::Mapper>] types
144
148
  # @param [Shale::Mapper] type
145
149
  #
146
- # @return [Array<Shale::Mapper>]
147
- #
148
150
  # @api private
149
- def collect_composite_types(type)
150
- types = [type]
151
+ def collect_complex_types(types, type)
152
+ types << type
151
153
 
152
154
  type.json_mapping.keys.values.each do |mapping|
153
155
  attribute = type.attributes[mapping.attribute]
154
156
  next unless attribute
155
157
 
156
158
  if mapper_type?(attribute.type) && !types.include?(attribute.type)
157
- types += collect_composite_types(attribute.type)
159
+ collect_complex_types(types, attribute.type)
158
160
  end
159
161
  end
160
-
161
- types.uniq
162
162
  end
163
163
  end
164
164
  end