Module: Datoki

Defined in:
lib/datoki.rb

Defined Under Namespace

Modules: Def_Field

Constant Summary collapse

UTC_NOW_DATE =
::Sequel.lit("CURRENT_DATE")
UTC_NOW_RAW =
"timezone('UTC'::text, now())"
UTC_NOW =
::Sequel.lit("timezone('UTC'::text, now())")
Invalid =
Class.new RuntimeError
Schema_Conflict =
Class.new RuntimeError
Actions =
[:all, :create, :read, :update, :update_or_create, :trash, :delete]
Char_Types =
[:varchar, :text]
Numeric_Types =
[:smallint, :integer, :bigint, :decimal, :numeric]
Types =
Char_Types + Numeric_Types + [:datetime]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.db(db = :return) ⇒ Object



25
26
27
28
29
# File 'lib/datoki.rb', line 25

def db db = :return
  return @db if db == :return
  @db = db
  @tables = @db.tables
end

.db_type_to_ruby(type, alt = nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/datoki.rb', line 31

def db_type_to_ruby type, alt = nil
  if Datoki::Types.include?( type.to_sym )
    type.to_sym
  elsif type['character varying']
    :varchar
  elsif Datoki::Types.include?(alt)
    alt
  else
    fail("Unknown db type: #{type.inspect}")
  end
end

.included(klass) ⇒ Object



20
21
22
23
# File 'lib/datoki.rb', line 20

def included klass
  klass.extend Def_Field
  klass.initialize_def_field
end

Instance Method Details

#clean_dataObject



473
474
475
# File 'lib/datoki.rb', line 473

def clean_data
  @clean_data ||= {}
end

#create(new_data) ⇒ Object



733
734
735
736
737
# File 'lib/datoki.rb', line 733

def create new_data
  @new_data = new_data
  run :create
  self
end

#errorsObject



458
459
460
# File 'lib/datoki.rb', line 458

def errors
  @errors ||= {}
end

#errors?Boolean

Returns:

  • (Boolean)


462
463
464
# File 'lib/datoki.rb', line 462

def errors?
  @errors && !@errors.empty?
end

#fail!(msg) ⇒ Object



481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
# File 'lib/datoki.rb', line 481

def fail! msg
  err_msg = msg.gsub(/!([a-z\_\-]+)/i) { |raw|
    name = $1
    case name
    when "English_name"
      self.class.fields[field_name][:english_name].capitalize.gsub('_', ' ')
    when "ENGLISH_NAME"
      self.class.fields[field_name][:english_name].upcase.gsub('_', ' ')
    when "max", "min", "exact_size"
      self.class.fields[field_name][name.downcase.to_sym]
    else
      fail "Unknown value: #{name}"
    end
  }

  if self.class.record_errors?
    save_error err_msg
    throw :error_saved
  else
    fail Invalid, err_msg
  end
end

#field(*args) ⇒ Object



528
529
530
531
532
533
534
535
536
537
# File 'lib/datoki.rb', line 528

def field *args
  case args.size
  when 0
    self.class.fields[field_name]
  when 1
    self.class.fields[args.first]
  else
    fail "Unknown args: #{args.inspect}"
  end
end

#field?(*args) ⇒ Boolean

Returns:

  • (Boolean)


539
540
541
# File 'lib/datoki.rb', line 539

def field? *args
  self.class.inspect_field? :type, field_name, *args
end

#field_name(*args) ⇒ Object



504
505
506
507
508
509
510
511
512
513
514
# File 'lib/datoki.rb', line 504

def field_name *args
  case args.size
  when 0
    fail "Field name not set." unless @field_name
    @field_name
  when 1
    @field_name = args.first
  else
    fail "Unknown args: #{args.inspect}"
  end
end

#initialize(data = nil) ⇒ Object

Instance Methods ===============


448
449
450
451
452
453
454
455
456
# File 'lib/datoki.rb', line 448

def initialize data = nil
  @data       = nil
  @new_data   = nil
  @field_name = nil
  @clean_data = nil
  @errors     = nil

  self.class.schema_match(:all)
end

#new_dataObject



477
478
479
# File 'lib/datoki.rb', line 477

def new_data
  @new_data ||= {}
end

#run(action) ⇒ Object



543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
# File 'lib/datoki.rb', line 543

def run action
  self.class.fields.each { |f_name, f_meta|

    field_name f_name
    is_set    = new_data.has_key?(field_name)
    is_update = action == :update
    is_nil    = is_set && new_data[field_name].nil?

    # === Should the field be skipped? ===============
    next if !is_set && is_update
    next if !is_set && field[:primary_key]
    next if field[:allow][:null] && (!is_set || is_nil)

    if is_set 
      val! new_data[field_name]
    elsif field.has_key?(:default)
      val! field[:default]
    end

    if val.is_a?(String) && field[:allow][:strip]
      val! val.strip
    end

    if field?(:chars) && !field.has_key?(:min) && val.is_a?(String) && field[:allow][:null]
      val! nil
    end

    catch :error_saved do

      if field?(:numeric) && val.is_a?(String)
        clean_val = Integer(val) rescue String
        if clean_val == String
          fail! "!English_name must be numeric."
        else
          val! clean_val
        end
      end

      # === check required. ============
      if val.nil? && !field[:allow][:null]
        fail! "!English_name is required."
      end

      if field?(:text) && val.is_a?(String) && val.empty? && field[:min].to_i > 0
        fail! "!English_name is required."
      end
      # ================================

      # === check min, max ======
      if val.is_a?(String) || val.is_a?(Numeric)
        case [field[:min], field[:max]].map(&:class)

        when [NilClass, NilClass]
          # do nothing

        when [NilClass, Fixnum]
          case
          when val.is_a?(String) && val.size > field[:max]
            fail! "!English_name can't be longer than !max characters."
          when val.is_a?(Numeric) && val > field[:max]
            fail! "!English_name can't be higher than !max."
          end

        when [Fixnum, NilClass]
          case
          when val.is_a?(String) && val.size < field[:min]
            fail! "!English_name can't be shorter than !min characters."
          when val.is_a?(Numeric) && val < field[:min]
            fail! "!English_name can't be less than !min."
          end

        when [Fixnum, Fixnum]
          case
          when val.is_a?(String) && (val.size < field[:min] || val.size > field[:max])
            fail! "!English_name must be between !min and !max characters."
          when val.is_a?(Numeric) && (val < field[:min] || val > field[:max])
            fail! "!English_name must be between !min and !max."
          end

        else
          fail "Unknown values for :min, :max: #{field[:min].inspect}, #{field[:max].inspect}"
        end
      end # === if
      # ================================

      # === to_i if necessary ==========
      if field?(:numeric)
        val! val.to_i
      end
      # ================================

      # === :strip if necessary ========
      if field?(:chars) && field[:allow][:strip] && val.is_a?(String)
        val! val.strip
      end
      # ================================

      # === Is value in options? =======
      if field[:options]
        if !field[:options].include?(val)
          fail! "!English_name can only be: #{field[:options].map(&:inspect).join ', '}"
        end
      end
      # ================================

      field[:cleaners].each { |cleaner, args|
        next if args === false # === cleaner has been disabled.

          case cleaner

          when :type
            case
            when field?(:numeric) && !val.is_a?(Integer)
              fail! "!English_name needs to be an integer."
            when field?(:chars) && !val.is_a?(String)
              fail! "!English_name needs to be a String."
            end

          when :exact_size
            if val.size != field[:exact_size]
              case
              when field?(:chars) || val.is_a?(String)
                fail! "!English_name needs to be !exact_size in length."
              else
                fail! "!English_name can only be !exact_size in size."
              end
            end

          when :set_to
            args.each { |meth|
              val! send(meth)
            }

          when :equal_to
            args.each { |pair|
              meth, msg, other = pair
              target = send(meth)
              fail!(msg || "!English_name must be equal to: #{target.inspect}") unless val == target
            }

          when :included_in
            arr, msg, other = args
            fail!(msg || "!English_name must be one of these: #{arr.join ', '}") unless arr.include?(val)

          when :upcase
            val! val.upcase

          when :match
            args.each { |pair|
              regex, msg, other = pair
              if val !~ regex
                fail!(msg || "!English_name must match #{regex.inspect}")
              end
            }

          when :not_match
            args.each { |pair|
              regex, msg, other = pair
              if val =~ regex
                fail!(msg || "!English_name must not match #{regex.inspect}")
              end
            }

          else
            fail "Cleaner not implemented: #{cleaner.inspect}"
          end # === case cleaner


      } # === field[:cleaners].each

      field[:on][action].each { |meth, is_enabled|
        next unless is_enabled
        send meth
      } if field[:on][action]

    end # === catch :error_saved
  } # === field

  return if errors?

  self.class.ons.each { |action, meths|
    meths.each { |meth, is_enabled|
      next unless is_enabled
      catch :error_saved do
        send meth
      end
    }
  }
end

#save_error(msg) ⇒ Object



466
467
468
469
470
471
# File 'lib/datoki.rb', line 466

def save_error msg
  @errors ||= {}
  @errors[field_name] ||= {}
  @errors[field_name][:msg] = msg
  @errors[field_name][:value] = val
end

#update(new_data) ⇒ Object



739
740
741
742
743
# File 'lib/datoki.rb', line 739

def update new_data
  @new_data = new_data
  run :update
  self
end

#valObject



516
517
518
519
520
521
522
# File 'lib/datoki.rb', line 516

def val
  if clean_data.has_key?(field_name)
    clean_data[field_name]
  else
    new_data[field_name]
  end
end

#val!(new_val) ⇒ Object



524
525
526
# File 'lib/datoki.rb', line 524

def val! new_val
  clean_data[field_name] = new_val
end