lib/ember_serialize.rb in ember_serialize-0.0.6 vs lib/ember_serialize.rb in ember_serialize-0.0.7

- old
+ new

@@ -19,10 +19,23 @@ @eig = "ember_serialize:ignore" @eai = "ember_serialize:as_is" @eas = "ember_serialize:async" @javascripts_dir = self.class.javascripts_dir || "app/assets/javascripts/" @models_dir = self.class.models_dir || @javascripts_dir+"models/" + @types = Hash.new(:string).merge({ + # string: :string, + # text: :string, + date: :date, + datetime: :date, + timestamp: :date, + time: :date, + primary_key: :number, + float: :number, + integer: :number, + boolean: :boolean, + decimal: :number + }) unless File.exists? @models_dir require 'fileutils' FileUtils.mkdir_p @models_dir end # engine / extension @@ -116,11 +129,11 @@ else "#{indent}#{camel_name}: DS.hasMany('#{camel_name}'#{_async})" end end else - existing[camel_name] || "#{indent}#{camel_name}: DS.attr('#{type || 'string'}')" + existing[camel_name] || "#{indent}#{camel_name}: DS.attr('#{@types[type]}')" end [camel_name, line] end def ember_model_parse(ember_model_file, model) @@ -139,30 +152,24 @@ line_start = lines.index {|l| l =~ /#{@est}/} line_end = lines.index {|l| l =~ /#{@een}/} if line_start && line_end # find settings for ignore, as_is, and async - ignore = lines.grep(/#{@eig}/) do |l| - l.gsub(/.*#{@eig} (.*)\s*$/, '\1').split(/\s*,\s*/) - end.flatten.uniq.map {|t| camel(t)} - as_is = lines.grep(/#{@eai}/) do |l| - l.gsub(/.*#{@eai} (.*)\s*$/, '\1').split(/\s*,\s*/) - end.flatten.uniq.map {|t| camel(t)} + ignore = setting_ignore lines + as_is = setting_as_is lines if @force_async.nil? - async = lines.grep(/#{@eas}/) do |l| - l.gsub(/.*#{@eai} (.*)\s*$/, '\1') == 'true' - end.flatten.last - async = true if async.nil? + async = setting_async lines else async = @force_async end # match the indent of the start line indent = lines[line_start][/\A */] # catalog existing lines existing = {} + outside = [] lines.each_with_index do |line, i| next if line =~ /^\s*#/ # reject comments next unless line =~ /:\s*DS\./ #include DS lines # reformat the line name, ds = line.strip.gsub(/,$/,'').split(/:\s*/,2) @@ -196,10 +203,11 @@ ember_reflect(model, name, :attribute, async, existing, indent, type) end next if camel_name == 'id' unless ignore.include? camel_name new_lines << line + ignore << camel_name end end # build associations schema[:associations].each do |key, assoc| @@ -211,12 +219,14 @@ end end # build final content content = [ lines[0..line_start].join("\n") ] - unless ignore.blank? - content << "#{indent}# #{@eig} #{ignore.join(", ")}" + # write original ignore setting + ignore_setting = setting_ignore(lines) + unless ignore_setting.blank? + content << "#{indent}# #{@eig} #{ignore_setting.join(", ")}" end unless as_is.blank? content << "#{indent}# #{@eai} #{as_is.join(", ")}" end if async == false @@ -246,8 +256,27 @@ if serializer.respond_to? :schema serializer.schema else {} end + end + + def setting_ignore(lines) + lines.grep(/#{@eig}/) do |l| + l.gsub(/.*#{@eig} (.*)\s*$/, '\1').split(/[\s,]+/) + end.flatten.uniq.map {|t| camel(t)} + end + + def setting_as_is(lines) + lines.grep(/#{@eai}/) do |l| + l.gsub(/.*#{@eai} (.*)\s*$/, '\1').split(/[\s,]+/) + end.flatten.uniq.map {|t| camel(t)} + end + + def setting_async(lines) + async = lines.grep(/#{@eas}/) do |l| + l.gsub(/.*#{@eas} (.*)\s*$/, '\1') == 'true' + end.flatten.last + async.nil? ? true : async end end end