Class: Osm::Model
- Inherits:
-
Object
- Object
- Osm::Model
- Includes:
- ActiveAttr::Model, ActiveModel::MassAssignmentSecurity
- Defined in:
- lib/osm/model.rb
Overview
This class is expected to be inherited from. It provides the caching and permission handling for model objects.
Direct Known Subclasses
Activity, ApiAccess, Badge, Badge::Data, Badges::DueBadges, Budget, Email::DeliveryReport, Email::DeliveryReport::Email, Email::DeliveryReport::Recipient, Event, Event::Attendance, Event::BadgeLink, Event::Column, FlexiRecord, FlexiRecord::Column, FlexiRecord::Data, GiftAid::Data, GiftAid::Donation, Grouping, Invoice, Invoice::Item, Meeting, Member, Osm::Member::Contact, Osm::Myscout::ParentLoginHistory, Osm::Myscout::Template, OnlinePayment::Schedule, OnlinePayment::Schedule::Payment, OnlinePayment::Schedule::PaymentStatus, OnlinePayment::Schedule::PaymentsForMember, Register::Attendance, Register::Field, Section, Sms::DeliveryReport, Term
Constant Summary collapse
- SORT_BY =
[:id]
- @@cache =
Class to use for caching
nil
- @@cache_prepend =
Prepended to the key
'OSMAPI'
- @@cache_ttl =
10 minutes
600
Class Method Summary collapse
-
.configure(options) ⇒ Object
Configure the options used by all models.
Instance Method Summary collapse
- #<(another) ⇒ Object
- #<=(another) ⇒ Object
-
#<=>(another) ⇒ Object
Compare functions.
- #>(another) ⇒ Object
- #>=(another) ⇒ Object
- #between?(min, max) ⇒ Boolean
-
#changed_attributes ⇒ Object
Get a list of attributes which have changed.
-
#reset_changed_attributes ⇒ Object
Reset the list of attributes which have changed.
-
#to_i ⇒ Object
Default to_i conversion is of id.
Class Method Details
.configure(options) ⇒ Object
Configure the options used by all models
27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/osm/model.rb', line 27 def self.configure() raise ArgumentError, ":ttl must be a FixNum greater than 0" if [:ttl] && !([:ttl].is_a?(Fixnum) && [:ttl] > 0) raise ArgumentError, ":prepend_to_key must be a String" if [:prepend_to_key] && ![:prepend_to_key].is_a?(String) if [:cache] [:exist?, :delete, :write, :read].each do |method| raise ArgumentError, ":cache must have a #{method} method" unless [:cache].methods.include?(method) end end @@cache = [:cache] @@cache_prepend = [:prepend_to_key] || 'OSMAPI' @@cache_ttl = [:ttl] || 600 nil end |
Instance Method Details
#<(another) ⇒ Object
55 56 57 |
# File 'lib/osm/model.rb', line 55 def <(another) send('<=>', another) < 0 end |
#<=(another) ⇒ Object
58 59 60 |
# File 'lib/osm/model.rb', line 58 def <=(another) send('<=>', another) <= 0 end |
#<=>(another) ⇒ Object
Compare functions
49 50 51 52 53 |
# File 'lib/osm/model.rb', line 49 def <=>(another) us_values = self.class::SORT_BY.map{ |i| self.try(i) } them_values = self.class::SORT_BY.map{ |i| another.try(i) } us_values <=> them_values end |
#>(another) ⇒ Object
61 62 63 |
# File 'lib/osm/model.rb', line 61 def >(another) send('<=>', another) > 0 end |
#>=(another) ⇒ Object
64 65 66 |
# File 'lib/osm/model.rb', line 64 def >=(another) send('<=>', another) >= 0 end |
#between?(min, max) ⇒ Boolean
67 68 69 |
# File 'lib/osm/model.rb', line 67 def between?(min, max) (send('<=>', min) > 0) && (send('<=>', max) < 0) end |
#changed_attributes ⇒ Object
Get a list of attributes which have changed
74 75 76 |
# File 'lib/osm/model.rb', line 74 def changed_attributes attributes.keys.select{ |k| attributes[k] != @original_attributes[k] } end |
#reset_changed_attributes ⇒ Object
Reset the list of attributes which have changed
79 80 81 82 83 84 85 |
# File 'lib/osm/model.rb', line 79 def reset_changed_attributes classes_to_clone = [Array, Hash] attributes_now = attributes.map do |k,v| [k, (classes_to_clone.include?(v.class) ? v.clone : v)] end # Deep(ish) clone @original_attributes = Hash[attributes_now] end |
#to_i ⇒ Object
Default to_i conversion is of id
44 45 46 |
# File 'lib/osm/model.rb', line 44 def to_i id.to_i end |