Project

manacle

0.0
No commit activity in last 3 years
No release in over 3 years
Manacle will allow you to attach constraints to your objects that will persist across new instances of similar objects. As an example, you can constrain a Time object to always advance to the next hour. Then, if you add even one minute to that Time, it will snap to the next hour.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

 Project Readme

Gem Version Build Status Code Climate Coverage Status Dependency Status

manacle

Manacle is a library for the creation of 'sticky' constraints.

What is a 'sticky' constraint?

Say you changing pricing a product, and always want the price to end with 99.

Typically, when you modify the price, you'll have to redo the calculation to make it end in 99.

A 'sticky' constraint means you have to declare your intent once, and the price enforces its own constraint as you make changes, even changes that would otherwise make it not end in 99.

Quick Start

A common situation is to want to constrain a Time object in some way or another.

For example, say you want a Time value to always snap to the beginning of the day.

Defining the manacle

require 'manacle/constraint/method'

module Manacle
  module Examples
    class BeginningOfDay
      include ::Manacle::Constraint::Method

      # Defines what types of values this manacle can constrain
      def self.constrainables
        [ActiveSupport::TimeWithZone, ::Time, ::Date] 
      end

      # Defines the constraint to call each time the underlying object is modified.
      def constraint
        :beginning_of_day
      end
    end
  end
end

Using the manacle

time = Time.now
puts "1. Time in your timezone is #{time.to_s}"

# Manacle is applied here
bod = Manacle::Examples::BeginningOfDay.new(time).proxy
puts "2. Manacled time at beginning of the day is #{bod.to_s}"

bod_plus_one_hour = bod + 60*60
puts "3. Manacled time at BOD + 1 hour is #{bod_plus_one_hour.to_s}"

bod_plus_one_day = bod + 24*60*60
puts "4. Manacled time in BOD + 1 day is #{bod_plus_one_day.to_s}"

bod_plus_25_hours = bod + 25*60*60
puts "5. Manacled time in BOD + 25 hours is #{bod_plus_25_hours.to_s}"

If the time was 2014-08-27 21:42:21 -0700, this would print:

1. Time in your timezone is 2014-08-27 21:42:21 -0700
2. Manacled time at beginning of the day is 2014-08-27 00:00:00 -0700
3. Manacled time at BOD + 1 hour is 2014-08-27 00:00:00 -0700
4. Manacled time in BOD + 1 day is 2014-08-28 00:00:00 -0700
5. Manacled time in BOD + 25 hours is 2014-08-28 00:00:00 -0700

Unconstraining

This time is now constrained forever to be beginning of the day. Unless you don't want it to be.

You can unmanacle any object, by calling #unconstrain. From the example above:

unproxy = bod.unconstrain
unproxy_plus_one_hour = unproxy + 60*60

puts "Unmanacled time + 1 hour is #{unproxy_plus_one_hour.to_s}"

This would print:

Unmanacled time + 1 hour is 2014-08-27 22:58:33 -0700

API Documentation

See RubyDoc

Contributors

See Contributing for details.

License

©2016 Ed Carrel. Released under the MIT License.

See License for details.