Conditional test in JSON model

时间:2018-07-17 10:57:59

标签: ruby-on-rails json if-statement model

I need to know how to add conditional if test in rails method when define a json object :

def save_metadata
    if model.class == Medium && file.present? && file.content_type.end_with?( 'jpeg' )
      exif_info = EXIFR::JPEG.new( url_or_path )
      if exif_info.gps.nil?
        model.metadata = {
          width:              exif_info.width,
          height:             exif_info.height,
          image_width:        exif_info.image_width,
          image_length:       exif_info.image_length,
          if exif_info.date_time.utc?
          date_time_original: exif_info.date_time.utc
        }
      else
        model.metadata = {
          width:              exif_info.width,
          height:             exif_info.height,
          make:               exif_info.make,
          model:              exif_info.model,
          date_time_original: exif_info.date_time.utc,
          image_width:        exif_info.image_width,
          image_length:       exif_info.image_length,
          gps_latitude:       exif_info.gps.latitude,
          gps_longitude:      exif_info.gps.longitude,
          gps_date_stamp:     format_date( exif_info ),
          gps_time_stamp:     format_date_time( exif_info )
        }
      end
      model.save!
    end
  end

I want to test if the exif_info.date_time.utc is not nil, i got this error :

syntax error, unexpected ':', expecting keyword_end date_time_original: exif_info.date_time.utc

2 个答案:

答案 0 :(得分:1)

您可以将键date_time_original与哈希值model.metadata中的值合并

model.metadata = {
  width:              exif_info.width,
  height:             exif_info.height,
  image_width:        exif_info.image_width,
  image_length:       exif_info.image_length
}

model.metadata.merge!({date_time_original: exif_info.date_time.utc}) if exif_info.date_time.utc?

您可以根据需要编辑条件

有关merge!的更多信息

答案 1 :(得分:0)

您看到的错误是因为您在构建if时遇到Hash条件。您可以在拥有哈希之后执行此操作。您可以按如下方式有条件地在哈希中设置键的值:

if exif_info.gps.nil?
  model.metadata = {
                     width:        exif_info.width,
                     height:       exif_info.height,
                     image_width:  exif_info.image_width,
                     image_length: exif_info.image_length,
                   }
  model.metadata[:date_time_original] = exif_info.date_time.utc if exif_info.date_time.utc?
else
  model.metadata = {
                     width:              exif_info.width,
                     height:             exif_info.height,
                     make:               exif_info.make,
                     model:              exif_info.model,
                     date_time_original: exif_info.date_time.utc,
                     image_width:        exif_info.image_width,
                     image_length:       exif_info.image_length,
                     gps_latitude:       exif_info.gps.latitude,
                     gps_longitude:      exif_info.gps.longitude,
                     gps_date_stamp:     format_date( exif_info ),
                     gps_time_stamp:     format_date_time( exif_info )
                   }
end