Module: Tanuki::ModelBehavior::ClassMethods

Defined in:
lib/tanuki/model_behavior.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(mod) ⇒ Object

Prepares the extended module.



107
108
109
110
# File 'lib/tanuki/model_behavior.rb', line 107

def self.extended(mod)
  mod.instance_variable_set(:@_attributes, {})
  mod.instance_variable_set(:@_relations, {})
end

Instance Method Details

#[](attribute) ⇒ Object

Returns meta-information for a given attribute.



77
78
79
# File 'lib/tanuki/model_behavior.rb', line 77

def [](attribute)
  @_attributes[attribute]
end

#get(data, ctx, lazy = false) ⇒ Object

Creates new model, or returns existing one.



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/tanuki/model_behavior.rb', line 82

def get(data, ctx, lazy=false) # IDENTITY TRACKING AND LAZY LOADING
  entity_key = extract_key(data)
  key = [self, entity_key]
  # extract_key is generated ad-hoc by model compiler!
  if cached = ctx.entity_cache[key]
    cached
  else
    ctx.entity_cache[key] = get(*entity_key)
    # get is generated ad-hoc by model compiler
  end
end

#has_attribute(attribute, attr_def) ⇒ Object

Assigns attribute with definition attr_def to model.



95
96
97
98
# File 'lib/tanuki/model_behavior.rb', line 95

def has_attribute(attribute, attr_def)
  @_attributes ||= superclass.instance_variable_get(:@_attributes).dup
  @_attributes[attribute] = attr_def
end

#has_relation(name, relation_def) ⇒ Object

Adds a relation name with definition relation_def to model.



101
102
103
104
# File 'lib/tanuki/model_behavior.rb', line 101

def has_relation(name, relation_def)
  @_relations ||= superclass.instance_variable_get(:@_relations).dup
  @_relations[name] = relation_def
end