Class: Unidata::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/unidata/model.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Model

Returns a new instance of Model.



117
118
119
120
121
122
123
124
# File 'lib/unidata/model.rb', line 117

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

.filenameObject

Returns the value of attribute filename.



4
5
6
# File 'lib/unidata/model.rb', line 4

def filename
  @filename
end

Class Method Details

.connectionObject



10
11
12
# File 'lib/unidata/model.rb', line 10

def connection
  Unidata.connection
end

.delete(id) ⇒ Object



88
89
90
# File 'lib/unidata/model.rb', line 88

def delete(id)
  connection.delete_record(filename, id)
end

.exists?(id) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/unidata/model.rb', line 69

def exists?(id)
  connection.exists?(filename, id)
end

.field(index, name, type = String, options = {}) ⇒ Object



26
27
28
29
30
# File 'lib/unidata/model.rb', line 26

def field(index, name, type=String, options={})
  @fields[name.to_sym] = Field.new(index, name, type, options)
  define_attribute_accessor(name)
  define_attribute_finder(name)
end

.field?(name) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/unidata/model.rb', line 22

def field?(name)
  fields.keys.include?(name.to_sym)
end

.fieldsObject



14
15
16
17
18
19
20
# File 'lib/unidata/model.rb', line 14

def fields
  if self < Unidata::Model
    superclass.fields.merge @fields
  else
    @fields
  end
end

.find(id) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/unidata/model.rb', line 73

def find(id)
  if record = connection.read(filename, id)
    instance = from_unidata(record)
    instance.id = id
    instance
  end
end

.find_by(name, value) ⇒ Object



81
82
83
84
85
86
# File 'lib/unidata/model.rb', line 81

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



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/unidata/model.rb', line 56

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

.inherited(subclass) ⇒ Object



6
7
8
# File 'lib/unidata/model.rb', line 6

def inherited(subclass)
  subclass.instance_variable_set(:@fields, {})
end

.to_unidata(instance) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/unidata/model.rb', line 32

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

#destroyObject



131
132
133
# File 'lib/unidata/model.rb', line 131

def destroy
  self.class.connection.delete_record(self.class.filename, id)
end

#saveObject



126
127
128
129
# File 'lib/unidata/model.rb', line 126

def save
  record = self.class.to_unidata(self)
  self.class.connection.write(self.class.filename, id, record)
end