Class: CheckedInteger

Inherits:
DataMapper::Type
  • Object
show all
Defined in:
lib/dm-checked-types/checked_integer.rb

Class Method Summary collapse

Class Method Details

.[](range = {}) ⇒ Object



27
28
29
# File 'lib/dm-checked-types/checked_integer.rb', line 27

def self.[](range = {})
  new(range)
end

.bind(property) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/dm-checked-types/checked_integer.rb', line 31

def self.bind(property)
  if defined?(::DataMapper::Validate)
    model = property.model

    unless model.skip_auto_validation_for?(property)
      if property.type.ancestors.include?(CheckedInteger)
        range = self.range
        model.class_eval do
          lower_bound = range[:gte]
          lower_bound ||= range[:gt] + 1 if range[:gt]
          lower_bound ||= -n

          upper_bound = range[:lt] - 1 if range[:lt]
          upper_bound ||= range[:lte]
          upper_bound ||= n

          validates_within property.name, :set => (lower_bound..upper_bound)
        end
      end
    end
  end
end

.generated_typesObject



23
24
25
# File 'lib/dm-checked-types/checked_integer.rb', line 23

def self.generated_types
  @generated_types ||= {}
end

.inherited(target) ⇒ Object



4
5
6
# File 'lib/dm-checked-types/checked_integer.rb', line 4

def self.inherited(target)
  target.instance_variable_set('@primitive', self.primitive)
end

.new(range) ⇒ Object



16
17
18
19
20
21
# File 'lib/dm-checked-types/checked_integer.rb', line 16

def self.new(range)
  type = generated_types[range] || Class.new(CheckedInteger)
  type.range = range
  generated_types[range] = type
  type
end

.rangeObject



8
9
10
# File 'lib/dm-checked-types/checked_integer.rb', line 8

def self.range
  @range || {}
end

.range=(value) ⇒ Object



12
13
14
# File 'lib/dm-checked-types/checked_integer.rb', line 12

def self.range=(value)
  @range = value
end