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
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 |
# File 'lib/datoki.rb', line 535 def clean *args @clean ||= {} return @clean if args.empty? # === Handle required fields: # Example: # :name!, :age! if args.size > 1 return args.each { |f| clean f } end name = args.first if (real_name = self.class.fields_as_required[name]) && @raw[real_name].nil? && @clean[real_name].nil? fail ArgumentError, "#{real_name.inspect} is not set." else name = real_name || name end @clean[name] = @raw[name] if !clean.has_key?(name) && @raw.has_key?(name) # === Skip cleaning if key is not set: return nil unless @clean.has_key?(name) field_name(name) = self.class.fields[name] # === 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
774 775 776 777 778 |
# File 'lib/datoki.rb', line 774 def create new_data @new_data = new_data run :create self end |
#create? ⇒ Boolean
792 793 794 795 |
# File 'lib/datoki.rb', line 792 def create? (@raw.has_key?(:create) && @raw[:create]) || @raw.has_key?(primary_key[:name]) && !@raw[primary_key[:name]] end |
#delete? ⇒ Boolean
805 806 807 |
# File 'lib/datoki.rb', line 805 def delete? !!(@raw.has_key?(:delete) && !@raw[:delete]) end |
#error? ⇒ Boolean
531 532 533 |
# File 'lib/datoki.rb', line 531 def error? @error && !@error.empty? end |
#fail!(msg) ⇒ Object
727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 |
# File 'lib/datoki.rb', line 727 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
759 760 761 762 763 764 765 766 767 768 |
# File 'lib/datoki.rb', line 759 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
770 771 772 |
# File 'lib/datoki.rb', line 770 def field? *args self.class.inspect_field? :type, field_name, *args end |
#field_name(*args) ⇒ Object
746 747 748 749 750 751 752 753 754 755 756 757 |
# File 'lib/datoki.rb', line 746 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 521 522 523 524 525 |
# 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 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 schema = self.class.schema if create? # === Did the programmer forget to set the value?: self.class.fields.each { |k, | has_default = schema[k] && schema[k][:default] if clean[k].nil? && ![:allow][:null] && ![:primary_key] && !has_default fail ArgumentError, "#{k.inspect} is not set." end # === Should we let the DB set the value? @clean.delete(k) if @clean[k].nil? && has_default } 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
709 710 711 |
# File 'lib/datoki.rb', line 709 def new_data @new_data ||= {} end |
#on(*args) ⇒ Object
713 714 715 716 717 718 719 720 721 722 723 724 725 |
# File 'lib/datoki.rb', line 713 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
786 787 788 789 790 |
# File 'lib/datoki.rb', line 786 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
797 798 799 |
# File 'lib/datoki.rb', line 797 def read? !!(@raw.has_key?(:read) && @raw[:read]) end |
#skip(name) ⇒ Object
527 528 529 |
# File 'lib/datoki.rb', line 527 def skip name @skips[name] = true end |
#update(new_data) ⇒ Object
780 781 782 783 784 |
# File 'lib/datoki.rb', line 780 def update new_data @new_data = new_data run :update self end |
#update? ⇒ Boolean
801 802 803 |
# File 'lib/datoki.rb', line 801 def update? !!(@raw.has_key?(:update) && @raw[:update]) end |