Module: JsonModel::ClassMethods
- Defined in:
- lib/json_model.rb
Instance Method Summary collapse
- #all ⇒ Object
- #attr_accessor(*vars) ⇒ Object
- #attributes ⇒ Object
- #field(name) ⇒ Object
- #filename ⇒ Object
- #filename=(str) ⇒ Object
- #find(value, key = 'id') ⇒ Object
- #find_by(hash) ⇒ Object
- #new_id ⇒ Object
Instance Method Details
#all ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/json_model.rb', line 29 def all entries = [] for entry in load_entries entries << create_object(entry) end entries end |
#attr_accessor(*vars) ⇒ Object
15 16 17 18 19 |
# File 'lib/json_model.rb', line 15 def attr_accessor(*vars) @attributes ||= [] @attributes.concat vars super(*vars) end |
#attributes ⇒ Object
21 22 23 |
# File 'lib/json_model.rb', line 21 def attributes @attributes end |
#field(name) ⇒ Object
25 26 27 |
# File 'lib/json_model.rb', line 25 def field(name) attr_accessor name end |
#filename ⇒ Object
57 58 59 60 |
# File 'lib/json_model.rb', line 57 def filename @filename ||= "#{ancestors.first}.json".downcase File.(@filename) end |
#filename=(str) ⇒ Object
53 54 55 |
# File 'lib/json_model.rb', line 53 def filename=(str) @filename = str end |
#find(value, key = 'id') ⇒ Object
38 39 40 |
# File 'lib/json_model.rb', line 38 def find(value, key='id') entry = all.find{|e| e.send(key) == value } end |
#find_by(hash) ⇒ Object
42 43 44 45 46 |
# File 'lib/json_model.rb', line 42 def find_by(hash) if hash.instance_of? Hash hash.each {| key, value | return find(value, key) } end end |
#new_id ⇒ Object
48 49 50 51 |
# File 'lib/json_model.rb', line 48 def new_id ids = all.map(&:id) ids.max.to_i + 1 end |