schema-model 0.3.7 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0b86ecbc7194678bb32de0ee683078efd6c313282d18b173eb2f114c01e1c01b
4
- data.tar.gz: 426d5fc0f310627a60c7e5f6c3215f1df75fc4f51bd220446f079e56d1ffc830
3
+ metadata.gz: 4e5bb83843c2fd8c09fc7773719e9c6c91a7008f5ff5ad69b935bee1616c4026
4
+ data.tar.gz: 7abce97c2b8ba2dbd5b1f955025a85bf64bfc4589e079531db823b150268c777
5
5
  SHA512:
6
- metadata.gz: 3657e878488ded76735242a1637482570622501802546e7bcdafafb330934ee93e12c556fffd5c7e9acb0dc9ce1327f83e657a3e4d02410df6c15d2bef309978
7
- data.tar.gz: 1810d41aff3936e52e2a25e1239d78f2e0cb5c71f5e8c659b58baddbdc0c4fee242c855fd9bc35f02aa93fc554664559102229b502c048184a53835db6fa614d
6
+ metadata.gz: 8e7ea9b0a73e35c8d1b69a7429d7a923959db7b3a38eb8c78326f5a96415326c4bea926c3c24f0d958bbe56cb7ee8b35b08d37fdc67a9ea563f02ebf308f457b
7
+ data.tar.gz: 2e0a2a6217c2594f152a1544d36d0eeb63d13fb9b1d6ee23589d63c70f4fccec5178869dd81bd2f3f0dc42dfb1f02cef0a022c75f2190a1765cf3c85da873f0f
@@ -28,12 +28,12 @@ module Schema
28
28
  def get_field_names(mapped_headers, header_prefix = nil, mapped = true)
29
29
  fields = []
30
30
  schema.each do |field_name, field_options|
31
- case field_options[:type]
32
- when :has_one,
33
- :has_many
31
+ next if field_options[:alias_of]
32
+
33
+ if field_options[:association]
34
34
  fields += get_model_field_names(field_name, field_options, mapped_headers, header_prefix, mapped)
35
35
  else
36
- next if skip_field?(field_name, field_options, mapped_headers, mapped)
36
+ next if skip_field?(field_name, mapped_headers, mapped)
37
37
 
38
38
  fields << generate_field_name(field_name, field_options, header_prefix)
39
39
  end
@@ -47,15 +47,12 @@ module Schema
47
47
  mapped_model = mapped_headers[field_name] || {}
48
48
  const_get(field_options[:class_name]).get_field_names(
49
49
  mapped_model,
50
- header_prefix || field_options[:header_prefix],
50
+ header_prefix || field_options[:aliases]&.first,
51
51
  mapped
52
52
  )
53
53
  end
54
54
 
55
- def skip_field?(field_name, field_options, mapped_headers, mapped)
56
- # skip alias fields
57
- return true if field_options[:alias_of]
58
-
55
+ def skip_field?(field_name, mapped_headers, mapped)
59
56
  if mapped
60
57
  mapped_headers[field_name].nil?
61
58
  else
@@ -88,12 +85,14 @@ module Schema
88
85
  def map_headers_to_has_many_associations(headers, mapped_headers)
89
86
  schema.each do |field_name, field_options|
90
87
  next unless field_options[:type] == :has_many
91
- next unless field_options[:header_prefix]
92
88
 
93
- mapped_model = get_mapped_model(field_options, headers, field_options[:header_prefix])
94
- next if mapped_model.empty?
89
+ get_header_prefixes(field_name, field_options).each do |header_prefix|
90
+ mapped_model = get_mapped_model(field_options, headers, header_prefix)
91
+ next if mapped_model.empty?
95
92
 
96
- mapped_headers[field_name] = mapped_model
93
+ mapped_headers[field_name] = mapped_model
94
+ break
95
+ end
97
96
  end
98
97
  mapped_headers
99
98
  end
@@ -121,6 +120,12 @@ module Schema
121
120
  end
122
121
  indexes
123
122
  end
123
+
124
+ def get_header_prefixes(field_name, field_options)
125
+ names = [field_name.to_s]
126
+ names += field_options[:aliases] if field_options[:aliases]
127
+ names
128
+ end
124
129
  end
125
130
  end
126
131
  end
@@ -32,6 +32,7 @@ module Schema
32
32
 
33
33
  kls = const_get(options[:class_name])
34
34
  kls.class_eval(&block) if block
35
+ add_aliases(name, options)
35
36
  kls
36
37
  end
37
38
  # rubocop:enable Naming/PredicateName
@@ -32,6 +32,7 @@ module Schema
32
32
 
33
33
  kls = const_get(options[:class_name])
34
34
  kls.class_eval(&block) if block
35
+ add_aliases(name, options)
35
36
  kls
36
37
  end
37
38
  # rubocop:enable Naming/PredicateName
data/lib/schema/model.rb CHANGED
@@ -92,15 +92,8 @@ module Schema
92
92
 
93
93
  def update_attributes(data)
94
94
  schema = get_schema(data)
95
- data.each do |key, value|
96
- unless schema.key?(key)
97
- parsing_errors.add(key, ::Schema::ParsingErrors::UNKNOWN_ATTRIBUTE)
98
- next
99
- end
100
-
101
- public_send(schema[key][:setter], value)
102
- end
103
-
95
+ update_model_attributes(schema, data)
96
+ update_associations(schema, data)
104
97
  self
105
98
  end
106
99
 
@@ -122,6 +115,8 @@ module Schema
122
115
  @parsing_errors ||= Errors.new
123
116
  end
124
117
 
118
+ private
119
+
125
120
  def get_schema(data)
126
121
  data.each_key do |key|
127
122
  break unless key.is_a?(Symbol)
@@ -130,5 +125,27 @@ module Schema
130
125
  end
131
126
  self.class.schema_with_string_keys
132
127
  end
128
+
129
+ def update_model_attributes(schema, data)
130
+ data.each do |key, value|
131
+ unless schema.key?(key)
132
+ parsing_errors.add(key, ::Schema::ParsingErrors::UNKNOWN_ATTRIBUTE)
133
+ next
134
+ end
135
+
136
+ next if schema[key][:association]
137
+
138
+ public_send(schema[key][:setter], value)
139
+ end
140
+ end
141
+
142
+ def update_associations(schema, data)
143
+ data.each do |key, value|
144
+ next unless schema.key?(key)
145
+ next unless schema[key][:association]
146
+
147
+ public_send(schema[key][:setter], value)
148
+ end
149
+ end
133
150
  end
134
151
  end
data/lib/schema/utils.rb CHANGED
@@ -26,6 +26,8 @@ module Schema
26
26
 
27
27
  def association_options(schema_name, schema_type, options)
28
28
  options[:class_name] ||= 'Schema' + classify_name(schema_type.to_s) + classify_name(schema_name.to_s)
29
+ options[:association] = true
30
+ options[:aliases] = [options[:alias]] if options.key?(:alias)
29
31
  ::Schema::Model.default_attribute_options(schema_name, schema_type).merge(options)
30
32
  end
31
33
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: schema-model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.7
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Doug Youch
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-08-09 00:00:00.000000000 Z
11
+ date: 2019-08-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: inheritance-helper
@@ -70,7 +70,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
70
70
  - !ruby/object:Gem::Version
71
71
  version: '0'
72
72
  requirements: []
73
- rubygems_version: 3.0.4
73
+ rubygems_version: 3.0.3
74
74
  signing_key:
75
75
  specification_version: 4
76
76
  summary: Schema Model