Module: ActiveFacts::API::Value::ClassMethods

Includes:
Instance::ClassMethods
Defined in:
lib/activefacts/api/value.rb

Overview

All ValueType classes include the methods defined here

Instance Method Summary collapse

Methods included from ObjectType

#detect_fact_type_collision, #has_one, #is_a?, #maybe, #one_to_one, #realise_role, #roles, #subtypes, #supertypes, #supertypes_transitive, #vocabulary

Instance Method Details

#assert_instance(constellation, args) ⇒ Object

:nodoc:



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/activefacts/api/value.rb', line 86

def assert_instance(constellation, args)  #:nodoc:
  # Build the key for this instance from the args
  # The key of an instance is the value or array of keys of the identifying values.
  # The key values aren't necessarily present in the constellation, even after this.
  key = identifying_role_values(*args)

  # Find and return an existing instance matching this key
  instances = constellation.instances[self]   # All instances of this class in this constellation
  instance = instances[key]
  return instance, key if instance      # A matching instance of this class

  #trace :assert, "Constructing new #{self} with #{args.inspect}" do
    instance = new(*args)
  #end

  instance.constellation = constellation
  return *index_instance(instance)
end

#identifying_role_values(*args) ⇒ Object

:nodoc:



78
79
80
81
82
83
84
# File 'lib/activefacts/api/value.rb', line 78

def identifying_role_values(*args)  #:nodoc:
  # If the single arg is the correct class or a subclass, use it directly
  if (args.size == 1 and (arg = args[0]).is_a?(self))   # No secondary supertypes allowed for value types
    return arg.identifying_role_values
  end
  new(*args).identifying_role_values
end

#index_instance(instance, key = nil, key_roles = nil) ⇒ Object

:nodoc:



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/activefacts/api/value.rb', line 105

def index_instance(instance, key = nil, key_roles = nil) #:nodoc:
  instances = instance.constellation.instances[self]
  key = instance.identifying_role_values
  instances[key] = instance

  # Index the instance for each supertype:
  supertypes.each do |supertype|
    supertype.index_instance(instance, key)
  end

  return instance, key
end

#inherited(other) ⇒ Object

:nodoc:



118
119
120
121
122
123
# File 'lib/activefacts/api/value.rb', line 118

def inherited(other)  #:nodoc:
  # Copy the type parameters here, etc?
  other.send :realise_supertypes, self
  vocabulary.__add_object_type(other)
  super
end

#value_type(*args, &block) ⇒ Object

:nodoc:



42
43
44
45
46
47
48
49
# File 'lib/activefacts/api/value.rb', line 42

def value_type *args, &block #:nodoc:
  # REVISIT: args could be a hash, with keys :length, :scale, :unit, :allow
  options = (args[-1].is_a?(Hash) ? args.pop : {})
  options.each do |key, value|
    raise "unknown value type option #{key}" unless respond_to?(key)
    send(key, value)
  end
end

#verbaliseObject

verbalise this ValueType



72
73
74
75
76
# File 'lib/activefacts/api/value.rb', line 72

def verbalise
  # REVISIT: Add length and scale here, if set
  # REVISIT: Set vocabulary name of superclass if not same as this
  "#{basename} = #{superclass.basename}();"
end