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]
- Key_Not_Found =
lambda { |hash, key| fail ArgumentError, "Key not found: #{key.inspect}" }
Instance Attribute Summary collapse
-
#error ⇒ Object
readonly
Instance Methods ===============.
Class Method Summary collapse
Instance Method Summary collapse
- #clean(*args) ⇒ Object
- #create(new_data) ⇒ Object
- #create? ⇒ Boolean
- #delete? ⇒ Boolean
- #error? ⇒ Boolean
- #fail!(msg) ⇒ Object
- #field(*args) ⇒ Object
- #field?(*args) ⇒ Boolean
- #field_name(*args) ⇒ Object
- #initialize(unknown = nil) ⇒ Object
-
#new_data ⇒ Object
def clean.
- #on(*args) ⇒ Object
- #primary_key ⇒ Object
- #read? ⇒ Boolean
- #skip(name) ⇒ Object
- #update(new_data) ⇒ Object
- #update? ⇒ Boolean
Instance Attribute Details
#error ⇒ Object (readonly)
Instance Methods ===============
457 458 459 |
# File 'lib/datoki.rb', line 457 def error @error end |
Class Method Details
.db(db = :return) ⇒ Object
29 30 31 32 33 |
# File 'lib/datoki.rb', line 29 def db db = :return return @db if db == :return @db = db @tables = @db.tables end |
.db_type_to_ruby(type, alt = nil) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/datoki.rb', line 35 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
24 25 26 27 |
# File 'lib/datoki.rb', line 24 def included klass klass.extend Def_Field klass.initialize_def_field end |
Instance Method Details
#clean(*args) ⇒ Object
530 531 532 533 534 535 536 537 538 539 540 541 542 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 |
# File 'lib/datoki.rb', line 530 def clean *args if args.empty? @clean ||= begin h = {} h.default_proc = Key_Not_Found h end return @clean end if args.size > 1 return args.each { |f| clean f } end name = args.first required = false if self.class.fields_as_required[name] name = self.class.fields_as_required[name] required = true end field_name(name) = self.class.fields[name] required = true if (!field[:allow][:null] && (!@raw.has_key?(name) || @raw[name] == nil)) # === Did the programmer forget to set the value?: if required && (!@raw.has_key?(name) || @raw[name].nil?) fail ArgumentError, "#{name.inspect} is not set." end # === Skip this if nothing is set and is null-able: if !required && field[:allow][:null] && !@raw.has_key?(name) && !clean.has_key?(name) return nil end clean[name] = @raw[name] unless clean.has_key?(name) # === Should we let the DB set the value? if self.class.schema[name] && self.class.schema[name][:default] && (!clean.has_key?(name) || !clean[name]) clean.delete name return self.class.schema[name][:default] end # === Strip the value: if clean[name].is_a?(String) && field[:allow][:strip] clean[name].strip! end if field?(:chars) && !field.has_key?(:min) && clean[name].is_a?(String) && field[:allow][:null] clean[name] = nil end if field?(:numeric) && clean[name].is_a?(String) clean_val = Integer(clean[name]) rescue String if clean_val == String fail! "!English_name must be numeric." else clean[name] = clean_val end end if field?(:text) && clean[name].is_a?(String) && clean[name].empty? && field[:min].to_i > 0 fail! "!English_name is required." end # ================================ # === check min, max ====== if clean[name].is_a?(String) || clean[name].is_a?(Numeric) case [field[:min], field[:max]].map(&:class) when [NilClass, NilClass] # do nothing when [NilClass, Fixnum] case when clean[name].is_a?(String) && clean[name].size > field[:max] fail! "!English_name can't be longer than !max characters." when clean[name].is_a?(Numeric) && clean[name] > field[:max] fail! "!English_name can't be higher than !max." end when [Fixnum, NilClass] case when clean[name].is_a?(String) && clean[name].size < field[:min] fail! "!English_name can't be shorter than !min characters." when clean[name].is_a?(Numeric) && clean[name] < field[:min] fail! "!English_name can't be less than !min." end when [Fixnum, Fixnum] case when clean[name].is_a?(String) && (clean[name].size < field[:min] || clean[name].size > field[:max]) fail! "!English_name must be between !min and !max characters." when clean[name].is_a?(Numeric) && (clean[name] < field[:min] || clean[name] > 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) if clean[name].nil? && !field[:allow][:null] clean[name] = clean[name].to_i end end # ================================ # === :strip if necessary ======== if field?(:chars) && field[:allow][:strip] && clean[name].is_a?(String) clean[name] = clean[name].strip end # ================================ # === Is value in options? ======= if field[:options] if !field[:options].include?(clean[name]) 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) && !clean[name].is_a?(Integer) fail! "!English_name needs to be an integer." when field?(:chars) && !clean[name].is_a?(String) fail! "!English_name needs to be a String." end when :exact_size if clean[name].size != field[:exact_size] case when field?(:chars) || clean[name].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| clean[name] = 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 clean[name] == target } when :included_in arr, msg, other = args fail!(msg || "!English_name must be one of these: #{arr.join ', '}") unless arr.include?(clean[name]) when :upcase clean[name] = clean[name].upcase when :match args.each { |pair| regex, msg, other = pair if clean[name] !~ regex fail!(msg || "!English_name must match #{regex.inspect}") end } when :not_match args.each { |pair| regex, msg, other = pair if clean[name] =~ regex fail!(msg || "!English_name must not match #{regex.inspect}") end } else fail "Cleaner not implemented: #{cleaner.inspect}" end # === case cleaner } # === field[:cleaners].each end |
#create(new_data) ⇒ Object
785 786 787 788 789 |
# File 'lib/datoki.rb', line 785 def create new_data @new_data = new_data run :create self end |
#create? ⇒ Boolean
803 804 805 806 |
# File 'lib/datoki.rb', line 803 def create? (@raw.has_key?(:create) && @raw[:create]) || @raw.has_key?(primary_key[:name]) && !@raw[primary_key[:name]] end |
#delete? ⇒ Boolean
816 817 818 |
# File 'lib/datoki.rb', line 816 def delete? !!(@raw.has_key?(:delete) && !@raw[:delete]) end |
#error? ⇒ Boolean
526 527 528 |
# File 'lib/datoki.rb', line 526 def error? @error && !@error.empty? end |
#fail!(msg) ⇒ Object
738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 |
# File 'lib/datoki.rb', line 738 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 } @error = {:field_name=>field_name, :msg=>err_msg, :value=>clean[field_name]} throw :invalid, self end |
#field(*args) ⇒ Object
770 771 772 773 774 775 776 777 778 779 |
# File 'lib/datoki.rb', line 770 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
781 782 783 |
# File 'lib/datoki.rb', line 781 def field? *args self.class.inspect_field? :type, field_name, *args end |
#field_name(*args) ⇒ Object
757 758 759 760 761 762 763 764 765 766 767 768 |
# File 'lib/datoki.rb', line 757 def field_name *args case args.size when 0 fail "Field name not set." unless @field_name @field_name when 1 fail ArgumentError, "Unknown field: #{args.first.inspect}" unless self.class.fields[args.first] @field_name = args.first else fail "Unknown args: #{args.inspect}" end end |
#initialize(unknown = nil) ⇒ Object
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 |
# File 'lib/datoki.rb', line 458 def initialize unknown = nil @data = nil @field_name = nil @clean = nil @error = nil @skips = {} if unknown if unknown.keys.all? { |f| self.class.fields.has_key?(f) } @data = unknown @data.default_proc = Key_Not_Found else @raw = unknown @raw.default_proc = Key_Not_Found end end if @raw self.class.on_doc.each { |raw_arr| conds = raw_arr.first func = raw_arr.last instance_eval(&func) if conds.all? { |cond| case cond when Symbol send(cond) when Proc cond.arity == 1 ? cond.call(@raw) : instance_eval(&cond) when TrueClass, FalseClass cond else fail ArgumentError, "Unknown: #{cond.inspect}" end } } # === on_doc.each if !@clean @raw.each { |k, v| clean(k) if self.class.fields.has_key?(k) } end if create? self.class.fields.each { |k, | if !clean.has_key?(k) && ![:allow][:null] && ![:primary_key] fail ArgumentError, "#{k.inspect} is not set." end } end case when create? insert_into_table unless !respond_to?(:insert_into_table) when update? alter_record unless !respond_to?(:alter_record) when delete? delete_from_table unless !respond_to?(:delete_from_table) end unless @skips[:db] end # === if @raw self.class.schema_match(:all) end |
#new_data ⇒ Object
def clean
720 721 722 |
# File 'lib/datoki.rb', line 720 def new_data @new_data ||= {} end |
#on(*args) ⇒ Object
724 725 726 727 728 729 730 731 732 733 734 735 736 |
# File 'lib/datoki.rb', line 724 def on *args fail ArgumentError, "No conditions." if args.empty? yield if args.all? { |cond| case cond when Symbol send(cond) when TrueClass, FalseClass cond else fail ArgumentError, "Unknown value: #{cond.inspect}" end } end |
#primary_key ⇒ Object
797 798 799 800 801 |
# File 'lib/datoki.rb', line 797 def primary_key arr = self.class.fields.detect { |k, v| v[:primary_key] } fail "Primary key not found." unless arr arr.last end |
#read? ⇒ Boolean
808 809 810 |
# File 'lib/datoki.rb', line 808 def read? !!(@raw.has_key?(:read) && @raw[:read]) end |
#skip(name) ⇒ Object
522 523 524 |
# File 'lib/datoki.rb', line 522 def skip name @skips[name] = true end |
#update(new_data) ⇒ Object
791 792 793 794 795 |
# File 'lib/datoki.rb', line 791 def update new_data @new_data = new_data run :update self end |
#update? ⇒ Boolean
812 813 814 |
# File 'lib/datoki.rb', line 812 def update? !!(@raw.has_key?(:update) && @raw[:update]) end |