Class: Migrant::SchemaProxy

Inherits:
BasicObject
Defined in:
lib/migrant/schema.rb

Overview

Why does this class exist? Excellent question. Basically, Kernel gives a whole bunch of global methods, like system, puts, etc. This is bad because our DSL relies on being able to translate any arbitrary method into a method_missing call. So, we call method missing in this happy bubble where these magic methods don’t exist. The reason we don’t inherit Schema itself in this way, is that we’d lose direct access to all other classes derived from Object. Normally this would just mean scope resolution operators all over the class, but the real killer is that the DSL would need to reference classes in that manner as well, which would reduce sexy factor by at least 100.

Instance Method Summary collapse

Constructor Details

#initialize(binding) ⇒ SchemaProxy

Returns a new instance of SchemaProxy.



123
124
125
# File 'lib/migrant/schema.rb', line 123

def initialize(binding)
  @binding = binding
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args, &block) ⇒ Object



136
137
138
139
140
141
# File 'lib/migrant/schema.rb', line 136

def method_missing(*args, &block)
  field = args.slice!(0)
  data_type = args.slice!(0) unless args.first.nil? || args.first.respond_to?(:keys)

  @binding.add_field(field.to_sym, data_type, args.extract_options!)
end

Instance Method Details

#property(*arguments) ⇒ Object

Provides a method for dynamically creating fields (i.e. not part of instance_eval)



132
133
134
# File 'lib/migrant/schema.rb', line 132

def property(*arguments)
  method_missing(*arguments)
end

#translate_fancy_dsl(&block) ⇒ Object



127
128
129
# File 'lib/migrant/schema.rb', line 127

def translate_fancy_dsl(&block)
  self.instance_eval(&block)
end