Class: MLS::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/mls/resource.rb

Direct Known Subclasses

Account, Address, Floorplan, Flyer, Listing, PDF, Photo, Region, Tour, Video

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}, persisted = false) ⇒ Resource

Returns a new instance of Resource.



10
11
12
13
14
15
16
# File 'lib/mls/resource.rb', line 10

def initialize(attributes = {}, persisted = false)
  @persisted = persisted
  @errors = {}
  
  set_default_values
  update_attributes(attributes)
end

Instance Attribute Details

#errorsObject

Returns the value of attribute errors.



4
5
6
# File 'lib/mls/resource.rb', line 4

def errors
  @errors
end

#persistedObject (readonly)

Returns the value of attribute persisted.



3
4
5
# File 'lib/mls/resource.rb', line 3

def persisted
  @persisted
end

Class Method Details

.inherited(subclass) ⇒ Object



6
7
8
# File 'lib/mls/resource.rb', line 6

def self.inherited(subclass)
  subclass.extend(MLS::Model)
end

Instance Method Details

#==(other) ⇒ Object



42
43
44
# File 'lib/mls/resource.rb', line 42

def ==(other)
  self.class == other.class && properties_for_comparison == other.properties_for_comparison
end

#create!Object



38
39
40
# File 'lib/mls/resource.rb', line 38

def create!
  create || raise(MLS::Exception::RecordInvalid)
end

#new_record?Boolean

Returns:



18
19
20
# File 'lib/mls/resource.rb', line 18

def new_record?
  !@persisted
end

#persisted?Boolean

Returns:



22
23
24
# File 'lib/mls/resource.rb', line 22

def persisted?
  @persisted
end

#propertiesObject

Properties ===================================================================================================



48
49
50
# File 'lib/mls/resource.rb', line 48

def properties
  self.class.properties
end

#properties_excluded_from_comparisonObject



60
61
62
# File 'lib/mls/resource.rb', line 60

def properties_excluded_from_comparison
  self.class.properties_excluded_from_comparison
end

#properties_for_comparisonObject



52
53
54
55
56
57
58
# File 'lib/mls/resource.rb', line 52

def properties_for_comparison
  compare = {}
  properties.reject{ |k, p| properties_excluded_from_comparison.include?(k) }.each do |k, p|
    compare[k] = self.send(properties[k].name.to_sym)
  end
  compare
end

#saveObject



26
27
28
# File 'lib/mls/resource.rb', line 26

def save
  new_record? ? create : update
end

#save!Object



30
31
32
# File 'lib/mls/resource.rb', line 30

def save!
  new_record? ? create! : update!
end

#set_default_valuesObject



64
65
66
67
68
# File 'lib/mls/resource.rb', line 64

def set_default_values
  properties.each do |name, property|
    self.send("#{name}=".to_sym, property.default) if property.default
  end
end

#to_hashObject

Combo Breaker ================================================================================================



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/mls/resource.rb', line 76

def to_hash
  hash = {}
  
  properties.each do |name, property|
    serialize = property.options[:serialize]
    serialize = :always if serialize.nil?
    case serialize
    when :always
      hash[name] = property.dump(self.send(name))
    when :if_present
      hash[name] = property.dump(self.send(name)) if self.send(name)
    end
  end
  
  hash
end

#to_keyObject

for rails form stuff



95
96
97
# File 'lib/mls/resource.rb', line 95

def to_key
  persisted? ? [id] : nil
end

#update!Object



34
35
36
# File 'lib/mls/resource.rb', line 34

def update!
  update || raise(MLS::Exception::RecordInvalid)
end

#update_attributes(attrs) ⇒ Object



70
71
72
# File 'lib/mls/resource.rb', line 70

def update_attributes(attrs)
  attrs.each { |k, v| self.send("#{k}=".to_sym, v) } unless attrs.nil?
end