Module: MiniModel::ClassMethods
- Included in:
- MiniModel
- Defined in:
- lib/minimodel.rb
Instance Method Summary collapse
- #all ⇒ Object
- #count ⇒ Object
- #find(primary_key, &block) ⇒ Object
- #indexed_by(primary_key, options = {}) ⇒ Object
- #insert(attributes) ⇒ Object
- #keys ⇒ Object
- #load_from(path) ⇒ Object
- #options(text_attribute) ⇒ Object
- #primary_key ⇒ Object
Instance Method Details
#all ⇒ Object
44 45 46 |
# File 'lib/minimodel.rb', line 44 def all index.values end |
#count ⇒ Object
48 49 50 |
# File 'lib/minimodel.rb', line 48 def count index.length end |
#find(primary_key, &block) ⇒ Object
90 91 92 |
# File 'lib/minimodel.rb', line 90 def find(primary_key, &block) index.fetch(primary_key, &block) end |
#indexed_by(primary_key, options = {}) ⇒ Object
56 57 58 59 60 |
# File 'lib/minimodel.rb', line 56 def indexed_by(primary_key, = {}) @primary_key = primary_key @auto_increment = [:auto_increment] ? 1 : nil end |
#insert(attributes) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/minimodel.rb', line 72 def insert(attributes) unless @auto_increment.nil? attributes[primary_key] = @auto_increment @auto_increment += 1 end object = new(attributes) pkey = object.send(primary_key) if index.key?(pkey) raise DuplicateKeyError end index[pkey] = object end |
#keys ⇒ Object
62 63 64 |
# File 'lib/minimodel.rb', line 62 def keys all.map(&primary_key) end |
#load_from(path) ⇒ Object
94 95 96 97 98 |
# File 'lib/minimodel.rb', line 94 def load_from(path) YAML.load_file(path).each do |key, attrs| insert symbolize_keys(attrs, primary_key => key) end end |
#options(text_attribute) ⇒ Object
66 67 68 69 70 |
# File 'lib/minimodel.rb', line 66 def (text_attribute) all.each_with_object({}) do |model, hash| hash[model.read_attribute(text_attribute)] = model.read_attribute(primary_key) end end |
#primary_key ⇒ Object
52 53 54 |
# File 'lib/minimodel.rb', line 52 def primary_key @primary_key end |