Module: Diograph

Defined in:
lib/diograph.rb,
lib/diograph/room.rb,
lib/diograph/diory.rb,
lib/diograph/diograph.rb,
lib/diograph/connection.rb

Defined Under Namespace

Classes: Connection, Diograph, Diory, Room

Class Method Summary collapse

Class Method Details

.create_connection_from_hash(diory, connection_hash) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/diograph.rb', line 82

def create_connection_from_hash(diory, connection_hash)
  connected_diory_hash = connection_hash["diory"]
  to_diory = find_or_create_diory_from_attributes(diory.room, connected_diory_hash)
  connection = Connection.new(
    from_diory: diory,
    to_diory: to_diory,
    room_id: diory.room.room_id
  )
  connection.save if connection.respond_to?(:save)
  connection
end


23
24
25
26
27
28
# File 'lib/diograph.rb', line 23

def create_connections_and_related_diories(diory, diory_hash)
  diory_hash["connections"].keys.each do |key|
    to_diory = find_or_create_diory_from_attributes(diory.room, diory_hash["connections"][key]["diory"])
    diory.connect_to(to_diory)
  end
end

.create_diory_and_connections_from_diograph(room, diory_hash) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/diograph.rb', line 30

def create_diory_and_connections_from_diograph(room, diory_hash)
  diory_hash = JSON.parse(diory_hash) if diory_hash.class == String
  diory = find_or_create_diory_from_attributes(room, diory_hash)
  diory_hash["connections"].keys.each do |key|
    connection = create_connection_from_hash(diory, diory_hash["connections"][key])
    diory.connections << connection
  end
  diory
end

.create_diory_from_attributes(room, args = {}) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/diograph.rb', line 65

def create_diory_from_attributes(room, args={})
  diory = Diory.new(
    diory_id: args["diory_id"] || args["ID"],
    name: args["name"],
    diory_type: args["type"],
    date: args["date"],
    address: args["address"],
    background: args["background"],
    modified: args["modified"] || DateTime.now,
    created: args["created"] || DateTime.now,
    room: room
  )
  room.diories << diory if diory.respond_to?(:save)
  diory.save if diory.respond_to?(:save)
  diory
end

.create_from_connection(connection) ⇒ Object

0.1.0 stuff ###



135
136
137
138
139
# File 'lib/diograph.rb', line 135

def create_from_connection(connection)
  d = connection.to_diory
  connection_dg = create_from_diory(d, false)
  connection_dg
end

.create_from_diory(diory, include_connections = true) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/diograph.rb', line 110

def create_from_diory(diory, include_connections=true)
  dg = {
    diory_id: diory.diory_id,
    type: diory.diory_type,
    text: diory.name,
    image: diory.background,
    style: { text: { color: "white"} },
    data: { geo: { latitude: diory.latitude, longitude: diory.longitude, zoom: 10 } },
    address: diory.address,
    date: diory.date,
    created: diory.created,
    modified: diory.modified,
  }
  if include_connections
    dg[:diorys] = {}
    diory.connections.each_with_index do |c, i|
      dg[:diorys][i] = create_from_connection(c)
    end
  end
  return JSON.parse dg.to_json # stringify keys
end

.create_from_room(room) ⇒ Object

FROM diories TO diograph ###



99
100
101
102
103
104
105
106
107
108
# File 'lib/diograph.rb', line 99

def create_from_room(room)
  dg = {
    name: room.name,
    roomID: room.room_id,
    created: room.created,
    modified: room.modified,
    diories: room.diories.map{|d| create_from_diory(d)}
  }
  return JSON.parse dg.to_json # stringify keys
end

.create_room_from_attributes(args = {}) ⇒ Object

FROM diograph TO diories ###



45
46
47
48
49
50
51
52
53
54
# File 'lib/diograph.rb', line 45

def create_room_from_attributes(args={})
  room = Room.new(
    name: args["name"],
    room_id: args["roomID"],
    created: args["created"],
    modified: args["modified"]
  )
  room.save if room.respond_to?(:save)
  room
end

.create_room_from_diograph(diograph) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/diograph.rb', line 12

def create_room_from_diograph(diograph)
  # Parse diograph to hash
  diograph = JSON.parse(diograph) if diograph.class == String
  # Create room
  room = create_room_from_attributes(diograph)
  diograph["diories"].map do |diory_hash|
    create_diory_and_connections_from_diograph(room, diory_hash)
  end
  room
end

.find_or_create_diory_from_attributes(room, args = {}) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/diograph.rb', line 56

def find_or_create_diory_from_attributes(room, args={})
  existing_diory = room.diories.find{|d| d.diory_id == args["ID"]}
  if existing_diory.nil?
    create_diory_from_attributes(room, args)
  else
    existing_diory
  end
end

.is_equal(diograph1, diograph2) ⇒ Object



141
142
143
144
145
# File 'lib/diograph.rb', line 141

def is_equal(diograph1, diograph2)
  one = diograph1["diories"].sort_by{|d| d["ID"]} unless diograph1.nil?
  two = diograph2["diories"].sort_by{|d| d["ID"]} unless diograph2.nil?
  one == two
end