class MagicModels::Model

Attributes

name[R]
schema[R]

Public Class Methods

new(schema, name) click to toggle source
# File lib/magic_models/model.rb, line 11
def initialize(schema, name)
  @schema = schema
  @name   = name
end

Public Instance Methods

associations()

Currently, we only support belongs_to associations. I don't think there is a way to differentiate a has_one from a has_many.

Alias for: belongs_to
belongs_to() click to toggle source
# File lib/magic_models/model.rb, line 38
def belongs_to
  schema.foreign_keys(name).map do |fk|
    Associations::BelongsTo.new(fk)
  end
end
Also aliased as: associations
define() click to toggle source
# File lib/magic_models/model.rb, line 29
def define
  schema.evaluate(render)
  constantize
end
filename() click to toggle source
# File lib/magic_models/model.rb, line 16
def filename
  File.join(schema.destination, "#{model_name.underscore}.rb")
end
model_name() click to toggle source
# File lib/magic_models/model.rb, line 48
def model_name
  name.singularize.camelize
end
primary_key() click to toggle source
# File lib/magic_models/model.rb, line 34
def primary_key
  @primary_key ||= schema.primary_key(name)
end
render() click to toggle source
# File lib/magic_models/model.rb, line 20
def render
  ERB.new(File.read(template)).result(binding)
end
write() click to toggle source
# File lib/magic_models/model.rb, line 24
def write
  File.open(filename, 'w') { |f| f.write render }
  filename
end

Private Instance Methods

template() click to toggle source
# File lib/magic_models/model.rb, line 54
def template
  File.expand_path('../../templates/model.erb', __FILE__)
end