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.



112
113
114
115
116
# File 'lib/activefacts/api/numeric.rb', line 112

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)


138
139
140
141
# File 'lib/activefacts/api/numeric.rb', line 138

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

.inherited(other) ⇒ Object

:nodoc:



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

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)


119
120
121
122
# File 'lib/activefacts/api/numeric.rb', line 119

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

#defined?Boolean

Ask whether a definite value has been assigned

Returns:

  • (Boolean)


125
126
127
# File 'lib/activefacts/api/numeric.rb', line 125

def defined?
  !@value.nil?
end

#eql?(o) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


151
152
153
# File 'lib/activefacts/api/numeric.rb', line 151

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

#hashObject

:nodoc:



147
148
149
# File 'lib/activefacts/api/numeric.rb', line 147

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

#inspectObject



143
144
145
# File 'lib/activefacts/api/numeric.rb', line 143

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

#to_sObject



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

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