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

#disable_dirty_tracking, #enable_dirty_tracking, #reload, #remote, #save, #save!, #skip_dirty_tracking

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
56
57
# File 'lib/active_remote/base.rb', line 48

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

  skip_dirty_tracking do
    run_callbacks :initialize do
      yield self if block_given?
    end
  end
end

Instance Method Details

#<=>(other) ⇒ Object

Allows sort on objects



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

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.



68
69
70
71
72
73
# File 'lib/active_remote/base.rb', line 68

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

#freezeObject



85
86
87
88
# File 'lib/active_remote/base.rb', line 85

def freeze
  @attributes.freeze
  self
end

#frozen?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/active_remote/base.rb', line 90

def frozen?
  @attributes.frozen?
end

#init_with(attributes) ⇒ Object

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



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

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.



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

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.



125
126
127
# File 'lib/active_remote/base.rb', line 125

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