Module: Squirm::Model

Includes:
ActiveModel::Naming
Defined in:
lib/squirm/model.rb,
lib/squirm/model/sample.rb,
lib/squirm/model/version.rb,
lib/squirm/model/method_definer.rb

Defined Under Namespace

Modules: InstanceMethods Classes: MethodDefiner, NotFound, Sample

Constant Summary collapse

VERSION =
"0.0.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#__attributesObject (readonly)

Returns the value of attribute __attributes.



17
18
19
# File 'lib/squirm/model.rb', line 17

def __attributes
  @__attributes
end

#apiObject (readonly)

Returns the value of attribute api.



16
17
18
# File 'lib/squirm/model.rb', line 16

def api
  @api
end

Class Method Details

.extended(base) ⇒ Object



19
20
21
22
23
24
# File 'lib/squirm/model.rb', line 19

def self.extended(base)
  base.class_eval do
    include ActiveModel::Conversion
    include InstanceMethods
  end
end

.finalizeObject

Invoke #finalize on all Squirm models.



27
28
29
30
31
# File 'lib/squirm/model.rb', line 27

def self.finalize
  ObjectSpace.each_object(self) do |mod|
    mod.finalize
  end
end

Instance Method Details

#create(*args) ⇒ Object



78
79
80
# File 'lib/squirm/model.rb', line 78

def create(*args)
  get api.create[*args]
end

#delete(id) ⇒ Object



92
93
94
# File 'lib/squirm/model.rb', line 92

def delete(id)
  api.delete[id]
end

#finalizeObject

Invoke to load API. This allows classes to be set up before the API has been created, or the db connection established.



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/squirm/model.rb', line 35

def finalize
  return if defined? @api
  sql = Pathname(__FILE__).dirname.join("model/schema_functions.sql").read
  schema_name = model_name.to_s.downcase
  Squirm.exec(sql, [schema_name]) do |result|
    @api = OpenStruct.new Hash[result.map do |row| [
      row["name"].to_sym,
      Procedure.new(row["name"], :schema => schema_name).load
    ]
    end]
  end
end

#get(id) ⇒ Object



82
83
84
85
86
87
88
89
90
# File 'lib/squirm/model.rb', line 82

def get(id)
  api.get.call(id) do |result|
    raise NotFound if result.ntuples == 0
    hash = result.first
    instance = allocate
    instance.instance_variable_set :@__attributes, hash
    instance
  end
end

#sample(arg = nil, &block) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/squirm/model.rb', line 48

def sample(arg = nil, &block)
  if block_given?
    @sample_block = block
    yield MethodDefiner.new(self)
  elsif !arg
    @sample ||= begin
      @sample = Sample.new
      @sample_block.call(@sample)
      @sample
    end
  else
    sample = Sample.new
    @sample_block.call(sample)
    sample.each do |key, value|
      setter = :"#{key}="
      arg.send(setter, value) if arg.respond_to?(setter)
    end
    return arg
  end
end

#to_ddlObject



69
70
71
72
73
74
75
76
# File 'lib/squirm/model.rb', line 69

def to_ddl
  buffer = []
  table = Squirm::Migrator::Table.new(name.downcase)
  table.add_column(*sample.keys)
  buffer << table.template.render
  table.api.each {|template| buffer << template.render}
  buffer.map(&:strip).join("\n\n")
end