press 0.1 → 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.
- data/Gemfile +3 -0
- data/Gemfile.lock +14 -0
- data/lib/press.rb +20 -0
- data/lib/press/printer.rb +59 -0
- metadata +6 -2
    
        data/Gemfile
    ADDED
    
    
    
        data/Gemfile.lock
    ADDED
    
    
    
        data/lib/press.rb
    ADDED
    
    | @@ -0,0 +1,20 @@ | |
| 1 | 
            +
            require "press/printer"
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            module Press
         | 
| 4 | 
            +
             | 
| 5 | 
            +
              def print(*data, &blk)
         | 
| 6 | 
            +
                Printer.print *data, &blk
         | 
| 7 | 
            +
              end
         | 
| 8 | 
            +
             | 
| 9 | 
            +
              def printfm(file, m, *data, &blk)
         | 
| 10 | 
            +
                Printer.printfm file, m, *data, &blk
         | 
| 11 | 
            +
              end
         | 
| 12 | 
            +
             | 
| 13 | 
            +
              def printe(e, *data)
         | 
| 14 | 
            +
                Printer.printe e, *data
         | 
| 15 | 
            +
              end
         | 
| 16 | 
            +
             | 
| 17 | 
            +
              def printfme(file, m, e, *data)
         | 
| 18 | 
            +
                Printer.printfme file, m, e, *data
         | 
| 19 | 
            +
              end
         | 
| 20 | 
            +
            end
         | 
| @@ -0,0 +1,59 @@ | |
| 1 | 
            +
            require "time"
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            module Press
         | 
| 4 | 
            +
              module Printer
         | 
| 5 | 
            +
             | 
| 6 | 
            +
                def self.print(*data, &blk)
         | 
| 7 | 
            +
                  write $stdout, hashify(*data), &blk
         | 
| 8 | 
            +
                end
         | 
| 9 | 
            +
             | 
| 10 | 
            +
                def self.printfm(file, m, *data, &blk)
         | 
| 11 | 
            +
                  write $stdout, hashify(*data, file: File.basename(file, ".rb"), fn: m), &blk
         | 
| 12 | 
            +
                end
         | 
| 13 | 
            +
             | 
| 14 | 
            +
                def self.printe(e, *data)
         | 
| 15 | 
            +
                  write $stderr, hashify(*data, at: "error", class: e.class, message: e.message)
         | 
| 16 | 
            +
                end
         | 
| 17 | 
            +
             | 
| 18 | 
            +
                def self.printfme(file, m, e, *data)
         | 
| 19 | 
            +
                  write $stderr, hashify(*data, file: File.basename(file, ".rb"), fn: m, at: "error", class: e.class, message: e.message)
         | 
| 20 | 
            +
                end
         | 
| 21 | 
            +
             | 
| 22 | 
            +
                def self.hashify(*data, initial)
         | 
| 23 | 
            +
                  data.compact.reduce(initial) { |d, v| d.merge v }
         | 
| 24 | 
            +
                end
         | 
| 25 | 
            +
             | 
| 26 | 
            +
                def self.stringify(data)
         | 
| 27 | 
            +
                  data.map do |(k, v)|
         | 
| 28 | 
            +
                    case v
         | 
| 29 | 
            +
                    when Hash
         | 
| 30 | 
            +
                      "#{k}={.."
         | 
| 31 | 
            +
                    when Array
         | 
| 32 | 
            +
                      "#{k}=[.."
         | 
| 33 | 
            +
                    when NilClass
         | 
| 34 | 
            +
                      "#{k}=nil"
         | 
| 35 | 
            +
                    when Float
         | 
| 36 | 
            +
                      "#{k}=#{format("%.3f", v)}"
         | 
| 37 | 
            +
                    when Time
         | 
| 38 | 
            +
                      "#{k}=#{v.iso8601}"
         | 
| 39 | 
            +
                    else
         | 
| 40 | 
            +
                      v_str = v.to_s
         | 
| 41 | 
            +
                      v_str.match(/\s/) ? "#{k}=\"#{v_str}\"" : "#{k}=#{v_str}"
         | 
| 42 | 
            +
                    end
         | 
| 43 | 
            +
                  end.join(" ")
         | 
| 44 | 
            +
                end
         | 
| 45 | 
            +
             | 
| 46 | 
            +
                def self.write(file, data, &blk)
         | 
| 47 | 
            +
                  unless blk
         | 
| 48 | 
            +
                    file.puts stringify(data)
         | 
| 49 | 
            +
                    file.flush
         | 
| 50 | 
            +
                  else
         | 
| 51 | 
            +
                    start = Time.now
         | 
| 52 | 
            +
                    write file, data.merge(at: "start")
         | 
| 53 | 
            +
                    result = yield
         | 
| 54 | 
            +
                    write file, data.merge(at: "finish", elapsed: Time.now - start)
         | 
| 55 | 
            +
                    result
         | 
| 56 | 
            +
                  end
         | 
| 57 | 
            +
                end
         | 
| 58 | 
            +
              end
         | 
| 59 | 
            +
            end
         | 
    
        metadata
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: press
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: '0. | 
| 4 | 
            +
              version: '0.2'
         | 
| 5 5 | 
             
              prerelease: 
         | 
| 6 6 | 
             
            platform: ruby
         | 
| 7 7 | 
             
            authors:
         | 
| @@ -16,7 +16,11 @@ email: mark.fine@gmail.com | |
| 16 16 | 
             
            executables: []
         | 
| 17 17 | 
             
            extensions: []
         | 
| 18 18 | 
             
            extra_rdoc_files: []
         | 
| 19 | 
            -
            files: | 
| 19 | 
            +
            files:
         | 
| 20 | 
            +
            - lib/press/printer.rb
         | 
| 21 | 
            +
            - lib/press.rb
         | 
| 22 | 
            +
            - Gemfile
         | 
| 23 | 
            +
            - Gemfile.lock
         | 
| 20 24 | 
             
            homepage: http://github.com/mfine/press
         | 
| 21 25 | 
             
            licenses: []
         | 
| 22 26 | 
             
            post_install_message: 
         |