Class: RecordStore::Record

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/record_store/record.rb

Direct Known Subclasses

A, AAAA, ALIAS, CNAME, MX, NS, SPF, SRV, TXT

Defined Under Namespace

Classes: A, AAAA, ALIAS, CNAME, MX, NS, SPF, SRV, TXT

Constant Summary collapse

FQDN_REGEX =
/\A(\*\.)?([a-z0-9_]+(-[a-z0-9]+)*\._?)+[a-z]{2,}\.\Z/i
CNAME_REGEX =
/\A(\*\.)?([a-z0-9]+(-[a-z0-9]+)*\._?)+[a-z]{2,}\.\Z/i

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record) ⇒ Record

Returns a new instance of Record.



14
15
16
17
18
# File 'lib/record_store/record.rb', line 14

def initialize(record)
  @fqdn = Record.ensure_ends_with_dot(record.fetch(:fqdn))
  @ttl  = record.fetch(:ttl)
  @id   = record.fetch(:record_id, nil)
end

Instance Attribute Details

#fqdnObject

Returns the value of attribute fqdn.



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

def fqdn
  @fqdn
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#ttlObject

Returns the value of attribute ttl.



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

def ttl
  @ttl
end

Class Method Details

.build_from_yaml_definition(yaml_definition) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/record_store/record.rb', line 20

def self.build_from_yaml_definition(yaml_definition)
  record_type = yaml_definition.fetch(:type)

  # TODO: remove backward compatibility support for ALIAS records using cname attribute instead of alias
  #       REMOVE after merging https://github.com/Shopify/record-store/pull/781
  case record_type
  when 'ALIAS'
    if yaml_definition.key?(:cname)
      yaml_definition[:alias] = yaml_definition.delete(:cname)
    end
  end
  Record.const_get(record_type).new(yaml_definition)
end

.ensure_ends_with_dot(fqdn) ⇒ Object



76
77
78
# File 'lib/record_store/record.rb', line 76

def self.ensure_ends_with_dot(fqdn)
  fqdn.end_with?(".") ? fqdn : "#{fqdn}."
end

Instance Method Details

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



50
51
52
# File 'lib/record_store/record.rb', line 50

def ==(other)
  other.class == self.class && other.to_hash == self.to_hash
end

#hashObject



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

def hash
  to_hash.hash
end

#keyObject



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

def key
  "#{type},#{fqdn}"
end

#log!(logger = STDOUT) ⇒ Object



34
35
36
# File 'lib/record_store/record.rb', line 34

def log!(logger=STDOUT)
  logger.puts to_s
end

#to_hashObject



38
39
40
41
42
43
44
# File 'lib/record_store/record.rb', line 38

def to_hash
  {
    type: type,
    fqdn: fqdn,
    ttl: ttl
  }.merge(rdata)
end

#to_jsonObject



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

def to_json
  { ttl: ttl, rdata: rdata }
end

#typeObject



46
47
48
# File 'lib/record_store/record.rb', line 46

def type
  self.class.name.split('::').last
end