mappum 0.2.0 → 0.2.1

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.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.2.1
data/lib/mappum/dsl.rb CHANGED
@@ -1,4 +1,4 @@
1
- require 'facets/kernel/instance_exec'
1
+ #require 'facets/kernel/instance_exec'
2
2
  class Object
3
3
  def >> field
4
4
  return {Mappum::DSL::Constant.new(self) => field} if field.kind_of?(Mappum::DSL::Field)
@@ -49,6 +49,29 @@ module Mappum
49
49
  end
50
50
  end
51
51
  #
52
+ # Define how "to" array is passed to function.
53
+ # First argument is a label
54
+ # :> - left to right map
55
+ # :< - right to left map
56
+ # :<=> - both maps
57
+ # Second argument is the_way and can be:
58
+ # :new - will create new element
59
+ # :first - will map to the first element
60
+ # :all - will map to all elements
61
+ #
62
+ def to_array_take(label, the_way)
63
+ raise "the_way should be one of :new, :first, :all" unless [:new, :first, :all].include? the_way.to_sym
64
+ case label
65
+ when :<=>
66
+ @def.to_array_take_l2r = the_way.to_sym
67
+ @def.to_array_take_r2l = the_way.to_sym
68
+ when :>, '>'
69
+ @def.to_array_take_l2r = the_way.to_sym
70
+ when :<, '<'
71
+ @def.to_array_take_r2l = the_way.to_sym
72
+ end
73
+ end
74
+ #
52
75
  # Add comment to mapping.
53
76
  #
54
77
  def `(str)
@@ -159,7 +182,10 @@ module Mappum
159
182
  if not @def.normalized? and not @def.map_when.nil?
160
183
  raise ":when function can be set only on unidirectional maps use :when_r2l and :when_l2r or map_when function"
161
184
  end
162
-
185
+ @def.to_array_take = attr[0][1][:to_array_take] if attr[0].size > type_size
186
+ if not @def.normalized? and not @def.to_array_take.nil?
187
+ raise ":to_array_take property can be set only on unidirectional maps use :to_array_take_r2l and :to_array_take_l2r or to_array_take function"
188
+ end
163
189
  @def.submap_alias = attr[0][1][:map] if attr[0].size > type_size
164
190
 
165
191
  end
@@ -235,7 +261,7 @@ module Mappum
235
261
  end
236
262
  end
237
263
  #this functions also indicate Array -> element
238
- if symbol == :find or symbol == :detect
264
+ if symbol == :find or symbol == :detect or symbol == :select
239
265
  @def.is_array = true
240
266
  end
241
267
  arguments = args.clone
data/lib/mappum/map.rb CHANGED
@@ -94,6 +94,7 @@ module Mappum
94
94
  class FieldMap < Map
95
95
  attr_accessor :dict, :desc, :left, :right, :func, :block, :to, :from, :func_on_nil, :submap_alias
96
96
  attr_accessor :name, :l2r_name, :r2l_name, :name_prefix, :map_when, :when_r2l, :when_l2r
97
+ attr_accessor :to_array_take, :to_array_take_r2l, :to_array_take_l2r
97
98
  # True if map is unidirectional. Map is unidirectional
98
99
  # when maps one way only.
99
100
  def normalized?
@@ -118,6 +119,8 @@ module Mappum
118
119
  map_r2l.r2l_name, map_r2l.l2r_name = nil, nil
119
120
  map_r2l.map_when = self.when_r2l
120
121
  map_r2l.when_r2l, map_r2l.when_l2r = nil, nil
122
+ map_r2l.to_array_take = self.to_array_take_r2l
123
+ map_r2l.to_array_take_r2l, map_r2l.to_array_take_l2r = nil, nil
121
124
  map_r2l.maps = self.maps.select do |m|
122
125
  m.to.parent == map_r2l.to
123
126
  end
@@ -131,6 +134,8 @@ module Mappum
131
134
  map_l2r.r2l_name, map_l2r.l2r_name = nil, nil
132
135
  map_l2r.map_when = self.when_l2r
133
136
  map_l2r.when_r2l, map_l2r.when_l2r = nil, nil
137
+ map_l2r.to_array_take = self.to_array_take_l2r
138
+ map_l2r.to_array_take_r2l, map_l2r.to_array_take_l2r = nil, nil
134
139
  map_l2r.maps = self.maps.select do |m|
135
140
  m.to.parent == map_l2r.to
136
141
  end
@@ -158,7 +163,9 @@ module Mappum
158
163
  return Field.new(@parent, symbol, args[0])
159
164
  end
160
165
  end
161
- class Field < Struct.new(:name, :clazz, :parent, :func, :block, :is_root, :is_array, :is_placeholder)
166
+ class Field < Struct.new(:name, :clazz, :parent, :func, :block, :is_root, :is_placeholder)
167
+ #define is_array separetly to exclude it from equals
168
+ attr_accessor :is_array
162
169
  def array?
163
170
  is_array
164
171
  end
@@ -166,7 +173,7 @@ module Mappum
166
173
  is_placeholder
167
174
  end
168
175
  end
169
- class Constant < Struct.new(:value)
176
+ class Constant < Struct.new(:value,:parent)
170
177
  def parent
171
178
  nil
172
179
  end
@@ -177,10 +184,7 @@ module Mappum
177
184
  nil
178
185
  end
179
186
  end
180
- class Function
181
- def parent
182
- nil
183
- end
187
+ class Function < Struct.new(:parent)
184
188
  def array?
185
189
  false
186
190
  end
@@ -40,11 +40,7 @@ module Mappum
40
40
  map.maps.each do |sm|
41
41
  from_value, to_value = nil, nil
42
42
 
43
- if sm.from.respond_to?(:name)
44
- from_value = get(from, sm.from.name)
45
- else
46
- from_value = sm.from.value
47
- end
43
+ from_value = get(from, sm.from, map.from)
48
44
 
49
45
  # skip to next mapping on false :map_when function
50
46
  next unless sm.map_when.nil? or sm.map_when.call(from_value)
@@ -81,6 +77,8 @@ module Mappum
81
77
  end
82
78
  end
83
79
  unless submaps.empty? or from_value.nil?
80
+ #We should make some kind of test like this
81
+ #raise "Not an array: #{sm.from.name} inspect:" + from_value.inspect if sm.from.is_array and not from_value.kind_of?(Array)
84
82
  if from_value.kind_of?(Array) or
85
83
  (Module.constants.include? "ArrayJavaProxy" and from_value.kind_of?(Module.const_get(:ArrayJavaProxy)))
86
84
  sm_v = sm.clone
@@ -92,21 +90,41 @@ module Mappum
92
90
  sm_v.to = sm.to.clone
93
91
  sm_v.to.is_array = false
94
92
  end
95
- sm_v.maps = submaps if sm_v.maps.empty?
96
-
97
- to_value = from_value.collect{|v| transform(v, sm_v)}
93
+ if sm_v.maps.empty?
94
+ sm_v.maps = submaps
95
+ sm_v.maps.each{|m| m.from.parent = sm_v.from}
96
+ #don't add parent we need separation
97
+ to_value = from_value.collect{|v| transform(v, sm_v)}
98
+ else
99
+ to_value = from_value.collect{|v| transform(add_parent(v, from), sm_v)}
100
+ end
98
101
  else
99
102
  to ||= map.to.clazz.new unless @force_open_struct or map.to.clazz.nil? or map.to.clazz.kind_of?(Symbol)
100
103
  to ||= @default_struct_class.new
101
- v_to = nil
104
+ v_to = []
102
105
  #array values are assigned after return
103
- v_to = get(to, sm.to.name) unless sm.to.is_array and not sm.from.is_array
104
- sm_v = sm
105
- if sm_v.maps.empty?
106
- sm_v = sm.clone
107
- sm_v.maps = submaps
106
+ v_to << get(to, sm.to) unless sm.to.is_array and not sm.from.is_array
107
+ #nless one whants to update existing to array
108
+ if sm.to_array_take == :first
109
+ arr_v = get(to, sm.to)
110
+ v_to << arr_v[0] if not arr_v.nil?
111
+ end
112
+ if sm.to_array_take == :all
113
+ arr_v = get(to, sm.to)
114
+ v_to += arr_v if not arr_v.nil?
108
115
  end
109
- to_value = transform(from_value, sm_v, v_to)
116
+ v_to.each do |v_t|
117
+ sm_v = sm
118
+ if sm_v.maps.empty?
119
+ sm_v = sm.clone
120
+ sm_v.maps = submaps
121
+ sm_v.maps.each{|m| m.from.parent = sm_v.from}
122
+ #don't add parent we need separation
123
+ to_value = transform(from_value, sm_v, v_t)
124
+ else
125
+ to_value = transform(add_parent(from_value, from), sm_v, v_t)
126
+ end
127
+ end
110
128
  end
111
129
 
112
130
  end
@@ -114,9 +132,9 @@ module Mappum
114
132
  to_value = sm.dict[to_value]
115
133
  end
116
134
  if sm.to.is_array and not sm.from.is_array
117
- to_array = convert_from(get(to,sm.to.name),sm.from)
135
+ to_array = convert_from(get(to,sm.to),sm.from)
118
136
  to_array ||= []
119
- to_array << to_value
137
+ to_array << to_value unless sm.to_array_take == :first or sm.to_array_take == :all
120
138
 
121
139
  if to_array.empty? and sm.strip_empty?
122
140
  to_array = nil
@@ -157,21 +175,36 @@ module Mappum
157
175
 
158
176
  protected
159
177
 
160
- def get(object, field)
161
- if field.nil? or object.nil?
162
- return object
163
- elsif
164
- begin
165
- return object.send(field)
166
- rescue NoMethodError => e
167
- #for open structures field will be defined later
168
- if object.kind_of?(@default_struct_class)
169
- return nil
170
- else
171
- raise e
172
- end
173
- end
174
- end
178
+ def get(object, field, parent_field=nil)
179
+ if field.kind_of?(String) or field.kind_of?(Symbol)
180
+ field_name = field
181
+ else
182
+ unless field.respond_to?(:name)
183
+ return field.value
184
+ end
185
+ if field.name.nil? or object.nil?
186
+ return object
187
+ end
188
+ #for fields targeted at parents go up the tree
189
+ if (not parent_field.nil?) and field.parent != parent_field
190
+ if object.respond_to?(:_mpum_parent)
191
+ return get(object._mpum_parent, field, parent_field.parent)
192
+ else
193
+ raise "We hit an element with no parent: #{object.inspect}"
194
+ end
195
+ end
196
+ field_name = field.name
197
+ end
198
+ begin
199
+ return object.send(field_name)
200
+ rescue NoMethodError => e
201
+ #for open structures field will be defined later
202
+ if object.kind_of?(@default_struct_class)
203
+ return nil
204
+ else
205
+ raise e
206
+ end
207
+ end
175
208
  end
176
209
  def convert_to(to, field_def)
177
210
  return to
@@ -179,6 +212,15 @@ module Mappum
179
212
  def convert_from(from, field_def)
180
213
  return from
181
214
  end
215
+ def add_parent(value, parent)
216
+ #don't work for Fixnums and Floats
217
+ return value if value.kind_of?(Fixnum) or value.kind_of?(Float)
218
+ class << value
219
+ attr_accessor :_mpum_parent
220
+ end
221
+ value._mpum_parent = parent
222
+ return value
223
+ end
182
224
  end
183
225
  class OpenStruct < OpenStruct
184
226
  def type(*attr)
@@ -178,12 +178,12 @@ module Mappum
178
178
  def initialize(*args)
179
179
  super(*args)
180
180
  end
181
- def get(object, field)
181
+ def get(object, field, parent_field=nil)
182
182
  begin
183
- super(object, field)
183
+ super(object, field, parent_field)
184
184
  rescue NoMethodError
185
185
  begin
186
- super(object, XSD::CodeGen::GenSupport.safemethodname(field.to_s).to_sym)
186
+ super(object, XSD::CodeGen::GenSupport.safemethodname(field.name.to_s).to_sym, parent_field)
187
187
  rescue NoMethodError
188
188
  #for dynamic xml nil value == no methond
189
189
  if object.kind_of?(SOAP::Mapping::Object)
data/mappum.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mappum}
8
- s.version = "0.2.0"
8
+ s.version = "0.2.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Jan Topi\305\204ski"]
12
- s.date = %q{2009-11-08}
12
+ s.date = %q{2009-11-13}
13
13
  s.default_executable = %q{mapserver.rb}
14
14
  s.description = %q{}
15
15
  s.email = %q{jtopinski@chatka.org}
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mappum
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - "Jan Topi\xC5\x84ski"
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-08 00:00:00 +01:00
12
+ date: 2009-11-13 00:00:00 +01:00
13
13
  default_executable: mapserver.rb
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency