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



47
48
49
50
# File 'lib/hugg_orm/persistence.rb', line 47

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

#countObject



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

def count
  new_query.count
end

#create(data) ⇒ Object



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

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

#delete_allObject



52
53
54
# File 'lib/hugg_orm/persistence.rb', line 52

def delete_all
  new_query.delete
end

#destroy(id) ⇒ Object



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

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

#destroy!(id) ⇒ Object



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

def destroy!(id)
  destroy(id) || raise(RecordNotFound.new(id))
end

#escape_like(str = '') ⇒ Object



121
122
123
124
# File 'lib/hugg_orm/persistence.rb', line 121

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
41
# File 'lib/hugg_orm/persistence.rb', line 38

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

#find!(id) ⇒ Object



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

def find!(id)
  find(id) || raise(RecordNotFound.new(id))
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



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

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

#new_collection(data_ary) ⇒ Object



109
110
111
# File 'lib/hugg_orm/persistence.rb', line 109

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

#new_queryObject



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

def new_query
  Query.new(table_name)
end

#order(*args) ⇒ Object



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

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 ||= "data->>'#{key_field}'"
end

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



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/hugg_orm/persistence.rb', line 56

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



117
118
119
# File 'lib/hugg_orm/persistence.rb', line 117

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



113
114
115
# File 'lib/hugg_orm/persistence.rb', line 113

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

#update(id, data) ⇒ Object



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

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

#update!(id, data) ⇒ Object



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

def update!(id, data)
  update(id, data) || raise(RecordNotFound.new(id))
end

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



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

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