Module: Highland::CollectionMethods
- Included in:
- Highland
- Defined in:
- lib/highland/collection_methods.rb
Overview
CollectionMethods are mostly the API of Highland. Methods marked with “API:” are the ones, which are recommended to use day by day.
Instance Method Summary collapse
-
#all(*params) ⇒ Object
API: Returns all elements from the collection, you can also specify details of your request.
-
#build_object ⇒ Object
Objectize method helper.
-
#clear ⇒ Object
API: Clears the collection.
-
#count(*params) ⇒ Object
API: Define keys and it will return all elements from collection.
-
#create(*params) ⇒ Object
API: Creates new element in collection.
-
#distinct(column) ⇒ Object
API: Allows to get all values of a specified key inside collection.
-
#find(*params) ⇒ Object
API: Allows you to find elements in collection.
-
#first(*params) ⇒ Object
API: Returns the first element according to the query.
-
#init_collection(collection) ⇒ Object
Initializes collection (loads virtual hash and helper).
-
#objectize(hash) ⇒ Object
It’s one of core methods, which works as a factory for HighlandObjects.
-
#remove(*params) ⇒ Object
API: Removes the element of collection.
-
#size(*params) ⇒ Object
API: is a synonim for “count”.
-
#sort(*params) ⇒ Object
API: Define the key and it will return all elements from collection.
-
#update(*params) ⇒ Object
API: Updates the element of collection.
-
#where(*params) ⇒ Object
API: Returns array of elements according to the query.
Instance Method Details
#all(*params) ⇒ Object
API: Returns all elements from the collection, you can also specify details of your request. Look at examples below
- example: your code #1
-
Users.all(:name => ‘Fake’, :age => 20)
- example: your code #2
-
Users.all(:age => 20)
- example: your code #3
-
Users.all
- example: what happens
-
Will return all HighlandObjects of collection or some of them according to the query.
65 66 67 68 |
# File 'lib/highland/collection_methods.rb', line 65 def all(*params) return where(*params) if params[0].class == Hash return objectize(@vhash) end |
#build_object ⇒ Object
Objectize method helper.
196 197 198 |
# File 'lib/highland/collection_methods.rb', line 196 def build_object Object.const_set("HighlandObject", Class.new) unless defined?(HighlandObject) end |
#clear ⇒ Object
API: Clears the collection. Static and virtual hashes get empty.
- example: your code
-
Users.clear
- example: what happens
-
Now Users collection is empty.
169 170 171 172 173 |
# File 'lib/highland/collection_methods.rb', line 169 def clear clear_virtual clear_static reload_virtual end |
#count(*params) ⇒ Object
API: Define keys and it will return all elements from collection. If the order is not specified - it’s ascending. Look at examples of possible syntax.
- example: your code #1
-
Users.count(:name => ‘Fake’, :age => 20)
- example: your code #2
-
Users.count(:name => ‘Fake’)
- example: your code #3
-
Users.count(:age => 21)
- example: your code #4
-
Users.count
- example: what happens
-
Will return quantity of files according to the defined query.
127 128 129 130 |
# File 'lib/highland/collection_methods.rb', line 127 def count(*params) return find_db(*params).keys.length if params[0].class == Hash return @vhash.keys.length end |
#create(*params) ⇒ Object
API: Creates new element in collection.
- example: your code
-
Users.create(:age => 26, :name => ‘Chris’)
- example: what happens
-
Creates user in Users collection.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/highland/collection_methods.rb', line 20 def create(*params) id = rand(999999999999999999) rec = {} params[0].each_key do |key| rec[key.to_s] ||= {} rec[key.to_s]["type"] = params[0][key].class.to_s.downcase rec[key.to_s]["value"] = params[0][key] end element = {id => rec} insert_vhash(element) insert_vhelper(element) insert_shash(element) return element end |
#distinct(column) ⇒ Object
API: Allows to get all values of a specified key inside collection. It’s a good way to get all ids as well.
- example: your code #1
-
Users.distinct(:name)
- example: your code #2
-
Users.distinct(:id)
- example: what happens
-
Will return all values of a specified key.
144 145 146 147 |
# File 'lib/highland/collection_methods.rb', line 144 def distinct(column) return @vhelper[column.to_s].keys if column.to_s != "id" return @vhash.keys if column.to_s == "id" end |
#find(*params) ⇒ Object
API: Allows you to find elements in collection. It’s more powerful than “where” if you want to find several elements by id or by other keys.
- example: your code #1
- example: your code #2
-
Users.find(:age => [20,21,22])
- example: your code #3
-
Users.find(:age => [28])
- example: your code #4
-
Users.find(:age => 20)
- example: what happens
-
Will return HighlandObjects according to the query.
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/highland/collection_methods.rb', line 79 def find(*params) output = [] if params[0].class == Hash params[0].each_key do |key| [params[0][key]].flatten.each {|el| output = output + all(key => el)} end else params.each {|id| output += where(id)} end return output end |
#first(*params) ⇒ Object
API: Returns the first element according to the query.
- example: your code #1
-
Users.first(:name => ‘Bill’, :age => 80)
- example: your code #2
-
Users.first(:age => 80)
- example: what happens
-
Will return the first HighlandObject, which fits the query constraints.
53 54 55 |
# File 'lib/highland/collection_methods.rb', line 53 def first(*params) where(*params).first end |
#init_collection(collection) ⇒ Object
Initializes collection (loads virtual hash and helper).
10 11 12 13 14 |
# File 'lib/highland/collection_methods.rb', line 10 def init_collection(collection) init_file(collection) load_vhash(@file) load_vhelper end |
#objectize(hash) ⇒ Object
It’s one of core methods, which works as a factory for HighlandObjects.
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/highland/collection_methods.rb', line 177 def objectize(hash) output = [] build_object hash.each_key do |id| o = HighlandObject.new o.instance_variable_set(:@id, id) o.singleton_class.send(:define_method, :id) { @id } hash[id].each_key do |key| instance = "@#{key}" o.instance_variable_set(instance.to_sym, hash[id][key]["value"]) o.singleton_class.send(:define_method, key) { eval(instance) } end output << o end return output end |
#remove(*params) ⇒ Object
API: Removes the element of collection.
- example: your code
-
Users.remove(:age => 25, :name => “Bob”)
- example: what happens
-
Removes the element from table according to the query.
161 162 163 |
# File 'lib/highland/collection_methods.rb', line 161 def remove(*params) delete(*params) end |
#size(*params) ⇒ Object
API: is a synonim for “count”.
134 135 136 |
# File 'lib/highland/collection_methods.rb', line 134 def size(*params) count(*params) end |
#sort(*params) ⇒ Object
API: Define the key and it will return all elements from collection. If the order is not specified - it’s ascending. Look at examples of possible syntax.
- example: your code #1
-
Users.sort(:age)
- example: your code #2
-
Users.sort(:age => “asc”)
- example: your code #3
-
Users.sort(:age => “desc”)
- example: what happens
-
Will return HighlandObjects according to the defined order.
100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/highland/collection_methods.rb', line 100 def sort(*params) column = (params[0].class == Hash)?(params[0].keys.first):(params[0]) sorted = if params[0].class == Hash and params[0][column] == "desc" distinct(column).sort{|x,y| y <=> x} else distinct(column).sort end output = [] sorted.each {|s| @vhelper[column.to_s][s].each{|id| output += find(id)}} return output end |
#update(*params) ⇒ Object
API: Updates the element of collection.
- example: your code
-
Users.update(:id => some_id,:age => 40, :name => “Kate”)
- example: what happens
-
Will update relative values for element with id equal “some_id”.
153 154 155 |
# File 'lib/highland/collection_methods.rb', line 153 def update(*params) update_db(*params) end |
#where(*params) ⇒ Object
API: Returns array of elements according to the query.
- example: your code #1
-
Users.where(:name => ‘Helen’, :age => 20)
- example: your code #2
-
Users.where(:age => 20)
- example: what happens
-
Will return Array of HighlandObjects, which fit the query constraints.
41 42 43 44 45 |
# File 'lib/highland/collection_methods.rb', line 41 def where(*params) hash = find_db(*params) output = objectize(hash) return output end |