Class: Osm::Model

Inherits:
Object
  • Object
show all
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.

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

Instance Method Summary collapse

Class Method Details

.configure(options) ⇒ Object

Configure the options used by all models

Parameters:

  • options (Hash)

Options Hash (options):

  • :cache (Class, nil)

    An instance of a cache class, must provide the methods (exist?, delete, write, read), for details see Rails.cache. Set to nil to disable caching.

  • :ttl (Fixnum) — default: optional, default = 1800 (30 minutes)

    The default TTL value for the cache, note that some items are cached for twice this time and others are cached for half this time (in seconds)

  • :prepend_to_key (String) — default: optional, default = 'OSMAPI'

    Text to prepend to the key used to store data in the cache

Returns:

  • nil

Raises:

  • (ArgumentError)

27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/osm/model.rb', line 27

def self.configure(options)
  raise ArgumentError, ":ttl must be a FixNum greater than 0" if options[:ttl] && !(options[:ttl].is_a?(Fixnum) && options[:ttl] > 0)
  raise ArgumentError, ":prepend_to_key must be a String" if options[:prepend_to_key] && !options[:prepend_to_key].is_a?(String)
  if options[:cache]
    [:exist?, :delete, :write, :read].each do |method|
      raise ArgumentError, ":cache must have a #{method} method" unless options[:cache].methods.include?(method)
    end
  end

  @@cache = options[:cache]
  @@cache_prepend = options[:prepend_to_key] || 'OSMAPI'
  @@cache_ttl = options[: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

Returns:

  • (Boolean)

67
68
69
# File 'lib/osm/model.rb', line 67

def between?(min, max)
  (send('<=>', min) > 0) && (send('<=>', max) < 0)
end

#changed_attributesObject

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_attributesObject

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_iObject

Default to_i conversion is of id


44
45
46
# File 'lib/osm/model.rb', line 44

def to_i
  id.to_i
end