Method: Marty::DataGrid.parse_fvalue

Defined in:
app/models/marty/data_grid.rb

.parse_fvalue(pt, passed_val, type, klass, strict_null_mode = false) ⇒ Object



593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
# File 'app/models/marty/data_grid.rb', line 593

def self.parse_fvalue(pt, passed_val, type, klass, strict_null_mode = false)
  return unless passed_val

  v = remove_not(passed_val)

  return nil if null_value?(v, strict_null_mode)

  case type
  when 'numrange', 'int4range'
    Marty::Util.human_to_pg_range(v)
  when 'integer'
    v.split(ARRSEP).map do |val|
      next nil if null_value?(val, strict_null_mode)

      Integer(val) rescue raise "invalid integer: #{val}"
    end.uniq.sort_by(&:to_i)
  when 'float'
    v.split(ARRSEP).map do |val|
      next nil if null_value?(val, strict_null_mode)

      Float(val) rescue raise "invalid float: #{val}"
    end.uniq.sort
  when 'string'
    res = v.split(ARRSEP).uniq.sort.map do |val|
      next nil if null_value?(val, strict_null_mode)

      val
    end

    raise 'leading/trailing spaces in elements not allowed' if res.any? do |x|
      x != x&.strip
    end

    raise '0-length string not allowed' if res.any? do |x|
      x&.empty?
    end

    res
  when 'boolean'
    return nil if null_value?(v, strict_null_mode)

    case v.downcase
    when 'true', 't'
      true
    when 'false', 'f'
      false
    else
      raise "bad boolean #{v}"
    end
  else
    # AR class
    # FIXME: won't work if the obj identifier (name) has ARRSEP
    res = v.split(ARRSEP).uniq
    res.each do |k|
      begin
        # check to see if class instance actually exists
        Marty::DataGrid.
          find_class_instance(pt, klass, k) || raise(NoMethodError)
      rescue NoMethodError
        raise "instance #{k} of #{type} not found"
      end
    end
    res
  end
end