Class: Dupe::Model::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/superdupe/schema.rb,
lib/superdupe/attribute_template.rb

Overview

:nodoc:

Defined Under Namespace

Classes: AttributeTemplate

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSchema

Returns a new instance of Schema.



7
8
9
10
# File 'lib/superdupe/schema.rb', line 7

def initialize
  @attribute_templates = {}
  @after_create_callbacks = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/superdupe/schema.rb', line 12

def method_missing(method_name, *args, &block)
  attribute_name = method_name.to_s[-1..-1] == '=' ? method_name.to_s[0..-2].to_sym : method_name
  if block && block.arity < 1
    default_value = block
    transformer = nil
  else
    default_value = args[0]
    transformer = block
  end
  
  @attribute_templates[method_name.to_sym] = 
    AttributeTemplate.new method_name.to_sym, :default => default_value, :transformer => transformer
end

Instance Attribute Details

#after_create_callbacksObject (readonly)

Returns the value of attribute after_create_callbacks.



5
6
7
# File 'lib/superdupe/schema.rb', line 5

def after_create_callbacks
  @after_create_callbacks
end

#attribute_templatesObject (readonly)

Returns the value of attribute attribute_templates.



4
5
6
# File 'lib/superdupe/schema.rb', line 4

def attribute_templates
  @attribute_templates
end

Instance Method Details

#after_create(&block) ⇒ Object

Raises:

  • (ArgumentError)


26
27
28
29
30
31
32
33
# File 'lib/superdupe/schema.rb', line 26

def after_create(&block)
  raise(
    ArgumentError, 
    "You must pass a block that accepts a single parameter to 'after_create'"
  ) if !block || block.arity != 1
  
  @after_create_callbacks << block
end

#uniquify(*args) ⇒ Object

Raises:

  • (ArgumentError)


35
36
37
38
39
40
41
42
43
44
# File 'lib/superdupe/schema.rb', line 35

def uniquify(*args)
  raise ArgumentError, "You must pass at least one attribute to uniquify." if args.empty?
  raise ArgumentError, "You may only pass symbols to uniquify." unless all_members_of_class(args, Symbol)

  args.each do |attribute|
    after_create do |record|
      record[attribute] = "#{record.__model__.name} #{record.id} #{attribute}" unless record[attribute]
    end
  end
end