Module: GoldenFleece::Context::Getters

Included in:
GoldenFleece::Context
Defined in:
lib/golden_fleece/context/getters.rb

Instance Method Summary collapse

Instance Method Details

#define_getters(*attributes) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/golden_fleece/context/getters.rb', line 4

def define_getters(*attributes)
  # For each attribute...
  attributes.each do |attribute|
    # ...and each top-level schema of each attribute...
    schemas[attribute.to_sym].each do |schema_name, schema|
      # ...if there isn't already an instance method named after the schema...
      if !model_class.new.respond_to?(schema_name)
        # ...define a getter for that schema's value!
        model_class.class_eval do
          define_method schema_name do
            self.class.fleece_context.schemas[attribute.to_sym][schema_name].value.compute(self)
          end
        end
      end
    end
  end
end