Module: RubyHaze::Persisted::InstanceMethods

Defined in:
lib/rubyhaze/persisted/model.rb

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ Object



108
109
110
111
# File 'lib/rubyhaze/persisted/model.rb', line 108

def ==(other)
  return false unless other.respond_to? :to_ary
  to_ary == other.to_ary
end

#attribute(key) ⇒ Object



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

def attribute(key)
  instance_variable_get("@#{key}")
end

#attribute=(key, value) ⇒ Object



73
74
75
76
# File 'lib/rubyhaze/persisted/model.rb', line 73

def attribute=(key, value)
  send("#{key}_will_change!") unless value == attribute(key)
  instance_variable_set("@#{key}", value)
end

#attribute?(key) ⇒ Boolean



78
79
80
# File 'lib/rubyhaze/persisted/model.rb', line 78

def attribute?(key)
  instance_variable_get("@#{key}").present?
end

#attribute_namesObject



82
83
84
# File 'lib/rubyhaze/persisted/model.rb', line 82

def attribute_names
  self.class.attribute_names
end

#attribute_optionsObject



90
91
92
# File 'lib/rubyhaze/persisted/model.rb', line 90

def attribute_options
  self.class.attribute_options
end

#attribute_typesObject



86
87
88
# File 'lib/rubyhaze/persisted/model.rb', line 86

def attribute_types
  self.class.attribute_types
end

#attributesObject



94
95
96
97
98
# File 'lib/rubyhaze/persisted/model.rb', line 94

def attributes
  attrs = {}
  attribute_names.each { |name| attrs[name.to_s] = attribute(name) }
  attrs
end

#createObject



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

def create
  _run_create_callbacks do
    @uid ||= RubyHaze.random_uuid
    self.class.map[uid] = shadow_object
    @previously_changed = changes
    @changed_attributes.clear
    @new_record = false
    uid
  end
end

#create_or_updateObject



132
133
134
135
# File 'lib/rubyhaze/persisted/model.rb', line 132

def create_or_update
  result = new_record? ? create : update
  result != false
end

#destroyObject Also known as: delete



172
173
174
175
176
177
178
179
180
# File 'lib/rubyhaze/persisted/model.rb', line 172

def destroy
  _run_destroy_callbacks do
    if persisted?
      self.class.map.remove uid
    end
    @destroyed = true
    freeze
  end
end

#destroyed?Boolean



65
66
67
# File 'lib/rubyhaze/persisted/model.rb', line 65

def destroyed?
  @destroyed
end

#initialize(options = {}) ⇒ Object



50
51
52
53
54
55
# File 'lib/rubyhaze/persisted/model.rb', line 50

def initialize(options = {})
  options.each { |name, value| send "#{name}=", value }
  @callbacks = []
  @new_record = true
  @destroyed = false
end

#loadObject Also known as: reload, reset



158
159
160
161
162
163
164
165
166
167
168
# File 'lib/rubyhaze/persisted/model.rb', line 158

def load
  _run_load_callbacks do
    raise "Missing uid for load" if uid.blank?
    found = self.class.map[uid]
    raise "Record not found" unless found
    load_shadow_object(found)
    @changed_attributes.clear
    @new_record = false
    self
  end
end

#load_shadow_object(shadow) ⇒ Object



117
118
119
120
121
122
# File 'lib/rubyhaze/persisted/model.rb', line 117

def load_shadow_object(shadow)
  attribute_names.each do |name|
    send "attribute=", name, shadow.send(name)
  end
  self
end

#new_record?Boolean



57
58
59
# File 'lib/rubyhaze/persisted/model.rb', line 57

def new_record?
  @new_record
end

#persisted?Boolean



61
62
63
# File 'lib/rubyhaze/persisted/model.rb', line 61

def persisted?
  !(new_record? || destroyed?)
end

#saveObject



124
125
126
# File 'lib/rubyhaze/persisted/model.rb', line 124

def save
  create_or_update
end

#save!Object



128
129
130
# File 'lib/rubyhaze/persisted/model.rb', line 128

def save!
  create_or_update || raise("Not saved")
end

#shadow_objectObject



113
114
115
# File 'lib/rubyhaze/persisted/model.rb', line 113

def shadow_object
  self.class.map_java_class.new *values
end

#to_aryObject



104
105
106
# File 'lib/rubyhaze/persisted/model.rb', line 104

def to_ary
  attribute_names.map { |name| [name.to_s, instance_variable_get("@#{name}")] }.unshift ['class', self.class.name]
end

#to_keyObject



189
190
191
# File 'lib/rubyhaze/persisted/model.rb', line 189

def to_key
  persisted? ? [uid] : nil
end

#to_sObject Also known as: inspect



183
184
185
# File 'lib/rubyhaze/persisted/model.rb', line 183

def to_s
  "<#{self.class.name}:#{object_id} #{to_ary[1..-1].map { |k, v| "#{k}=#{v}" }.join(" ")} >"
end

#updateObject



148
149
150
151
152
153
154
155
156
# File 'lib/rubyhaze/persisted/model.rb', line 148

def update
  _run_update_callbacks do
    raise "Missing uid" unless uid?
    self.class.map[uid] = shadow_object
    @previously_changed = changes
    @changed_attributes.clear
    true
  end
end

#valuesObject



100
101
102
# File 'lib/rubyhaze/persisted/model.rb', line 100

def values
  attribute_names.map { |name| attribute(name) }
end