-
Notifications
You must be signed in to change notification settings - Fork 0
Provides ability to proxy ActiveRecord field through another object
License
angelo0000/proxy_field
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
ProxyField
==========
ProxyField provied the ability to proxy an ActiveRecord column or columns through
another object.
Example
=======
You store time duration in seconds in ActiveRecord column called "duration".
Lets say your model is called WaitingRoom. If you wanted to get the hours from an
instance of the model you would have
w = WaitingRoom.find(CONDITIONS)
w.duration / 60 / 60 # to get the hours
ProxyField would allow you to easily override the default return value of the call
to duration with another object. It will create an instance of your wrapper object
with the columns value as its constructor parameter.
class DurationField
def initialize(seconds)
@seconds = seconds
end
def to_hours
@seconds / 60 / 60
end
# more methods on the proxy here
# to_seconds, to_minutes, to_days
# to_weeks, to_years, etc...
end
class WaitingRoom < ActiveRecord::Base
proxy_field :duration, :as => DurationField
# Also valid
# proxy_field [:col1, :col2], :as => ProxyObj
end
Now I can say:
w = WaitingRoom.find(CONDITIONS)
w.to_hours
Copyright (c) 2009 StarterStep, released under the MIT license
About
Provides ability to proxy ActiveRecord field through another object
Resources
License
Stars
Watchers
Forks
Releases
No releases published
Packages 0
No packages published