Class: DataMapper::Property::Numeric

Inherits:
Object show all
Defined in:
lib/dm-core/property/numeric.rb

Direct Known Subclasses

Decimal, Float, Integer

Constant Summary

DEFAULT_NUMERIC_MIN =
0
DEFAULT_NUMERIC_MAX =
2**31-1

Constants inherited from DataMapper::Property

INVALID_NAMES, OPTIONS, PRIMITIVES, VISIBILITY_OPTIONS

Instance Attribute Summary collapse

Attributes inherited from DataMapper::Property

#allow_blank, #allow_nil, #coercion_method, #default, #dump_as, #index, #instance_variable_name, #load_as, #model, #name, #options, #reader_visibility, #repository_name, #required, #unique_index, #writer_visibility

Instance Method Summary collapse

Methods inherited from Object

#dump, #load, #marshal, #to_child_key, #unmarshal

Methods inherited from DataMapper::Property

accept_options, accepted_options, #allow_blank?, #allow_nil?, #assert_valid_options, #assert_valid_value, #bind, demodulized_names, descendants, determine_class, #determine_visibility, #field, find_class, #get, #get!, inherited, #inspect, #key?, #lazy?, #lazy_load, #lazy_load_properties, #loaded?, nullable, options, primitive, #primitive, #primitive?, #properties, #required?, #serial?, #set, #set!, #typecast, #unique?, #valid?, #value_dumped?, #value_loaded?

Methods included from Deprecate

#deprecate

Methods included from Equalizer

#equalize

Methods included from Subject

#default?, #default_for

Methods included from Assertions

#assert_kind_of

Constructor Details

#initialize(model, name, options = {}) ⇒ Numeric (protected)

Returns a new instance of Numeric



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/dm-core/property/numeric.rb', line 13

def initialize(model, name, options = {})
  super

  if kind_of?(Decimal) || kind_of?(Float)
    @precision = @options.fetch(:precision)
    @scale     = @options.fetch(:scale)

    unless @precision > 0
      raise ArgumentError, "precision must be greater than 0, but was #{@precision.inspect}"
    end
  end

  if @options.key?(:min) || @options.key?(:max)
    @min = @options.fetch(:min, self.class::DEFAULT_NUMERIC_MIN)
    @max = @options.fetch(:max, self.class::DEFAULT_NUMERIC_MAX)

    if @max < DEFAULT_NUMERIC_MIN && !@options.key?(:min)
      raise ArgumentError, "min should be specified when the max is less than #{DEFAULT_NUMERIC_MIN}"
    elsif @max < @min
      raise ArgumentError, "max must be less than the min, but was #{@max} while the min was #{@min}"
    end
  end
end

Instance Attribute Details

#maxObject (readonly)

Returns the value of attribute max



6
7
8
# File 'lib/dm-core/property/numeric.rb', line 6

def max
  @max
end

#minObject (readonly)

Returns the value of attribute min



6
7
8
# File 'lib/dm-core/property/numeric.rb', line 6

def min
  @min
end

#precisionObject (readonly)

Returns the value of attribute precision



6
7
8
# File 'lib/dm-core/property/numeric.rb', line 6

def precision
  @precision
end

#scaleObject (readonly)

Returns the value of attribute scale



6
7
8
# File 'lib/dm-core/property/numeric.rb', line 6

def scale
  @scale
end