github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

alexreisner / physical_quantifier

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 1
    • 0
  • Source
  • Commits
  • Network (0)
  • Issues (0)
  • Downloads (0)
  • Wiki (1)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (1)
    • master ✓
  • Tags (0)
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

Rails plugin for managing numbers with units. — Read more

  cancel

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

Add missing Rakefile. 
alexreisner (author)
Fri Jul 17 17:20:17 -0700 2009
commit  c191e94fedc7f6b46c5bb4af440a30616766938e
tree    43b8f5b5eae14227e3b1b326e5f4e60611bb39f9
parent  ef72b3edd8b70ef97f49021864e0da0727e7de5f
physical_quantifier /
name age
history
message
file MIT-LICENSE Loading commit data...
file README.rdoc
file Rakefile
file init.rb
directory lib/
directory tasks/
directory test/
README.rdoc

PhysicalQuantifier

Classes for representing and manipulating physical quantities (numbers with units).

Written by Alex Reisner (alex@alexreisner.com).

Summary

In scientific and engineering applications there is often a need to work with numbers that represent actual real-world things. Quantities (like 42) become "physical" when we attach units to them (like meters or pounds). They then represent properties of real objects like time, mass, length, temperature, etc. PhysicalQuantifier allows your numbers to retain their "physicality" whether they’re stored in a database, loaded into memory, or manipulated mathematically and combined with other quantities of like and unlike units. It also facilitates easy conversion between units of different measurement systems.

Installation

Install PhysicalQuantifier from the Git repository with the Rails plugin script:

  script/plugin install git://github.com/alexreisner/physical_quantifier.git

or outside of Rails without the plugin script:

  git clone git://github.com/alexreisner/physical_quantifier.git

Calculations and Conversions

Examples of basic usage:

  # Define some physical quantities.
  dist = PhysicalQuantity.new(53.125, :m) # 53.125 meters
  time = PhysicalQuantity.new( 4.25,  :s) #  4.250 seconds

  # Calculate a rate.
  speed = dist / time
  speed.to_s
    => "12.5 m/s"

  # Convert to different units.
  speed.convert_to(:km => 1, :h => -1)
  speed.to_s
    => "0.125 km/hr"

  # Compare to another rate (with different but compatible units).
  old_speed = PhysicalQuantity.new(2.1, {:m => 1, :s => -1) # 2.1 m/s
  speed > old_speed
    => true

  # Display with HTML formatting.
  accel = PhysicalQuantity.new(48, :m => 1, :s => -2) # 48 m/s^2
  accel.to_s(:html)
    => "48 m/s<sup>2</sup>"

  # Define custom unit (femtometer = meter x 10^-15).
  Unit.new :fm, 'femtometer', :m, 1e-15
  # You can now use femtometers in calculations, conversions, etc.

Model Integration

To add physical quantities to your existing Rails model (make raw numbers stored in your database "physical"):

        extend PhysicalQuantifier
        physical_quantity 'mm',   :depth, :width, :thickness
        physical_quantity 'mm^2', :coating_area, :surface_area
        physical_quantity 'dm^4', :second_moment_of_area_x, :second_moment_of_area_y
        physical_quantity 'kg/m', :weight_per_length

The above declarations will add methods like these to your model, which will return PhysicalQuantity objects instead of raw numbers:

  depth_qty
  width_qty
  thickness_qty
  ...

If you’d prefer to have your default getter methods return PhysicalQuantity objects, place this declaration after you extend your model and before you declare your physical quantities:

  getters_return_physical_quantities

This will cause the default getter methods (depth, width, thickness, etc) to return PhysicalQuantity objects, and define these methods for accessing the raw numbers stored in your database:

  raw_depth
  raw_width
  raw_thickness
  ...

To-do List

  • Add MeasurementSystem class.
  • Add rounding option to to_s method.
  • Add support for a "displayable" unit symbol for use in to_s method.

Copyright © 2008 Alex Reisner (alex@alexreisner.com), released under the MIT license.

Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server