Module: Panda::Record::BaseHelper

Included in:
Base
Defined in:
lib/panda/record/base_helper.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/panda/record/base_helper.rb', line 4

def self.included(base)
  class << base
    attr_reader :properties, :table

    private

    def column_names_with_constraints
      name_with_constraints = []
      properties.each do |column_name, constraints|
        query_string = []
        query_string << column_name.to_s
        parse_constraints(constraints, query_string)
        name_with_constraints << query_string.join(" ")
      end
      name_with_constraints
    end

    def parse_constraints(constraints, query_string)
      constraints.each do |attribute, value|
        query_string << send(attribute.to_s, value)
      end
    end

    def build_column_methods
      properties.keys.each(&method(:attr_accessor))
    end

    def get_model_object(row)
      return nil unless row
      model ||= new
      properties.keys.each_with_index do |key, index|
        model.send("#{key}=", row[index])
      end
      model
    end

    def type(value)
      value.to_s
    end

    def primary_key(is_primary)
      "PRIMARY KEY AUTOINCREMENT" if is_primary
    end

    def nullable(is_null)
      "NOT NULL" unless is_null
    end
  end
end