Class: FixtureFox::Anchors

Inherits:
Object
  • Object
show all
Defined in:
lib/fixture_fox/anchor.rb

Overview

TODO: Remove Anchors and replace with a simple hash

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, anchors = []) ⇒ Anchors

Initialize an Anchors object

anchors is an array of hashes of name, table, and id values. It is the same as the output from #to_yaml



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/fixture_fox/anchor.rb', line 27

def initialize(type, anchors = [])
  constrain type, PgGraph::Type
  constrain anchors, [Hash], NilClass
  @type = type
  @anchors = {}
  (anchors || []).each { |anchor| create(anchor[:name], anchor[:table], id: anchor[:id]) }

#     @anchors = anchors.map { |name, record_uid|
#       raise "This is actually used!"
#       table_uid, id = PgGraph::Data::Record.split_uid(record_uid)
#       Anchor.new(name, type.dot(table_uid).record_type, id)
#     }.to_h
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



21
22
23
# File 'lib/fixture_fox/anchor.rb', line 21

def type
  @type
end

Class Method Details

.load(type, filename) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/fixture_fox/anchor.rb', line 76

def self.load(type, filename)
  anchors = Anchors.new(type)
  YAML.load(IO.read(filename)).each { |entry|
    anchors.create(entry[:name], entry[:table], id: entry[:id])
  }
  anchors
end

Instance Method Details

#[](name) ⇒ Object



56
57
58
59
# File 'lib/fixture_fox/anchor.rb', line 56

def [](name)
  constrain name, Symbol
  @anchors[name]
end

#create(name, arg, id: nil) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/fixture_fox/anchor.rb', line 43

def create(name, arg, id: nil)
  constrain name, Symbol
  constrain arg, PgGraph::Type::RecordType, String
  constrain id, NilClass, Integer # id is initially nil for anchors declared in the source
  !@anchors.key?(name) or raise Error, "Duplicate anchor: #{name.inspect}"
  type =
      case arg
        when PgGraph::Type::RecordType; arg
        when String; @type.dot(arg) or raise Error, "Illegal path: #{path.inspect}"
      end
  @anchors[name] = Anchor.new(name, type.record_type, id: id)
end

#dumpObject



84
85
86
87
88
89
90
# File 'lib/fixture_fox/anchor.rb', line 84

def dump
  puts "Anchors"
  indent {
    puts "type: #{type.uid}"
    puts "anchors: #{@anchors.keys}"
  }
end

#merge!(anchors) ⇒ Object



61
62
63
64
65
66
# File 'lib/fixture_fox/anchor.rb', line 61

def merge!(anchors)
  constrain anchors, Anchors
  type == anchors.type or raise Error, "anchor type mismatch"
  @anchors.merge!(anchors.instance_variable_get(:@anchors))
  self
end

#save(filename) ⇒ Object



72
73
74
# File 'lib/fixture_fox/anchor.rb', line 72

def save(filename)
  IO.write(filename, YAML.dump(to_yaml))
end

#to_yamlObject



68
69
70
# File 'lib/fixture_fox/anchor.rb', line 68

def to_yaml
  @anchors.values.map { |anchor| { name: anchor.name, table: anchor.table_uid, id: anchor.id } }
end