Class: DataGraph::Graph

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/data_graph/graph.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

cpk?, foreign_key, patherize_attrs, primary_keys, reference_key

Constructor Details

#initialize(node, options = {}) ⇒ Graph

Returns a new instance of Graph.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/data_graph/graph.rb', line 13

def initialize(node, options={})
  aliases = options[:aliases]
  subsets = options[:subsets]
  
  @node = node
  @nest_paths = node.nest_paths
  @aliases = @node.aliases
  @aliases.merge!(aliases) if aliases
  
  @paths = {}
  @subsets = {}
  
  subsets = {
    :default => '*',
    :get => node.get_paths,
    :set => node.set_paths
  }.merge(subsets || {})
  
  subsets.each_pair do |name, unresolved_paths|
    resolved_paths  = resolve(unresolved_paths)
    @paths[name]    = resolved_paths
    @subsets[name]  = node.only(resolved_paths)
  end
end

Instance Attribute Details

#aliasesObject (readonly)

Returns the value of attribute aliases.



8
9
10
# File 'lib/data_graph/graph.rb', line 8

def aliases
  @aliases
end

#modelObject (readonly)

Returns the value of attribute model.



7
8
9
# File 'lib/data_graph/graph.rb', line 7

def model
  @model
end

#nodeObject (readonly)

Returns the value of attribute node.



11
12
13
# File 'lib/data_graph/graph.rb', line 11

def node
  @node
end

#pathsObject (readonly)

Returns the value of attribute paths.



9
10
11
# File 'lib/data_graph/graph.rb', line 9

def paths
  @paths
end

#subsetsObject (readonly)

Returns the value of attribute subsets.



10
11
12
# File 'lib/data_graph/graph.rb', line 10

def subsets
  @subsets
end

Instance Method Details

#except(paths) ⇒ Object



50
51
52
# File 'lib/data_graph/graph.rb', line 50

def except(paths)
  node.only validate(:get, resolve(paths))
end

#find(*args) ⇒ Object



38
39
40
# File 'lib/data_graph/graph.rb', line 38

def find(*args)
  node.find(*args)
end

#only(paths) ⇒ Object



46
47
48
# File 'lib/data_graph/graph.rb', line 46

def only(paths)
  node.only validate(:get, resolve(paths))
end

#paginate(*args) ⇒ Object



42
43
44
# File 'lib/data_graph/graph.rb', line 42

def paginate(*args)
  node.paginate(*args)
end

#path(type) ⇒ Object



62
63
64
# File 'lib/data_graph/graph.rb', line 62

def path(type)
  paths[type] or raise "no such path: #{type.inspect}"
end

#resolve(paths) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/data_graph/graph.rb', line 54

def resolve(paths)
  paths = paths.collect {|path| aliases[path.to_s] || path }
  paths.flatten!
  paths.collect! {|path| path.to_s }
  paths.uniq!
  paths
end

#subset(type, default_type = :default) ⇒ Object



66
67
68
# File 'lib/data_graph/graph.rb', line 66

def subset(type, default_type = :default)
  (subsets[type] || subsets[default_type]) or raise "no such subset: #{type.inspect}"
end

#validate(type, paths) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/data_graph/graph.rb', line 70

def validate(type, paths)
  inaccessible_paths = paths - path(type)
  unless inaccessible_paths.empty?
    raise InaccessiblePathError.new(inaccessible_paths)
  end
  paths
end

#validate_attrs(type, attrs) ⇒ Object



78
79
80
81
# File 'lib/data_graph/graph.rb', line 78

def validate_attrs(type, attrs)
  validate(type, patherize_attrs(attrs, @nest_paths))
  attrs
end