numeric_with_unit 0.0.2 → 0.0.3
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/numeric_with_unit/unit.rb +24 -24
- data/lib/numeric_with_unit.rb +5 -2
- metadata +2 -2
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 5a9e3bf253a2b7b59618f970d6c3dbdd22ff33b7
         | 
| 4 | 
            +
              data.tar.gz: 13adf31a8364d7ffa4e88c283ba0b67738c114e9
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 5938fcabeb8013050901f66e98888a78a02ab65fb175b3341747e91913044b570efe5c3d18a36683a388ab341b05877759200824e269b26bbe78e744e932434f
         | 
| 7 | 
            +
              data.tar.gz: 1154f4a257116d39df130e411e9c3021bbb4519ec851d76d345f43405cf3a5345d1ad7680e4d0d4b9d9e4123a2e9f4f06e1c36e5e1a04c7fa1cc1fd2e4be1f04
         | 
| @@ -1,20 +1,17 @@ | |
| 1 1 | 
             
            # coding: utf-8
         | 
| 2 2 |  | 
| 3 | 
            -
             | 
| 4 3 | 
             
            class NumericWithUnit
         | 
| 5 4 | 
             
              class Unit
         | 
| 6 5 | 
             
                class Config
         | 
| 7 | 
            -
                  attr_reader :symbol, :dimension | 
| 6 | 
            +
                  attr_reader :symbol, :dimension
         | 
| 8 7 | 
             
                  attr_reader :si
         | 
| 9 8 |  | 
| 10 | 
            -
                  def initialize( | 
| 9 | 
            +
                  def initialize()
         | 
| 11 10 | 
             
                    @symbol = nil
         | 
| 12 11 | 
             
                    @dimension = Hash.new(0)
         | 
| 13 12 | 
             
                    @from_si = nil
         | 
| 14 13 | 
             
                    @to_si = nil
         | 
| 15 14 | 
             
                    @si = false
         | 
| 16 | 
            -
                    
         | 
| 17 | 
            -
                    @parent = parent
         | 
| 18 15 | 
             
                  end
         | 
| 19 16 |  | 
| 20 17 | 
             
                  def compile
         | 
| @@ -23,9 +20,6 @@ class NumericWithUnit | |
| 23 20 | 
             
                    @from_si ||= ->(x){x}
         | 
| 24 21 | 
             
                    @to_si ||= ->(x){x}
         | 
| 25 22 |  | 
| 26 | 
            -
                    @derivation = Hash.new(0)
         | 
| 27 | 
            -
                    @derivation[@parent] += 1 unless @parent.nil?
         | 
| 28 | 
            -
                    
         | 
| 29 23 | 
             
                    self
         | 
| 30 24 | 
             
                  end
         | 
| 31 25 |  | 
| @@ -122,9 +116,10 @@ class NumericWithUnit | |
| 122 116 |  | 
| 123 117 | 
             
                  h = derivation.sort_by{|u,v| u.symbol}.sort_by{|u,v| v} # ←どうしよう
         | 
| 124 118 |  | 
| 125 | 
            -
                   | 
| 126 | 
            -
                   | 
| 127 | 
            -
                  symbol =  | 
| 119 | 
            +
                  syms_pos = h.select{|u,v| v > 0}.map{|u,v| u.symbol + ((v.abs>1) ? v.abs.to_s : '')}
         | 
| 120 | 
            +
                  syms_neg = h.select{|u,v| v < 0}.map{|u,v| u.symbol + ((v.abs>1) ? v.abs.to_s : '')}
         | 
| 121 | 
            +
                  symbol = syms_pos.join('.')
         | 
| 122 | 
            +
                  symbol += '/' + (syms_neg.size==1 ? "#{syms_neg.first}" : "(#{syms_neg.join('.')})") unless syms_neg.empty?
         | 
| 128 123 |  | 
| 129 124 | 
             
                  derivation.each do |u,v|
         | 
| 130 125 | 
             
                    u.dimension.each do |d,i|
         | 
| @@ -158,11 +153,11 @@ class NumericWithUnit | |
| 158 153 | 
             
                    ->(x){memo[prc[x]]}
         | 
| 159 154 | 
             
                  }
         | 
| 160 155 |  | 
| 161 | 
            -
                  self.new{| | 
| 162 | 
            -
                     | 
| 163 | 
            -
                     | 
| 164 | 
            -
                     | 
| 165 | 
            -
                     | 
| 156 | 
            +
                  self.new(derivation){|config|
         | 
| 157 | 
            +
                    config.symbol = symbol
         | 
| 158 | 
            +
                    config.dimension = dimension
         | 
| 159 | 
            +
                    config.from_si = from_si
         | 
| 160 | 
            +
                    config.to_si = to_si
         | 
| 166 161 | 
             
                  }
         | 
| 167 162 | 
             
                end
         | 
| 168 163 |  | 
| @@ -327,9 +322,9 @@ class NumericWithUnit | |
| 327 322 | 
             
                attr_reader :symbol
         | 
| 328 323 | 
             
                attr_reader :dimension, :derivation
         | 
| 329 324 |  | 
| 330 | 
            -
                def initialize
         | 
| 325 | 
            +
                def initialize(derivation=nil)
         | 
| 331 326 | 
             
                  # Unit::Configとinitializeの役割が分離できていないので見なおせ
         | 
| 332 | 
            -
                  config = Config.new | 
| 327 | 
            +
                  config = Config.new
         | 
| 333 328 | 
             
                  yield(config) if block_given?
         | 
| 334 329 | 
             
                  config.compile
         | 
| 335 330 |  | 
| @@ -338,17 +333,21 @@ class NumericWithUnit | |
| 338 333 | 
             
                  @from_si = config.from_si
         | 
| 339 334 | 
             
                  @to_si = config.to_si
         | 
| 340 335 |  | 
| 341 | 
            -
                   | 
| 336 | 
            +
                  unless derivation
         | 
| 337 | 
            +
                    derivation = Hash.new(0)
         | 
| 338 | 
            +
                    derivation[self] += 1
         | 
| 339 | 
            +
                  end
         | 
| 340 | 
            +
                  @derivation = derivation
         | 
| 342 341 | 
             
                end
         | 
| 343 342 |  | 
| 344 343 | 
             
                # create new unit with new symbol and factor from self.
         | 
| 345 344 | 
             
                # use for converting [in] = 25.4[mm] .
         | 
| 346 345 | 
             
                def cast(new_symbol, factor = 1)
         | 
| 347 | 
            -
                  self.class.new do | | 
| 348 | 
            -
                     | 
| 349 | 
            -
                     | 
| 350 | 
            -
                     | 
| 351 | 
            -
                     | 
| 346 | 
            +
                  self.class.new do |config|
         | 
| 347 | 
            +
                    config.symbol = new_symbol
         | 
| 348 | 
            +
                    config.dimension = @dimension
         | 
| 349 | 
            +
                    config.from_si = ->(x){from_si(x.quo(factor))}
         | 
| 350 | 
            +
                    config.to_si = ->(x){to_si(x * factor)}
         | 
| 352 351 | 
             
                  end
         | 
| 353 352 | 
             
                end
         | 
| 354 353 |  | 
| @@ -409,6 +408,7 @@ class NumericWithUnit | |
| 409 408 | 
             
                    end
         | 
| 410 409 | 
             
                  end
         | 
| 411 410 | 
             
                end
         | 
| 411 | 
            +
                
         | 
| 412 412 | 
             
              end
         | 
| 413 413 |  | 
| 414 414 | 
             
              class Unit
         | 
    
        data/lib/numeric_with_unit.rb
    CHANGED
    
    | @@ -187,8 +187,11 @@ end | |
| 187 187 |  | 
| 188 188 | 
             
            class String
         | 
| 189 189 | 
             
              def to_nwu(mthd=:to_r)
         | 
| 190 | 
            -
                 | 
| 191 | 
            -
                 | 
| 190 | 
            +
                # 適当
         | 
| 191 | 
            +
                m = self.match /.*?(?=[\s\(\[])/
         | 
| 192 | 
            +
                value = m.to_s
         | 
| 193 | 
            +
                unit = m.post_match.strip.gsub(/^\[|\]$/, '')
         | 
| 194 | 
            +
                NumericWithUnit.new(value.__send__(mthd), unit)
         | 
| 192 195 | 
             
              end
         | 
| 193 196 | 
             
            end
         | 
| 194 197 |  | 
    
        metadata
    CHANGED
    
    | @@ -1,14 +1,14 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: numeric_with_unit
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0.0. | 
| 4 | 
            +
              version: 0.0.3
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - diaphragm
         | 
| 8 8 | 
             
            autorequire: 
         | 
| 9 9 | 
             
            bindir: bin
         | 
| 10 10 | 
             
            cert_chain: []
         | 
| 11 | 
            -
            date: 2015- | 
| 11 | 
            +
            date: 2015-02-03 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies: []
         | 
| 13 13 | 
             
            description: This gem provide NumericWithUnit class to calculate numeric with unit
         | 
| 14 14 | 
             
              of measurement.
         |