Class: Quill::BaseModel

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
lib/quill/base_model.rb

Direct Known Subclasses

ActivityModel, ActivitySession

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ BaseModel

Returns a new instance of BaseModel.



31
32
33
34
35
# File 'lib/quill/base_model.rb', line 31

def initialize *args
  @data = {}
  super
  load_model_attributes
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



3
4
5
# File 'lib/quill/base_model.rb', line 3

def access_token
  @access_token
end

#idObject

Returns the value of attribute id.



3
4
5
# File 'lib/quill/base_model.rb', line 3

def id
  @id
end

Class Method Details

.attributes(*attrs) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/quill/base_model.rb', line 14

def self.attributes *attrs
  return @attributes if defined?(@attributes)
  @attributes = attrs

  attrs.dup.unshift(*@special_attrs).each do |attr|
    class_eval <<-CODE
      def #{attr}
        @data[:#{attr}]
      end

      def #{attr}= value
        @data[:#{attr}] = value
      end
    CODE
  end
end

.inherited(subclass) ⇒ Object



10
11
12
# File 'lib/quill/base_model.rb', line 10

def self.inherited subclass
  subclass.special_attrs *@special_attrs
end

.special_attrs(*attrs) ⇒ Object



5
6
7
8
# File 'lib/quill/base_model.rb', line 5

def self.special_attrs *attrs
  return @special_attrs unless attrs.any?
  @special_attrs = attrs
end

Instance Method Details

#inspectObject



97
98
99
# File 'lib/quill/base_model.rb', line 97

def inspect
  %Q|#<#{self.class.name} #{@data.map{|k,v| "#{k}=#{v.inspect}"}.join(' ')}>|
end

#load_model_attributesObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/quill/base_model.rb', line 61

def load_model_attributes
  return unless key_present?

  object = find
  object.data = {} if object.data.nil?
  attrs = {}

  self.id = object.uid

  # load attributes defined on the superclass. These attributes
  # are designated by Quill.
  self.class.special_attrs.each do |attr|
    attrs[attr] = object.send(attr)
  end

  # load user defined attributes. This is arbitrary data that the app
  # has stored for this record.
  self.class.attributes.each do |attr|
    begin
      if object.data[attr].to_s[0..2] == '---'
        attrs[attr] = YAML.load(object.data[attr])
      else
        attrs[attr] = object.data[attr]
      end
    rescue Psych::SyntaxError
      attrs[attr] = object.data[attr]
    end
  end

  @data.reverse_merge!(attrs)
end

#persistObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/quill/base_model.rb', line 41

def persist
  data = @data.except(*self.class.special_attrs.dup)
  serialized_data = {}

  data.each do |key, value|
    serialized_data[key] = value.to_yaml
  end

  params = { data: serialized_data }

  self.class.special_attrs.each do |attr|
    params[attr] = send(attr)
  end

  params = filter_params(params) if respond_to?(:filter_params)
  object = persist_params params
  self.id = object.uid if id.blank?
  true
end

#save(options = {}) ⇒ Object



37
38
39
# File 'lib/quill/base_model.rb', line 37

def save(options={})
  perform_validations(options) ? persist : false
end

#save!Object



93
94
95
# File 'lib/quill/base_model.rb', line 93

def save!
  save || raise
end