Module: UtopiaData::Resource::Models

Included in:
UtopiaData::Resource
Defined in:
lib/utopia_data/resource/models.rb

Instance Method Summary collapse

Instance Method Details

#attributes(&block) ⇒ Object



33
34
35
36
# File 'lib/utopia_data/resource/models.rb', line 33

def attributes(&block)
  @columns = []
  instance_eval &block if block_given?
end

#columnsObject



42
43
44
# File 'lib/utopia_data/resource/models.rb', line 42

def columns
  @columns ||= []
end

#create_resource_modelObject

Create the resource model



47
48
49
50
51
52
53
54
55
56
# File 'lib/utopia_data/resource/models.rb', line 47

def create_resource_model
  table_name = @config[:table_name] ? "self.table_name = '#{@config[:table_name]}'" : nil
  model_class = <<-MODEL
    class ::#{model_name} < ActiveRecord::Base
      #{table_name}
      attr_accessor #{columns.join(", ")}
    end
  MODEL
  eval model_class
end

#modelObject



29
30
31
# File 'lib/utopia_data/resource/models.rb', line 29

def model
  @model ||= resource_class
end

#model_nameObject



25
26
27
# File 'lib/utopia_data/resource/models.rb', line 25

def model_name
  resource_class_name
end

#set(attribute, options = {}) ⇒ Object



38
39
40
# File 'lib/utopia_data/resource/models.rb', line 38

def set(attribute, options = {})
  @columns << attribute
end