Sha256: 74ab16b7b5d0574363fd575883069e7a71da740db48e5f1953f431b34288e158

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

module Unitwise
  class Atom < Base
    attr_accessor :classification, :property, :metric, :special
    attr_accessor :arbitrary, :dim

    include Unitwise::Composable

    class << self
      def data
        @data ||= data_files.reduce([]){|m,f| m += YAML::load File.open(f)}
      end

      def data_files
        %w(base_unit derived_unit).map{|type| Unitwise.data_file type}
      end
    end

    def base?
      scale.nil? && !dim.nil?
    end

    def derived?
      !base?
    end

    def metric?
      base? ? true : !!metric
    end

    def nonmetric?
      !metric?
    end

    def special?
      !!special
    end

    def arbitrary?
      !!arbitrary
    end

    def depth
      base? ? 0 : scale.depth + 1
    end

    def terminal?
      depth <= 3
    end

    def key
      base? ? dim : property
    end

    def scale=(attrs)
      @scale = if attrs[:function_code]
        Functional.new(attrs[:value], attrs[:unit_code], attrs[:function_code])
      else
        Scale.new(attrs[:value], attrs[:unit_code])
      end
    end

    def scalar
      base? ? 1 : scale.scalar
    end

    def functional(x, direction=1)
      scale.functional(x, direction)
    end


    def root_terms
      base? ? [Term.new(atom: self)] : scale.root_terms
    end

    def to_s
      "#{primary_code} (#{names.join('|')})"
    end

    def inspect
      "<#{self.class} #{to_s}>"
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
unitwise-0.1.0 lib/unitwise/atom.rb