Module: HuggORM::Persistence::ClassMethods

Defined in:
lib/hugg_orm/persistence.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#_exclude_fieldsObject

Returns the value of attribute _exclude_fields.



11
12
13
# File 'lib/hugg_orm/persistence.rb', line 11

def _exclude_fields
  @_exclude_fields
end

#_include_fieldsObject

Returns the value of attribute _include_fields.



11
12
13
# File 'lib/hugg_orm/persistence.rb', line 11

def _include_fields
  @_include_fields
end

#_key_fieldObject

Returns the value of attribute _key_field.



11
12
13
# File 'lib/hugg_orm/persistence.rb', line 11

def _key_field
  @_key_field
end

#_primary_keyObject

Returns the value of attribute _primary_key.



11
12
13
# File 'lib/hugg_orm/persistence.rb', line 11

def _primary_key
  @_primary_key
end

#_table_nameObject

Returns the value of attribute _table_name.



11
12
13
# File 'lib/hugg_orm/persistence.rb', line 11

def _table_name
  @_table_name
end

Instance Method Details

#allObject



42
43
44
45
# File 'lib/hugg_orm/persistence.rb', line 42

def all
  results = new_query.select
  new_collection(results)
end

#countObject



72
73
74
# File 'lib/hugg_orm/persistence.rb', line 72

def count
  new_query.count
end

#create(data) ⇒ Object



59
60
61
# File 'lib/hugg_orm/persistence.rb', line 59

def create(data)
  new(data) if new_query.insert(data) > 0
end

#destroy(id) ⇒ Object



68
69
70
# File 'lib/hugg_orm/persistence.rb', line 68

def destroy(id)
  where("#{primary_key} = ?", [id]).delete > 0
end

#escape_like(str = '') ⇒ Object



104
105
106
107
# File 'lib/hugg_orm/persistence.rb', line 104

def escape_like(str = '')
  pat = Regexp.union("\\", "%", "_")
  str.gsub(pat) { |mat| ["\\", mat].join }
end

#exclude_fields(*fields) ⇒ Object



28
29
30
31
# File 'lib/hugg_orm/persistence.rb', line 28

def exclude_fields(*fields)
  self._exclude_fields = fields unless fields.empty?
  self._exclude_fields ||= []
end

#find(id) ⇒ Object



38
39
40
# File 'lib/hugg_orm/persistence.rb', line 38

def find(id)
  where("#{primary_key} = ?", [id]).select.first
end

#include_fields(*fields) ⇒ Object



33
34
35
36
# File 'lib/hugg_orm/persistence.rb', line 33

def include_fields(*fields)
  self._include_fields = fields unless fields.empty?
  self._include_fields ||= []
end

#key_field(key_field = nil) ⇒ Object



23
24
25
26
# File 'lib/hugg_orm/persistence.rb', line 23

def key_field(key_field = nil)
  self._key_field = key_field.to_sym unless key_field.nil?
  self._key_field ||= :id
end

#limit(limit, offset = nil) ⇒ Object



80
81
82
# File 'lib/hugg_orm/persistence.rb', line 80

def limit(limit, offset = nil)
  new_query.limit(limit, offset)
end

#new_collection(data_ary) ⇒ Object



92
93
94
# File 'lib/hugg_orm/persistence.rb', line 92

def new_collection(data_ary)
  data_ary.map { |data| new(data) }
end

#new_queryObject



88
89
90
# File 'lib/hugg_orm/persistence.rb', line 88

def new_query
  Query.new(table_name)
end

#order(*args) ⇒ Object



84
85
86
# File 'lib/hugg_orm/persistence.rb', line 84

def order(*args)
  new_query.order(*args)
end

#primary_key(primary_key = nil) ⇒ Object



18
19
20
21
# File 'lib/hugg_orm/persistence.rb', line 18

def primary_key(primary_key = nil)
  self._primary_key = primary_key unless primary_key.nil?
  self._primary_key
end

#select(query = nil, where: nil, order: nil, limit: nil, offset: nil) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/hugg_orm/persistence.rb', line 47

def select(query = nil, where: nil, order: nil, limit: nil, offset: nil)
  return new_collection(query.select) if query

  query = new_query
  query.where(where[:conditions], where[:values]) if where
  query.order(order) if order
  query.limit(limit) if limit
  query.offset(offset) if offset

  new_collection(query.select)
end

#string_to_pgtimestamp(string) ⇒ Object



100
101
102
# File 'lib/hugg_orm/persistence.rb', line 100

def string_to_pgtimestamp(string)
  to_pgtimestamp("'#{string}'")
end

#table_name(table_name = nil) ⇒ Object



13
14
15
16
# File 'lib/hugg_orm/persistence.rb', line 13

def table_name(table_name = nil)
  self._table_name = table_name unless table_name.nil?
  self._table_name
end

#to_pgtimestamp(field) ⇒ Object



96
97
98
# File 'lib/hugg_orm/persistence.rb', line 96

def to_pgtimestamp(field)
  "to_timestamp(#{field}, 'YYYY-MM-DD HH24:MI:SS')"
end

#update(id, data) ⇒ Object



63
64
65
66
# File 'lib/hugg_orm/persistence.rb', line 63

def update(id, data)
  results = where("#{primary_key} = ?", [id]).update(data, true)
  new(results.first) unless results.empty?
end

#where(conditions, values = []) ⇒ Object



76
77
78
# File 'lib/hugg_orm/persistence.rb', line 76

def where(conditions, values = [])
  new_query.where(conditions, values)
end