Skip to content

Value objects

zverok edited this page Apr 11, 2016 · 3 revisions

For smoother experience and prettier demonstrations, Reality wraps several kinds of data in simple-yet-useful value objects.

They may be (or may be not) replaced with additional gems in future versions.

Reality::Measure

Just a numerical value with units.

argentina.population
# => #<Reality::Measure(43,417,000 person)>
argentina.population.amount
# => 43,417,000
argentina.population.unit
# => #<Reality::Measure::Unit(person)>
argentina.population ** 2
# => #<Reality::Measure(1,885,035,889,000,000 person²)>
argentina.population / argentina.area
# => #<Reality::Measure(15 person/km²)>

# using on its own:
m = Reality::Measure.new(10, 'm')
# => #<Reality::Measure(10 m)> 
m ** 2
# => #<Reality::Measure(100 m²)>

NB: No measure conversion provided for now. Attempt to sum metres with kilometres will fail miserably. unitwise may be utilised instead or inside Measure in future.

Reality::Geo::Coord

Wrapper around (latitude, longitude) pair.

argentina.capital.coord
# => #<Reality::Geo::Coord(34°35′58″S,58°22′54″W)>
argentina.capital.coord.to_s
# => "-34.599722222222,-58.381944444444"
argentina.capital.coord.distance_to(argentina.highest_point.coord)
# => #<Reality::Measure(1,097 km)>
# argentina.capital.coord.distance_to(argentina.highest_point) also can be used, if highest_point has coord method
argentina.capital.coord.direction_to(argentina.highest_point.coord)
# => #<Reality::Measure(278 °)>

NB: geokit already somehow utilized inside.

Some of external services are mashed in Coord object and make it even more useful.

Reality::TZOffset

Wrapper around timezone offset (in hours and minutes). Useful when only numeric value of offset is known (no timezone name).

argentina.tz_offset
# => #<Reality::TZOffset(UTC-03:00)>
argentina.tz_offset.now
# => 2016-03-01 16:03:52 -0300
argentina.tz_offset.local(2016, 3, 2, 14, 30)
# => 2016-03-02 14:30:00 -0300
argentina.tz_offset.convert(Time.now)
# => 2016-03-01 16:04:36 -0300

# using on its own:
Reality::TZOffset.parse('GMT+1').now
# => 2016-03-01 20:05:10 +0100

Next: using external services other than Wikipedia/Wikidata.

Clone this wiki locally