Module: Cequel::Model::Properties::ClassMethods

Defined in:
lib/cequel/model/properties.rb

Instance Method Summary collapse

Instance Method Details

#column(name, type, options = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/cequel/model/properties.rb', line 34

def column(name, type, options = {})
  name = name.to_sym
  @_cequel.add_column(name, type, options.symbolize_keys)

  module_eval <<-RUBY, __FILE__, __LINE__+1
    def #{name}
      read_attribute(#{name.inspect})
    end

    def #{name}=(value)
      write_attribute(#{name.inspect}, value)
    end
  RUBY

  if type == :boolean
    module_eval <<-RUBY, __FILE__, __LINE__+1 if type == :boolean
      def #{name}?
        !!read_attribute(#{name.inspect})
      end
    RUBY
  end
end

#column_namesObject



65
66
67
# File 'lib/cequel/model/properties.rb', line 65

def column_names
  [@_cequel.key.name, *@_cequel.columns.keys]
end

#columnsObject



69
70
71
# File 'lib/cequel/model/properties.rb', line 69

def columns
  [@_cequel.key, *@_cequel.columns.values]
end

#key(key_alias, type) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/cequel/model/properties.rb', line 15

def key(key_alias, type)
  key_alias = key_alias.to_sym
  @_cequel.key = Column.new(key_alias, type)

  module_eval(<<-RUBY, __FILE__, __LINE__+1)
    def #{key_alias}
      @_cequel.key
    end

    def #{key_alias}=(key)
      @_cequel.key = key
    end

    def to_key
      [@_cequel.key]
    end
  RUBY
end

#key_aliasObject



57
58
59
# File 'lib/cequel/model/properties.rb', line 57

def key_alias
  key_column.name
end

#key_columnObject



61
62
63
# File 'lib/cequel/model/properties.rb', line 61

def key_column
  @_cequel.key
end

#type_columnObject



73
74
75
# File 'lib/cequel/model/properties.rb', line 73

def type_column
  @_cequel.type_column
end