Class: Terrestrial::Record

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/terrestrial/record.rb

Direct Known Subclasses

DeletedRecord, UpsertRecord

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mapping, attributes) ⇒ Record



8
9
10
11
# File 'lib/terrestrial/record.rb', line 8

def initialize(mapping, attributes)
  @mapping = mapping
  @attributes = attributes
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



13
14
15
# File 'lib/terrestrial/record.rb', line 13

def attributes
  @attributes
end

#mappingObject (readonly)

Returns the value of attribute mapping.



13
14
15
# File 'lib/terrestrial/record.rb', line 13

def mapping
  @mapping
end

Instance Method Details

#==(other) ⇒ Object



81
82
83
84
# File 'lib/terrestrial/record.rb', line 81

def ==(other)
  other.is_a?(self.class) &&
    [other.mapping, other.attributes] == [mapping, attributes]
end

#deep_cloneObject



77
78
79
# File 'lib/terrestrial/record.rb', line 77

def deep_clone
  new_with_attributes(Marshal.load(Marshal.dump(attributes)))
end

#empty?Boolean



68
69
70
# File 'lib/terrestrial/record.rb', line 68

def empty?
  updatable_attributes.empty?
end

#identityObject



44
45
46
# File 'lib/terrestrial/record.rb', line 44

def identity
  attributes.select { |k,_v| identity_fields.include?(k) }
end

#identity_fieldsObject



48
49
50
# File 'lib/terrestrial/record.rb', line 48

def identity_fields
  mapping.primary_key
end

#identity_valuesObject



40
41
42
# File 'lib/terrestrial/record.rb', line 40

def identity_values
  identity.values
end

#if_delete(&block) ⇒ Object



24
25
26
# File 'lib/terrestrial/record.rb', line 24

def if_delete(&block)
  self
end

#if_upsert(&block) ⇒ Object



20
21
22
# File 'lib/terrestrial/record.rb', line 20

def if_upsert(&block)
  self
end

#keysObject



36
37
38
# File 'lib/terrestrial/record.rb', line 36

def keys
  attributes.keys
end

#merge(more_attributes) ⇒ Object



52
53
54
# File 'lib/terrestrial/record.rb', line 52

def merge(more_attributes)
  new_with_attributes(attributes.merge(more_attributes))
end

#merge!(more_attributes) ⇒ Object



56
57
58
# File 'lib/terrestrial/record.rb', line 56

def merge!(more_attributes)
  attributes.merge!(more_attributes)
end

#namespaceObject



16
17
18
# File 'lib/terrestrial/record.rb', line 16

def namespace
  mapping.namespace
end

#reject(&block) ⇒ Object



60
61
62
# File 'lib/terrestrial/record.rb', line 60

def reject(&block)
  new_with_attributes(updatable_attributes.reject(&block).merge(identity))
end

#subset?(other_record) ⇒ Boolean



72
73
74
75
# File 'lib/terrestrial/record.rb', line 72

def subset?(other_record)
  mapping == other_record.mapping &&
    to_set.subset?(other_record.to_set)
end

#to_hObject



64
65
66
# File 'lib/terrestrial/record.rb', line 64

def to_h
  attributes.to_h
end

#updatable?Boolean



28
29
30
# File 'lib/terrestrial/record.rb', line 28

def updatable?
  updatable_attributes.any?
end

#updatable_attributesObject



32
33
34
# File 'lib/terrestrial/record.rb', line 32

def updatable_attributes
  attributes.reject { |k, _v| non_updatable_fields.include?(k) }
end