Class: PipeLineDealer::Model::Base

Inherits:
Object
  • Object
show all
Includes:
Concern::Persistance
Defined in:
lib/pipe_line_dealer/model/base.rb,
lib/pipe_line_dealer/model/base/concern/persistance.rb

Direct Known Subclasses

Company, CustomField, Note, Person

Defined Under Namespace

Modules: Concern

Constant Summary

Constants included from Concern::Persistance

Concern::Persistance::IGNORE_ATTRIBUTES_WHEN_SAVING

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Concern::Persistance

#destroy, #new_record?, #persisted?, #save

Constructor Details

#initialize(options = {}) ⇒ Base

Returns a new instance of Base.



11
12
13
14
15
16
17
# File 'lib/pipe_line_dealer/model/base.rb', line 11

def initialize options = {}
  @options       = options
  @persisted     = !!@options[:persisted]
  @collection    = @options[:collection]

  import_attributes! @options[:attributes]
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



8
9
10
# File 'lib/pipe_line_dealer/model/base.rb', line 8

def attributes
  @attributes
end

#idObject (readonly)

Returns the value of attribute id.



9
10
11
# File 'lib/pipe_line_dealer/model/base.rb', line 9

def id
  @id
end

Class Method Details

.attrs(*attrs) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/pipe_line_dealer/model/base.rb', line 37

def self.attrs *attrs
  if attrs.last.kind_of?(Hash)
    options = attrs.pop
  else
    options = {}
  end

  attrs.each do |attr|
    pld_attr attr, options
  end
end

.inherited(subklass) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/pipe_line_dealer/model/base.rb', line 49

def self.inherited(subklass)
  @attributes ||= {}

  # Copy all attributes to the children.
  @attributes.each do |attr, options|
    subklass.pld_attr attr, options
  end
end

.pld_attr(attr, options = {}) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/pipe_line_dealer/model/base.rb', line 58

def self.pld_attr attr, options = {}
  @attributes ||= {}
  @attributes[attr] = options

  # Getter
  define_method attr do
    @attributes[attr.to_s]
  end

  # Setter
  define_method "#{attr}=".to_sym do |value|
    @attributes[attr.to_s] = value
  end
end

Instance Method Details

#==(other) ⇒ Object



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

def ==(other)
  if other.kind_of?(self.class)
    other.attributes == self.attributes
  else
    super(other)
  end
end

#attributes_for_saving(attributes) ⇒ Object

TODO : remove check of methode bestaat



20
21
22
# File 'lib/pipe_line_dealer/model/base.rb', line 20

def attributes_for_saving(attributes)
  attributes
end

#to_jsonObject



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

def to_json
  JSON.dump(@attributes)
end

#to_sObject



73
74
75
# File 'lib/pipe_line_dealer/model/base.rb', line 73

def to_s
  "<PipeLineDealer::Model:#{__id__} klass=#{self.class.name} id=#{@id} attributes=#{@attributes.inspect}>"
end