Module: Familia::Horreum::RelatedFieldsManagement::RelatedFieldsAccessors

Defined in:
lib/familia/horreum/related_fields.rb

Overview

RelatedFieldsManagement::RelatedFieldsAccessors

Instance Method Summary collapse

Instance Method Details

Sets up all DataType related methods This method generates the following for each registered DataType:

Instance methods: set(), list(), hashkey(), sorted_set(), etc. Query methods: set?(), list?(), hashkey?(), sorted_set?(), etc. Collection methods: sets(), lists(), hashkeys(), sorted_sets(), etc. Class methods: class_set(), class_list(), etc.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/familia/horreum/related_fields.rb', line 108

def setup_related_fields_definition_methods
  Familia::DataType.registered_types.each_pair do |kind, klass|
    Familia.trace :registered_types, kind, klass if Familia.debug?

    # Dynamically define instance-level relation methods
    #
    # Once defined, these methods can be used at the instance-level of a
    # Familia member to define *instance-level* relations to any of the
    # DataType types (e.g. set, list, hash, etc).
    #
    define_method :"#{kind}" do |*args|
      name, opts = *args

      # As log as we have at least one relation, we can set this flag.
      @has_related_fields = true

      attach_instance_related_field name, klass, opts
    end
    define_method :"#{kind}?" do |name|
      obj = related_fields[name.to_s.to_sym]
      !obj.nil? && klass == obj.klass
    end
    define_method :"#{kind}s" do
      names = related_fields.keys.select { |name| send(:"#{kind}?", name) }
      names.collect! { |name| related_fields[name] }
      names
    end

    # Dynamically define class-level relation methods
    #
    # Once defined, these methods can be used at the class-level of a
    # Familia member to define *class-level relations* to any of the
    # DataType types (e.g. class_set, class_list, class_hash, etc).
    #
    define_method :"class_#{kind}" do |*args|
      name, opts = *args
      attach_class_related_field name, klass, opts
    end
    define_method :"class_#{kind}?" do |name|
      obj = class_related_fields[name.to_s.to_sym]
      !obj.nil? && klass == obj.klass
    end
    define_method :"class_#{kind}s" do
      names = class_related_fields.keys.select { |name| send(:"class_#{kind}?", name) }
      # TODO: This returns instances of the DataType class which
      # also contain the options. This is different from the instance
      # DataTypes defined above which returns the Struct of name, klass, and opts.
      # names.collect! { |name| self.send name }
      # OR NOT:
      names.collect! { |name| class_related_fields[name] }
      names
    end
  end
end