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

#all_roles, #check_identifying_role_has_valid_cardinality, #has_one, #maybe, #one_to_one, #realise_role, #roles, #subtypes, #supertypes, #supertypes_transitive, #vocabulary

Instance Method Details

#assert_instance(constellation, args) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/activefacts/api/value.rb', line 89

def assert_instance(constellation, args)
  new_identifier = args == [:new]
  key = identifying_role_values(constellation, args)
  # args are now normalized to an array containing a single Hash element
  arg_hash = args[0]

  if new_identifier
    instance = key  # AutoCounter is its own key
  else
    instance_index = constellation.instances[self]
    unless instance = constellation.has_candidate(self, key) || instance_index[key]
      instance = new_instance(constellation, key)
      constellation.candidate(instance)
    end
  end

  # Assign any extra roles that may have been passed.
  # An exception here leaves the object as a candidate,
  # but without the offending role (re-)assigned.
  arg_hash.each do |k, v|
    instance.send(:"#{k}=", v)
  end

  instance
end

#identifying_role_values(constellation, args) ⇒ Object

:nodoc:



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/activefacts/api/value.rb', line 75

def identifying_role_values(constellation, args)   #:nodoc:
    # Normalise positional arguments into an arguments hash (this changes the passed parameter)
    arg_hash = args[-1].is_a?(Hash) ? args.pop : {}

  # If a single arg is already the correct class or a subclass,
    # use it directly, otherwise create one.
    # This appears to be the only way to handle e.g. Date correctly
  unless args.size == 1 and instance = args[0] and instance.is_a?(self)
      instance = new_instance(constellation, *args)
  end
    args.replace([arg_hash])
    instance.identifying_role_values(self)
end

#index_instance(constellation, instance) ⇒ Object

:nodoc:



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/activefacts/api/value.rb', line 115

def index_instance(constellation, instance) #:nodoc:
    # Index the instance in the constellation's InstanceIndex for this class:
  instances = constellation.instances[self]
  key = instance.identifying_role_values(self)
  instances[key] = instance

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

  instance
end

#inherited(other) ⇒ Object

:nodoc:



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

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:



39
40
41
42
43
44
45
46
# File 'lib/activefacts/api/value.rb', line 39

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 UnrecognisedOptionsException.new('ValueType', basename, key) unless respond_to?(key)
    send(key, value)
  end
end

#verbaliseObject

verbalise this ValueType



69
70
71
72
73
# File 'lib/activefacts/api/value.rb', line 69

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