Class: Unidata::Model
- Inherits:
-
Object
- Object
- Unidata::Model
- Defined in:
- lib/unidata/model.rb
Class Attribute Summary collapse
-
.filename ⇒ Object
Returns the value of attribute filename.
Class Method Summary collapse
- .connection ⇒ Object
- .delete(id) ⇒ Object
- .exists?(id) ⇒ Boolean
- .field(index, name, type = String, options = {}) ⇒ Object
- .field?(name) ⇒ Boolean
- .fields ⇒ Object
- .find(id) ⇒ Object
- .find_by(name, value) ⇒ Object
- .from_unidata(record) ⇒ Object
- .to_unidata(instance) ⇒ Object
Instance Method Summary collapse
- #destroy ⇒ Object
-
#initialize(attributes = {}) ⇒ Model
constructor
A new instance of Model.
- #save ⇒ Object
Constructor Details
#initialize(attributes = {}) ⇒ Model
Returns a new instance of Model.
111 112 113 114 115 116 117 118 |
# File 'lib/unidata/model.rb', line 111 def initialize(attributes={}) initialize_attributes attributes.each do |key, value| next unless self.class.field?(key.to_sym) write_attribute(key, value) end end |
Class Attribute Details
.filename ⇒ Object
Returns the value of attribute filename.
4 5 6 |
# File 'lib/unidata/model.rb', line 4 def filename @filename end |
Class Method Details
.connection ⇒ Object
6 7 8 |
# File 'lib/unidata/model.rb', line 6 def connection Unidata.connection end |
.delete(id) ⇒ Object
85 86 87 |
# File 'lib/unidata/model.rb', line 85 def delete(id) connection.delete_record(filename, id) end |
.exists?(id) ⇒ Boolean
66 67 68 |
# File 'lib/unidata/model.rb', line 66 def exists?(id) connection.exists?(filename, id) end |
.field(index, name, type = String, options = {}) ⇒ Object
23 24 25 26 27 |
# File 'lib/unidata/model.rb', line 23 def field(index, name, type=String, ={}) fields[name.to_sym] = Field.new(index, name, type, ) define_attribute_accessor(name) define_attribute_finder(name) end |
.field?(name) ⇒ Boolean
19 20 21 |
# File 'lib/unidata/model.rb', line 19 def field?(name) fields.keys.include?(name.to_sym) end |
.fields ⇒ Object
10 11 12 13 14 15 16 17 |
# File 'lib/unidata/model.rb', line 10 def fields unless @fields @fields = {} field(0, :id) end @fields end |
.find(id) ⇒ Object
70 71 72 73 74 75 76 |
# File 'lib/unidata/model.rb', line 70 def find(id) if record = connection.read(filename, id) instance = from_unidata(record) instance.id = id instance end end |
.find_by(name, value) ⇒ Object
78 79 80 81 82 83 |
# File 'lib/unidata/model.rb', line 78 def find_by(name, value) field_number = "F#{fields[name.to_sym].index.first}" connection.select(filename, "#{field_number} EQ \"#{value}\"").map do |id| find(id) end end |
.from_unidata(record) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/unidata/model.rb', line 53 def from_unidata(record) instance = new fields.each do |key, field| next if key == :id value = record.extract(*field.index).to_s instance.send("#{key}=", field.from_unidata(value)) end instance end |
.to_unidata(instance) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/unidata/model.rb', line 29 def to_unidata(instance) record = Unidata::UniDynArray.new fields.each do |key, field| next if key == :id value = instance.send(key) next if value.nil? a_field, a_value, a_subvalue = field.index a_string = field.to_unidata(value) if a_subvalue record.replace a_field, a_value, a_subvalue, a_string elsif a_value record.replace a_field, a_value, a_string else record.replace a_field, a_string end end record end |
Instance Method Details
#destroy ⇒ Object
125 126 127 |
# File 'lib/unidata/model.rb', line 125 def destroy self.class.connection.delete_record(self.class.filename, id) end |
#save ⇒ Object
120 121 122 123 |
# File 'lib/unidata/model.rb', line 120 def save record = self.class.to_unidata(self) self.class.connection.write(self.class.filename, id, record) end |