Class: AutoCounter

Inherits:
Object
  • Object
show all
Defined in:
lib/activefacts/api/numeric.rb

Overview

The AutoCounter class is an integer, but only after the value has been established in the database. Construct it with the value :new to get an uncommitted value. You can use this new instance as a value of any role of this type, including to identify an entity instance. The assigned value will be filled out everywhere it needs to be, upon save.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(i = :new) ⇒ AutoCounter

Returns a new instance of AutoCounter.



110
111
112
113
114
# File 'lib/activefacts/api/numeric.rb', line 110

def initialize(i = :new)
  raise "AutoCounter #{self.class} may not be #{i.inspect}" unless i == :new or Integer === i
  # puts "new AutoCounter #{self.class} from\n\t#{caller.select{|s| s !~ %r{rspec}}*"\n\t"}"
  @value = i == :new ? nil : i
end

Class Method Details

.coerce(i) ⇒ Object

An AutoCounter may only be used in numeric expressions after a definite value has been assigned

Raises:

  • (ArgumentError)


136
137
138
139
# File 'lib/activefacts/api/numeric.rb', line 136

def self.coerce(i)
  raise ArgumentError unless @value
  [ i.to_i, @value ]
end

.inherited(other) ⇒ Object

:nodoc:



153
154
155
156
157
158
159
# File 'lib/activefacts/api/numeric.rb', line 153

def self.inherited(other)             #:nodoc:
  def other.identifying_role_values(*args)
    return nil if args == [:new]  # A new object has no identifying_role_values
    return new(*args)
  end
  super
end

Instance Method Details

#assign(i) ⇒ Object

Assign a definite value to an AutoCounter; this may only be done once

Raises:

  • (ArgumentError)


117
118
119
120
# File 'lib/activefacts/api/numeric.rb', line 117

def assign(i)
  raise ArgumentError if @value
  @value = i.to_i
end

#defined?Boolean

Ask whether a definite value has been assigned

Returns:

  • (Boolean)


123
124
125
# File 'lib/activefacts/api/numeric.rb', line 123

def defined?
  !@value.nil?
end

#eql?(o) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


149
150
151
# File 'lib/activefacts/api/numeric.rb', line 149

def eql?(o)                           #:nodoc:
  self.class == o.class and to_s.eql?(o.to_s)
end

#hashObject

:nodoc:



145
146
147
# File 'lib/activefacts/api/numeric.rb', line 145

def hash                              #:nodoc:
  to_s.hash ^ self.class.hash
end

#inspectObject



141
142
143
# File 'lib/activefacts/api/numeric.rb', line 141

def inspect
  "\#<AutoCounter "+to_s+">"
end

#to_sObject



127
128
129
130
131
132
133
# File 'lib/activefacts/api/numeric.rb', line 127

def to_s
  if self.defined?
    @value.to_s 
  else
    "new_#{object_id}"
  end
end