Class: Reorm::Model

Inherits:
Object
  • Object
show all
Includes:
EventHandler, EventSource, TableBacked, Validations
Defined in:
lib/reorm/model.rb

Constant Summary collapse

@@class_tables =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Validations

#validate_exclusion_of, #validate_inclusion_of, #validate_length_of, #validate_presence_of

Methods included from TableBacked

included

Methods included from EventHandler

included

Methods included from EventSource

#fire_events, included

Constructor Details

#initialize(properties = {}) ⇒ Model

Returns a new instance of Model.



14
15
16
17
18
# File 'lib/reorm/model.rb', line 14

def initialize(properties={})
  @properties = {}
  @errors     = PropertyErrors.new
  properties.each {|key, value| set_property(key, value)}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments, &block) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
# File 'lib/reorm/model.rb', line 137

def method_missing(method_name, *arguments, &block)
  if method_name.to_s[-1,1] != "="
    if @properties.include?(property_name(method_name))
      @properties[method_name]
    else
      super
    end
  else
    @properties[property_name(method_name)] = arguments.first
  end
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



19
20
21
# File 'lib/reorm/model.rb', line 19

def errors
  @errors
end

Class Method Details

.allObject



173
174
175
# File 'lib/reorm/model.rb', line 173

def self.all
  Cursor.new(self, r.table(table_name))
end

.create(properties = {}) ⇒ Object



167
168
169
170
171
# File 'lib/reorm/model.rb', line 167

def self.create(properties={})
  object = self.new(properties)
  object.save
  object
end

.filter(predicate = nil, &block) ⇒ Object



177
178
179
180
181
182
183
# File 'lib/reorm/model.rb', line 177

def self.filter(predicate=nil, &block)
  if predicate.nil?
    Cursor.new(self, r.table(table_name).filter(&block))
  else
    Cursor.new(self, r.table(table_name).filter(predicate))
  end
end

.get(id) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/reorm/model.rb', line 153

def self.get(id)
  model = nil
  Reorm.connection do |connection|
    if table_exists?(table_name, connection)
      properties = r.table(table_name).get(id).run(connection)
      if !properties.nil?
        model = self.new
        model.assign_properties(properties)
      end
    end
  end
  model
end

Instance Method Details

#[](property_name) ⇒ Object



97
98
99
# File 'lib/reorm/model.rb', line 97

def [](property_name)
  @properties[property_name.to_sym]
end

#[]=(property_name, value) ⇒ Object



101
102
103
104
# File 'lib/reorm/model.rb', line 101

def []=(property_name, value)
  @properties[property_name.to_sym] = value
  value
end

#assign_properties(properties = {}) ⇒ Object



126
127
128
129
130
131
# File 'lib/reorm/model.rb', line 126

def assign_properties(properties={})
  properties.each do |key, value|
    @properties[key.to_sym] = value
  end
  self
end

#deleteObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/reorm/model.rb', line 81

def delete
  key = get_property(primary_key)
  if key
    Reorm.connection do |connection|
      fire_events(events: [:before_delete])
      result = r.table(table_name).get(key).delete.run(connection)
      if result["deleted"] != 1
        raise Error, "Deletion of record for a #{self.class.name} class instance with a primary key of #{key} failed."
      end
      fire_events(events: [:after_delete])
      set_property(primary_key, nil)
    end
  end
  self
end

#get_property(property) ⇒ Object



110
111
112
# File 'lib/reorm/model.rb', line 110

def get_property(property)
  has_property?(property) ? self.__send__(property_name(property)) : nil
end

#has_property?(property) ⇒ Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/reorm/model.rb', line 106

def has_property?(property)
  @properties.include?(property_name(property))
end

#include?(field) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/reorm/model.rb', line 33

def include?(field)
  @properties.include?(property_name(field))
end

#respond_to?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/reorm/model.rb', line 133

def respond_to?(method_name, include_private=false)
  method_name.to_s[-1, 1] == "=" || @properties.include?(property_name(method_name)) || super
end

#save(validated = true) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/reorm/model.rb', line 37

def save(validated=true)
  if validated && !valid?
    raise Error, "Validation error encountered saving an instance of the #{self.class.name} class."
  end

  action_type = (@properties[primary_key] ? :update : :create)
  if action_type == :create
    fire_events(events: [:before_create, :before_save])
  else
    fire_events(events: [:before_update, :before_save])
  end

  Reorm.connection do |connection|
    ensure_table_exists(connection)
    if !@properties.include?(primary_key)
      result = r.table(table_name).insert(self.to_h, return_changes: true).run(connection)
      if !result["inserted"] || result["inserted"] != 1
        raise Error, "Creation of database record for an instance of the #{self.class.name} class failed."
      end
      @properties[primary_key] = result["generated_keys"].first
    else
      result = r.table(table_name).update(self.to_h).run(connection)
      if !result["replaced"] || !result["replaced"] == 1
        raise Error, "Update of database record for an instance of the #{self.class.name} class failed."
      end
    end
  end

  if action_type == :create
    fire_events(events: [:after_save, :after_create])
  else
    fire_events(events: [:after_save, :after_update])
  end
  self
end

#set_properties(settings = {}) ⇒ Object



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

def set_properties(settings={})
  settings.each do |property, value|
    set_property(property, value)
  end
  self
end

#set_property(property, value) ⇒ Object



114
115
116
117
# File 'lib/reorm/model.rb', line 114

def set_property(property, value)
  self.__send__(setter_name(property), value)
  self
end

#to_hObject



149
150
151
# File 'lib/reorm/model.rb', line 149

def to_h
  {}.merge(@properties)
end

#update(properties = {}) ⇒ Object



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

def update(properties={})
  properties.each do |property, value|
    set_property(property, value)
  end
  self.save if !properties.empty?
  self
end

#valid?Boolean

Returns:

  • (Boolean)


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

def valid?
  validate
  @errors.clear?
end

#validateObject



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

def validate
  fire_events(events: [:before_validate])
  @errors.reset
  fire_events(events: [:after_validate])
  self
end