Module: LightModels::ParserWrapper
- Defined in:
- lib/lightmodels/parsing.rb
Constant Summary collapse
- JavaCollection =
::Java::JavaClass.for_name("java.util.Collection")
- @@verbose =
false
Instance Method Summary collapse
- #adapter(model_class, ref) ⇒ Object
- #assign_att_to_model(model, att, value) ⇒ Object
- #assign_ref_to_model(model, ref, value) ⇒ Object
- #attribute_to_method(model_class, att) ⇒ Object
- #get_feature_value(node, feat) ⇒ Object
- #get_feature_value_through_getter(node, feat_name) ⇒ Object
- #log(msg) ⇒ Object
- #node_to_model(node) ⇒ Object
- #populate_attr(node, att, model) ⇒ Object
- #populate_ref(node, ref, model) ⇒ Object
- #reference_to_method(model_class, ref) ⇒ Object
- #transform_enum_values(value) ⇒ Object
Instance Method Details
#adapter(model_class, ref) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/lightmodels/parsing.rb', line 35 def adapter(model_class,ref) if adapter_specific_class(model_class,ref) adapter_specific_class(model_class,ref) else if model_class.superclass!=Object adapter(model_class.superclass,ref) else nil end end end |
#assign_att_to_model(model, att, value) ⇒ Object
78 79 80 81 82 83 84 85 86 87 |
# File 'lib/lightmodels/parsing.rb', line 78 def assign_att_to_model(model,att,value) if att.many adder_method = :"add#{att.name.capitalize}" value.each {|el| model.send(adder_method,el)} else setter_method = :"#{att.name}=" raise "Trying to assign an array to a single property. Class #{model.class}, property #{att.name}" if value.is_a?(::Array) model.send(setter_method,value) end end |
#assign_ref_to_model(model, ref, value) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/lightmodels/parsing.rb', line 62 def assign_ref_to_model(model,ref,value) return unless value==nil # we do not need to assign a nil... if ref.many adder_method = :"add#{ref.name.capitalize}" value.each {|el| model.send(adder_method,node_to_model(el))} else setter_method = :"#{ref.name}=" raise "Trying to assign an array to a single property. Class #{model.class}, property #{ref.name}" if value.is_a?(::Array) model.send(setter_method,node_to_model(value)) end rescue Object => e puts "Problem while assigning ref #{ref.name} (many? #{ref.many}) to #{model.class}. Value: #{value.class}" puts "\t<<#{e}>>" raise e end |
#attribute_to_method(model_class, att) ⇒ Object
55 56 57 58 59 60 |
# File 'lib/lightmodels/parsing.rb', line 55 def attribute_to_method(model_class,att) s = att.name adapted = adapter(model_class,att) s = adapted if adapted s.to_sym end |
#get_feature_value(node, feat) ⇒ Object
174 175 176 177 178 179 180 181 182 |
# File 'lib/lightmodels/parsing.rb', line 174 def get_feature_value(node,feat) adapter = adapter(node.class,feat) if adapter #puts "Using adapter for #{node.class} #{feat_name}" adapter[:adapter].call(node) else get_feature_value_through_getter(node,feat.name) end end |
#get_feature_value_through_getter(node, feat_name) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/lightmodels/parsing.rb', line 158 def get_feature_value_through_getter(node,feat_name) capitalized_name = feat_name.proper_capitalize methods = [:"get#{capitalized_name}",:"is#{capitalized_name}"] methods.each do |m| if node.respond_to?(m) begin return transform_enum_values(node.send(m)) rescue Object => e raise "Problem invoking #{m} on #{node.class}: #{e}" end end end raise "how should I get this... #{feat_name} on #{node.class}. It does not respond to #{methods}" end |
#log(msg) ⇒ Object
133 134 135 |
# File 'lib/lightmodels/parsing.rb', line 133 def log(msg) puts msg if @@verbose end |
#node_to_model(node) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/lightmodels/parsing.rb', line 137 def node_to_model(node) log("node_to_model #{node.class}") = (node) instance = .new .ecore.eAllAttributes.each do |attr| populate_attr(node,attr,instance) end .ecore.eAllReferences.each do |ref| populate_ref(node,ref,instance) end instance end |
#populate_attr(node, att, model) ⇒ Object
89 90 91 92 93 94 95 96 97 |
# File 'lib/lightmodels/parsing.rb', line 89 def populate_attr(node,att,model) value = get_feature_value(node,att) #puts "Value got for #{node.class} #{att} : #{value.class}" # nil are ignored model.send(:"#{att.name}=",value) if value!=nil rescue Object => e puts "Problem while populating attribute #{att.name} of #{model} from #{node}. Value: #{value}" raise e end |
#populate_ref(node, ref, model) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/lightmodels/parsing.rb', line 99 def populate_ref(node,ref,model) log("populate ref #{ref.name}, node: #{node.class}, model: #{model.class}") value = get_feature_value(node,ref) log("\tvalue=#{value.class}") if value!=nil if value==node puts "avoiding loop... #{ref.name}, class #{node.class}" return end if JavaCollection.assignable_from?(value.java_class) log("\tvalue is a collection") capitalized_name = ref.name.proper_capitalize value.each do |el| unless el.respond_to?(:parent) class << el attr_accessor :parent end end el.parent = node model.send(:"add#{capitalized_name}",node_to_model(el)) end else log("\tvalue is not a collection") unless value.respond_to?(:parent) class << value attr_accessor :parent end end value.parent = node model.send(:"#{ref.name}=",node_to_model(value)) end end end |
#reference_to_method(model_class, ref) ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/lightmodels/parsing.rb', line 47 def reference_to_method(model_class,ref) s = ref.name #s = 'value' if s=='body' adapted = adapter(model_class,ref) s = adapted if adapted s.to_sym end |
#transform_enum_values(value) ⇒ Object
150 151 152 153 154 155 156 |
# File 'lib/lightmodels/parsing.rb', line 150 def transform_enum_values(value) if value.respond_to?(:java_class) && value.java_class.enum? value.name else value end end |