Module: HuggORM::Persistence::ClassMethods
- Defined in:
- lib/hugg_orm/persistence.rb
Instance Attribute Summary collapse
-
#_exclude_fields ⇒ Object
Returns the value of attribute _exclude_fields.
-
#_include_fields ⇒ Object
Returns the value of attribute _include_fields.
-
#_key_field ⇒ Object
Returns the value of attribute _key_field.
-
#_primary_key ⇒ Object
Returns the value of attribute _primary_key.
-
#_table_name ⇒ Object
Returns the value of attribute _table_name.
Instance Method Summary collapse
- #all ⇒ Object
- #count ⇒ Object
- #create(data) ⇒ Object
- #delete_all ⇒ Object
- #destroy(id) ⇒ Object
- #destroy!(id) ⇒ Object
- #escape_like(str = '') ⇒ Object
- #exclude_fields(*fields) ⇒ Object
- #find(id) ⇒ Object
- #find!(id) ⇒ Object
- #include_fields(*fields) ⇒ Object
- #key_field(key_field = nil) ⇒ Object
- #limit(limit, offset = nil) ⇒ Object
- #new_collection(data_ary) ⇒ Object
- #new_query ⇒ Object
- #order(*args) ⇒ Object
- #primary_key(primary_key = nil) ⇒ Object
- #select(query = nil, where: nil, order: nil, limit: nil, offset: nil) ⇒ Object
- #string_to_pgtimestamp(string) ⇒ Object
- #table_name(table_name = nil) ⇒ Object
- #to_pgtimestamp(field) ⇒ Object
- #update(id, data) ⇒ Object
- #update!(id, data) ⇒ Object
- #where(conditions, values = []) ⇒ Object
Instance Attribute Details
#_exclude_fields ⇒ Object
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_fields ⇒ Object
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_field ⇒ Object
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_key ⇒ Object
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_name ⇒ Object
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
#all ⇒ Object
47 48 49 50 |
# File 'lib/hugg_orm/persistence.rb', line 47 def all results = new_query.select new_collection(results) end |
#count ⇒ Object
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_all ⇒ Object
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_query ⇒ Object
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) ("'#{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 (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 |