Class: ActiveRemote::Base

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Callbacks
Includes:
ActiveModel::Attributes, ActiveModel::Model, ActiveModel::Validations::Callbacks, Association, AttributeMethods, DSL, Dirty, Integration, Persistence, PrimaryKey, QueryAttributes, RPC, ScopeKeys, Search, Serialization, Validations
Defined in:
lib/active_remote/base.rb

Instance Method Summary collapse

Methods included from Validations

#save, #save!, #valid?

Methods included from Dirty

#reload, #remote, #save, #save!

Methods included from Serialization

#add_errors

Methods included from Search

#reload

Methods included from ScopeKeys

#scope_key_hash, #scope_keys

Methods included from RPC

#assign_attributes_from_rpc, #remote_call, #rpc

Methods included from PrimaryKey

#primary_key, #to_key

Methods included from Persistence

#delete, #delete!, #destroy, #destroy!, #has_errors?, #instantiate, #new_record?, #persisted?, #readonly!, #readonly?, #remote, #save, #save!, #success?, #update_attribute, #update_attributes, #update_attributes!

Methods included from QueryAttributes

#query_attribute

Methods included from Integration

#cache_key, #cache_key_with_version, #cache_version, #to_param

Methods included from AttributeMethods

#[], #[]=, #attribute_for_inspect, #attribute_names

Constructor Details

#initialize(attributes = {}) ⇒ Base

Returns a new instance of Base.



48
49
50
51
52
53
54
55
# File 'lib/active_remote/base.rb', line 48

def initialize(attributes = {})
  super
  @new_record = true

  run_callbacks :initialize do
    yield self if block_given?
  end
end

Instance Method Details

#<=>(other) ⇒ Object

Allows sort on objects



75
76
77
78
79
80
81
# File 'lib/active_remote/base.rb', line 75

def <=>(other)
  if other.is_a?(self.class)
    to_key <=> other.to_key
  else
    super
  end
end

#==(other) ⇒ Object Also known as: eql?

Returns true if comparison_object is the same exact object, or comparison_object is of the same type and self has an ID and it is equal to comparison_object.id.

Note that new records are different from any other record by definition, unless the other record is the receiver itself. Besides, if you fetch existing records with select and leave the ID out, you’re on your own, this predicate will return false.

Note also that destroying a record preserves its ID in the model instance, so deleted models are still comparable.



66
67
68
69
70
71
# File 'lib/active_remote/base.rb', line 66

def ==(other)
  super ||
    other.instance_of?(self.class) &&
      !send(primary_key).nil? &&
      other.send(primary_key) == send(primary_key)
end

#freezeObject



83
84
85
86
# File 'lib/active_remote/base.rb', line 83

def freeze
  @attributes.freeze
  self
end

#frozen?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/active_remote/base.rb', line 88

def frozen?
  @attributes.frozen?
end

#init_with(attributes) ⇒ Object

Initialize an object with the attributes hash directly When used with allocate, bypasses initialize



94
95
96
97
98
99
100
101
# File 'lib/active_remote/base.rb', line 94

def init_with(attributes)
  @attributes = attributes
  @new_record = false

  run_callbacks :initialize

  self
end

#inspectObject

Returns the contents of the record as a nicely formatted string.



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/active_remote/base.rb', line 104

def inspect
  # We check defined?(@attributes) not to issue warnings if the object is
  # allocated but not initialized.
  inspection = if defined?(@attributes) && @attributes
    attribute_names.collect do |name, _|
      if attribute?(name)
        "#{name}: #{attribute_for_inspect(name)}"
      else
        name
      end
    end.compact.join(", ")
  else
    "not initialized"
  end

  "#<#{self.class} #{inspection}>"
end

#slice(*methods) ⇒ Object

Returns a hash of the given methods with their names as keys and returned values as values.



123
124
125
# File 'lib/active_remote/base.rb', line 123

def slice(*methods)
  methods.flatten.map! { |method| [method, public_send(method)] }.to_h.with_indifferent_access
end