type-enforcer 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/type-enforcer/base.rb +60 -16
- metadata +1 -1
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: a27db785362b083e806346c71fe4bda84d22d073
         | 
| 4 | 
            +
              data.tar.gz: 6fc452f5a0e7d87dccb29f2f5e7ed2047028e894
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: bd204e0790cec27cb94be37028a1b3c05fe846602756207fa63d9a599ce835abf4bd90ae44f0eb6456abe561de165180d6389d5f0e2c4bd391408548186cdc06
         | 
| 7 | 
            +
              data.tar.gz: 09857f101fc4ad5881975dcf82b98f80dfc0065e6fdb00c9e30bc9f5b0f788caf04c1c1b1e80dde01e1feb744ef906a888afd84b26aba3aae43ee4d247e8da93
         | 
    
        data/lib/type-enforcer/base.rb
    CHANGED
    
    | @@ -1,23 +1,39 @@ | |
| 1 1 | 
             
            module TypeEnforcer
         | 
| 2 2 | 
             
              class Error < StandardError; end
         | 
| 3 | 
            -
              class  | 
| 3 | 
            +
              class NotFulfilledError < Error; end
         | 
| 4 | 
            +
              class NotPresentError < Error; end
         | 
| 4 5 |  | 
| 5 6 | 
             
              refine Object do
         | 
| 6 | 
            -
                def enforce( | 
| 7 | 
            -
                   | 
| 8 | 
            -
             | 
| 9 | 
            -
             | 
| 7 | 
            +
                def enforce(*args, &block)
         | 
| 8 | 
            +
                  options = TypeEnforcer.build_options(args, error: nil)
         | 
| 9 | 
            +
             | 
| 10 | 
            +
                  if block_given?
         | 
| 11 | 
            +
                    if try(*args, &block)
         | 
| 12 | 
            +
                      self
         | 
| 13 | 
            +
                    else
         | 
| 14 | 
            +
                      TypeEnforcer.raise_or_return(options[:error])
         | 
| 15 | 
            +
                    end
         | 
| 10 16 | 
             
                  else
         | 
| 11 | 
            -
                     | 
| 17 | 
            +
                    case type = args.first
         | 
| 18 | 
            +
                    when Symbol
         | 
| 19 | 
            +
                      if TypeEnforcer.try(type, self, *args[1..-1]) || try(*args)
         | 
| 20 | 
            +
                        self
         | 
| 21 | 
            +
                      else
         | 
| 22 | 
            +
                        TypeEnforcer.raise_or_return(options[:error])
         | 
| 23 | 
            +
                      end
         | 
| 24 | 
            +
                    else
         | 
| 25 | 
            +
                      if self.acts_as?(type)
         | 
| 26 | 
            +
                        self.try(:convert_to, *args) || self
         | 
| 27 | 
            +
                      else
         | 
| 28 | 
            +
                        TypeEnforcer.raise_or_return(options[:error])
         | 
| 29 | 
            +
                      end
         | 
| 30 | 
            +
                    end
         | 
| 12 31 | 
             
                  end
         | 
| 13 32 | 
             
                end
         | 
| 14 33 | 
             
                alias_method :e, :enforce
         | 
| 15 34 |  | 
| 16 | 
            -
                def enforce!( | 
| 17 | 
            -
                   | 
| 18 | 
            -
             | 
| 19 | 
            -
                  raise TypeError if enforced.nil?
         | 
| 20 | 
            -
                  enforced
         | 
| 35 | 
            +
                def enforce!(*args, &block)
         | 
| 36 | 
            +
                  enforce(*args, {error: NotFulfilledError}, &block)
         | 
| 21 37 | 
             
                end
         | 
| 22 38 | 
             
                alias_method :e!, :enforce!
         | 
| 23 39 |  | 
| @@ -29,18 +45,46 @@ module TypeEnforcer | |
| 29 45 | 
             
                  !nil?
         | 
| 30 46 | 
             
                end
         | 
| 31 47 |  | 
| 32 | 
            -
                def present!
         | 
| 33 | 
            -
                   | 
| 34 | 
            -
             | 
| 48 | 
            +
                def present!(error: NotPresentError)
         | 
| 49 | 
            +
                  unless present?
         | 
| 50 | 
            +
                    TypeEnforcer.raise_or_return(error)
         | 
| 51 | 
            +
                  else
         | 
| 52 | 
            +
                    self
         | 
| 53 | 
            +
                  end
         | 
| 35 54 | 
             
                end
         | 
| 36 55 | 
             
                alias_method :p!, :present!
         | 
| 37 56 |  | 
| 38 | 
            -
                def try( | 
| 39 | 
            -
                   | 
| 57 | 
            +
                def try(*args)
         | 
| 58 | 
            +
                  options = TypeEnforcer.build_options(args, rescues: StandardError)
         | 
| 59 | 
            +
                  begin
         | 
| 60 | 
            +
                    if block_given?
         | 
| 61 | 
            +
                      yield(self, *args)
         | 
| 62 | 
            +
                    else
         | 
| 63 | 
            +
                      send(*args)
         | 
| 64 | 
            +
                    end
         | 
| 65 | 
            +
                  rescue options[:rescues]
         | 
| 66 | 
            +
                    nil
         | 
| 67 | 
            +
                  end
         | 
| 40 68 | 
             
                end
         | 
| 41 69 |  | 
| 42 70 | 
             
                def blank?
         | 
| 43 71 | 
             
                  nil? || try(:empty?)
         | 
| 44 72 | 
             
                end
         | 
| 45 73 | 
             
              end
         | 
| 74 | 
            +
             | 
| 75 | 
            +
              def self.build_options(options, defaults)
         | 
| 76 | 
            +
                if options.is_a?(Array)
         | 
| 77 | 
            +
                  options = options.last.is_a?(Hash) ? options.pop : {}
         | 
| 78 | 
            +
                end
         | 
| 79 | 
            +
             | 
| 80 | 
            +
                defaults.merge(options)
         | 
| 81 | 
            +
              end
         | 
| 82 | 
            +
             | 
| 83 | 
            +
              def self.raise_or_return(error)
         | 
| 84 | 
            +
                if error.is_a?(Class) && error <= Exception
         | 
| 85 | 
            +
                  raise error
         | 
| 86 | 
            +
                else
         | 
| 87 | 
            +
                  error
         | 
| 88 | 
            +
                end
         | 
| 89 | 
            +
              end
         | 
| 46 90 | 
             
            end
         |