Class: Fabrik::Blueprint
- Inherits:
-
Object
show all
- Defined in:
- lib/fabrik/database.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(klass) ⇒ Blueprint
Returns a new instance of Blueprint.
53
54
55
56
57
58
|
# File 'lib/fabrik/database.rb', line 53
def initialize(klass)
@klass = klass
@default_attributes = {}
@unique_keys = []
@functions = {}
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
72
73
74
|
# File 'lib/fabrik/database.rb', line 72
def method_missing(method_name, *args, &block)
@default_attributes[method_name.to_sym] = args.first.nil? ? block : ->(_) { args.first }
end
|
Instance Attribute Details
#callback ⇒ Object
Returns the value of attribute callback.
60
61
62
|
# File 'lib/fabrik/database.rb', line 60
def callback
@callback
end
|
#default_attributes ⇒ Object
Returns the value of attribute default_attributes.
60
61
62
|
# File 'lib/fabrik/database.rb', line 60
def default_attributes
@default_attributes
end
|
#functions ⇒ Object
Returns the value of attribute functions.
60
61
62
|
# File 'lib/fabrik/database.rb', line 60
def functions
@functions
end
|
#klass ⇒ Object
Returns the value of attribute klass.
60
61
62
|
# File 'lib/fabrik/database.rb', line 60
def klass
@klass
end
|
#unique_keys ⇒ Object
Returns the value of attribute unique_keys.
60
61
62
|
# File 'lib/fabrik/database.rb', line 60
def unique_keys
@unique_keys
end
|
Instance Method Details
#after_create(&block) ⇒ Object
66
|
# File 'lib/fabrik/database.rb', line 66
def after_create(&block) = @callback = block
|
#call_after_create(record, db) ⇒ Object
68
|
# File 'lib/fabrik/database.rb', line 68
def call_after_create(record, db) = @callback.nil? ? nil : db.instance_exec(record, &@callback)
|
80
81
82
|
# File 'lib/fabrik/database.rb', line 80
def configure(&configuration)
instance_eval(&configuration) unless configuration.nil?
end
|
#defaults(**default_attributes) ⇒ Object
62
|
# File 'lib/fabrik/database.rb', line 62
def defaults(**default_attributes) = @default_attributes = default_attributes
|
#function(name, &implementation) ⇒ Object
70
|
# File 'lib/fabrik/database.rb', line 70
def function(name, &implementation) = @functions[name.to_sym] = implementation
|
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
76
|
# File 'lib/fabrik/database.rb', line 76
def respond_to_missing?(method_name, include_private = false) = @default_attributes.key?(method_name.to_sym) || super
|
#to_s ⇒ Object
78
|
# File 'lib/fabrik/database.rb', line 78
def to_s = "#{klass} blueprint (#{object_id}) (#{default_attributes.keys.size} defaults, #{unique_keys.size} unique keys #{(!callback.nil?) ? "with callback" : ""})"
|
#unique(*keys) ⇒ Object
64
|
# File 'lib/fabrik/database.rb', line 64
def unique(*keys) = @unique_keys = keys
|