Module: Tanuki::ModelBehavior::ClassMethods

Defined in:
lib/tanuki/behavior/model_behavior.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(mod) ⇒ Object

Prepares the extended module.



103
104
105
106
# File 'lib/tanuki/behavior/model_behavior.rb', line 103

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.



75
76
77
# File 'lib/tanuki/behavior/model_behavior.rb', line 75

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

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

Creates new model, or returns existing one.



80
81
82
83
84
85
86
87
88
# File 'lib/tanuki/behavior/model_behavior.rb', line 80

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.



91
92
93
94
# File 'lib/tanuki/behavior/model_behavior.rb', line 91

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.



97
98
99
100
# File 'lib/tanuki/behavior/model_behavior.rb', line 97

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