Class: Fabrik::Proxy
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- Fabrik::Proxy
show all
- Defined in:
- lib/fabrik/database.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(db, blueprint) ⇒ Proxy
Returns a new instance of Proxy.
86
87
88
89
90
91
|
# File 'lib/fabrik/database.rb', line 86
def initialize(db, blueprint)
@db = db
@blueprint = blueprint
@records = {}
super(klass)
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
107
|
# File 'lib/fabrik/database.rb', line 107
def method_missing(method_name, *args, &block) = self[method_name] || call_function(method_name, *args) || super
|
Instance Attribute Details
#blueprint ⇒ Object
Returns the value of attribute blueprint.
92
93
94
|
# File 'lib/fabrik/database.rb', line 92
def blueprint
@blueprint
end
|
Instance Method Details
#[](label) ⇒ Object
101
|
# File 'lib/fabrik/database.rb', line 101
def [](label) = @records[label.to_sym]
|
#[]=(label, record) ⇒ Object
103
104
105
|
# File 'lib/fabrik/database.rb', line 103
def []=(label, record)
@records[label.to_sym] = record
end
|
#callback ⇒ Object
123
|
# File 'lib/fabrik/database.rb', line 123
def callback = @blueprint.callback
|
113
114
115
|
# File 'lib/fabrik/database.rb', line 113
def configure(&configuration)
@blueprint.configure(&configuration) unless configuration.nil?
end
|
#create(label = nil, after_create: true, **attributes) ⇒ Object
Also known as:
create!
94
95
96
97
98
|
# File 'lib/fabrik/database.rb', line 94
def create(label = nil, after_create: true, **attributes)
(@blueprint.unique_keys.any? ? find_or_create_record(attributes, callback: after_create) : create_record(attributes, callback: after_create)).tap do |record|
self[label] = record if label
end
end
|
#default_attributes ⇒ Object
121
|
# File 'lib/fabrik/database.rb', line 121
def default_attributes = @blueprint.default_attributes
|
#functions ⇒ Object
125
|
# File 'lib/fabrik/database.rb', line 125
def functions = @blueprint.functions
|
#klass ⇒ Object
119
|
# File 'lib/fabrik/database.rb', line 119
def klass = @blueprint.klass
|
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
109
|
# File 'lib/fabrik/database.rb', line 109
def respond_to_missing?(method_name, include_private = false) = !self[method_name].nil? || !functions[method_name].nil? || super
|
#to_s ⇒ Object
111
|
# File 'lib/fabrik/database.rb', line 111
def to_s = "Proxy #{object_id} for #{@blueprint} (#{@records.keys.size} records)"
|
#unique_keys ⇒ Object
117
|
# File 'lib/fabrik/database.rb', line 117
def unique_keys = @blueprint.unique_keys
|