Class: Perry::Base

Inherits:
Object
  • Object
show all
Includes:
Associations::Contains, Associations::External, Scopes, Serialization, Support::ClassAttributes
Defined in:
lib/perry/base.rb

Constant Summary collapse

DEFAULT_PRIMARY_KEY =
:id

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Scopes

included

Methods included from Serialization

included

Methods included from Associations::External

included

Methods included from Associations::Contains

included

Methods included from Support::ClassAttributes

included

Constructor Details

#initialize(attributes = {}) ⇒ Base

Returns a new instance of Base.



32
33
34
35
# File 'lib/perry/base.rb', line 32

def initialize(attributes={})
  self.new_record = true
  set_attributes(attributes)
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



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

def attributes
  @attributes
end

#new_recordObject Also known as: new_record?

Returns the value of attribute new_record.



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

def new_record
  @new_record
end

#savedObject Also known as: saved?

Returns the value of attribute saved.



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

def saved
  @saved
end

#write_optionsObject

Returns the value of attribute write_options.



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

def write_options
  @write_options
end

Class Method Details

.inherited(subclass) ⇒ Object



111
112
113
114
# File 'lib/perry/base.rb', line 111

def inherited(subclass)
  update_extension_map(self, subclass)
  super
end

.new_from_data_store(hash) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'lib/perry/base.rb', line 91

def new_from_data_store(hash)
  if hash.nil?
    nil
  else
    record = self.new(hash)
    record.new_record = false
    record
  end
end

.primary_keyObject



77
78
79
# File 'lib/perry/base.rb', line 77

def primary_key
  @primary_key || DEFAULT_PRIMARY_KEY
end

.respond_to?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


116
117
118
119
# File 'lib/perry/base.rb', line 116

def respond_to?(method, include_private=false)
  super ||
  scoped.dynamic_finder_method(method)
end

.set_primary_key(attribute) ⇒ Object

Allows you to specify an attribute other than :id to use as your models primary key.



84
85
86
87
88
89
# File 'lib/perry/base.rb', line 84

def set_primary_key(attribute)
  unless defined_attributes.include?(attribute.to_s)
    raise Perry::PerryError.new("cannot set primary key to non-existent attribute")
  end
  @primary_key = attribute.to_sym
end

.unscopedObject



101
102
103
104
105
106
107
108
109
# File 'lib/perry/base.rb', line 101

def unscoped
  current_scopes = self.scoped_methods
  self.scoped_methods = []
  begin
    yield
  ensure
    self.scoped_methods = current_scopes
  end
end

Instance Method Details

#[](attribute) ⇒ Object



37
38
39
# File 'lib/perry/base.rb', line 37

def [](attribute)
  @attributes[attribute.to_s]
end

#errorsObject



41
42
43
# File 'lib/perry/base.rb', line 41

def errors
  @errors ||= {}
end

#primary_keyObject



45
46
47
# File 'lib/perry/base.rb', line 45

def primary_key
  self.class.primary_key
end