Class: AutoCounter
- Inherits:
-
Object
- Object
- AutoCounter
- Defined in:
- lib/activefacts/api/numeric.rb
Instance Attribute Summary collapse
-
#place_holder_number ⇒ Object
readonly
Returns the value of attribute place_holder_number.
Class Method Summary collapse
-
.inherited(other) ⇒ Object
extend ActiveFacts::AutoCounterClass.
Instance Method Summary collapse
-
#<=>(o) ⇒ Object
:nodoc:.
-
#assign(i) ⇒ Object
Assign a definite value to an AutoCounter; this may only be done once.
- #clone ⇒ Object
-
#coerce(i) ⇒ Object
Coerce āiā to be of the same type as self.
-
#defined? ⇒ Boolean
Ask whether a definite value has been assigned.
-
#eql?(o) ⇒ Boolean
:nodoc:.
-
#equal?(value) ⇒ Boolean
if the value is unassigned, it equal?(:new).
-
#hash ⇒ Object
:nodoc:.
- #identifying_role_values(klass = nil) ⇒ Object
-
#initialize(i = :new) ⇒ AutoCounter
constructor
A new instance of AutoCounter.
- #inspect ⇒ Object
-
#to_i ⇒ Object
An AutoCounter may only be used in numeric expressions after a definite value has been assigned.
- #to_s ⇒ Object
Constructor Details
#initialize(i = :new) ⇒ AutoCounter
Returns a new instance of AutoCounter.
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/activefacts/api/numeric.rb', line 117 def initialize(i = :new) unless i == :new or i.is_a?(Integer) or i.is_a?(AutoCounter) raise ArgumentError.new("AutoCounter #{self.class} may not be #{i.inspect}") end @@place_holder ||= 0 case i when :new @value = nil @place_holder_number = (@@place_holder+=1) when AutoCounter if i.defined? @value = i.to_i else @place_holder_number = i.place_holder_number @value = nil end else @place_holder_number = @value = i.to_i; end end |
Instance Attribute Details
#place_holder_number ⇒ Object (readonly)
Returns the value of attribute place_holder_number.
116 117 118 |
# File 'lib/activefacts/api/numeric.rb', line 116 def place_holder_number @place_holder_number end |
Class Method Details
.inherited(other) ⇒ Object
extend ActiveFacts::AutoCounterClass
211 212 213 214 215 216 |
# File 'lib/activefacts/api/numeric.rb', line 211 def self.inherited(other) #:nodoc: other.class_eval do extend ActiveFacts::AutoCounterClass end super end |
Instance Method Details
#<=>(o) ⇒ Object
:nodoc:
194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/activefacts/api/numeric.rb', line 194 def <=>(o) #:nodoc: if self.defined? && !o == [] && o.defined? if (c = (self.class <=> o.class.name)) != 0 return c else return to_i <=> o.to_i end else to_s.<=>(o.to_s) end end |
#assign(i) ⇒ Object
Assign a definite value to an AutoCounter; this may only be done once
139 140 141 142 |
# File 'lib/activefacts/api/numeric.rb', line 139 def assign(i) raise ArgumentError, "Illegal attempt to assign integer value of a committed AutoCounter" if @value @value = i.to_i end |
#clone ⇒ Object
218 219 220 |
# File 'lib/activefacts/api/numeric.rb', line 218 def clone raise "Not allowed to clone AutoCounters" end |
#coerce(i) ⇒ Object
Coerce āiā to be of the same type as self
171 172 173 174 175 176 |
# File 'lib/activefacts/api/numeric.rb', line 171 def coerce(i) unless @value raise ArgumentError, "Illegal attempt to use the value of an uncommitted AutoCounter" end [ i.to_i, @value ] end |
#defined? ⇒ Boolean
Ask whether a definite value has been assigned
145 146 147 |
# File 'lib/activefacts/api/numeric.rb', line 145 def defined? !@value.nil? end |
#eql?(o) ⇒ Boolean
:nodoc:
190 191 192 |
# File 'lib/activefacts/api/numeric.rb', line 190 def eql?(o) #:nodoc: to_s.eql?(o.to_s) end |
#equal?(value) ⇒ Boolean
if the value is unassigned, it equal?(:new).
158 159 160 |
# File 'lib/activefacts/api/numeric.rb', line 158 def equal? value value == :new ? @value == nil : super end |
#hash ⇒ Object
:nodoc:
182 183 184 185 186 187 188 |
# File 'lib/activefacts/api/numeric.rb', line 182 def hash #:nodoc: if self.defined? @value.hash else @place_holder_number end end |
#identifying_role_values(klass = nil) ⇒ Object
206 207 208 |
# File 'lib/activefacts/api/numeric.rb', line 206 def klass = nil self end |
#inspect ⇒ Object
178 179 180 |
# File 'lib/activefacts/api/numeric.rb', line 178 def inspect "\#<AutoCounter "+to_s+">" end |
#to_i ⇒ Object
An AutoCounter may only be used in numeric expressions after a definite value has been assigned
163 164 165 166 167 168 |
# File 'lib/activefacts/api/numeric.rb', line 163 def to_i unless @value raise ArgumentError, "Illegal attempt to get integer value of an uncommitted AutoCounter" end @value end |
#to_s ⇒ Object
149 150 151 152 153 154 155 |
# File 'lib/activefacts/api/numeric.rb', line 149 def to_s if self.defined? @value.to_s else "new_#{@place_holder_number}" end end |