Class: Kartograph::Map

Inherits:
Object
  • Object
show all
Defined in:
lib/kartograph/map.rb

Instance Method Summary collapse

Constructor Details

#initializeMap

Returns a new instance of Map.



5
6
7
# File 'lib/kartograph/map.rb', line 5

def initialize
  @scope_mutex = Mutex.new
end

Instance Method Details

#==(other) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/kartograph/map.rb', line 83

def ==(other)
  methods = %i(properties root_keys mapping cache cache_key)
  methods.inject(true) do |current_value, method|
    break unless current_value
    send(method) == other.send(method)
  end
end

#cache(object = nil) ⇒ Object



48
49
50
51
# File 'lib/kartograph/map.rb', line 48

def cache(object = nil)
  @cache = object unless object.nil?
  @cache.nil? ? Kartograph.default_cache : @cache
end

#cache_key(&calculator) ⇒ Object



53
54
55
56
# File 'lib/kartograph/map.rb', line 53

def cache_key(&calculator)
  @cache_calculator = calculator if block_given?
  @cache_calculator.nil? ? Kartograph.default_cache_key : @cache_calculator
end

#dupObject



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

def dup
  Kartograph::Map.new.tap do |map|
    self.properties.each do |property|
      map.properties << property.dup
    end

    map.mapping self.mapping

    self.root_keys.each do |rk|
      map.root_keys << rk
    end

    map.cache self.cache
    map.cache_key &self.cache_key if self.cache_key
  end
end

#mapping(klass = nil) ⇒ Object



39
40
41
42
# File 'lib/kartograph/map.rb', line 39

def mapping(klass = nil)
  @mapping = klass if klass
  @mapping
end

#propertiesObject



21
22
23
# File 'lib/kartograph/map.rb', line 21

def properties
  @properties ||= PropertyCollection.new
end

#property(*args, &block) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/kartograph/map.rb', line 9

def property(*args, &block)
  options = args.last.is_a?(Hash) ? args.pop : {}

  # Append scopes if we're currently mapping in a scoped block
  options[:scopes] ||= []
  options[:scopes] += Array(@current_scopes)

  args.each do |prop|
    properties << Property.new(prop, options, &block)
  end
end

#root_key(options) ⇒ Object



44
45
46
# File 'lib/kartograph/map.rb', line 44

def root_key(options)
  root_keys << RootKey.new(options)
end

#root_key_for(scope, type) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/kartograph/map.rb', line 58

def root_key_for(scope, type)
  return unless %i(singular plural).include?(type)

  if (root_key = root_keys.select {|rk| rk.scopes.include?(scope) }[0])
    root_key.send(type)
  end
end

#root_keysObject



35
36
37
# File 'lib/kartograph/map.rb', line 35

def root_keys
  @root_keys ||= []
end

#scoped(*scopes, &block) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/kartograph/map.rb', line 25

def scoped(*scopes, &block)
  @scope_mutex.synchronize do
    @current_scopes = scopes

    instance_eval(&block) if block_given?

    @current_scopes = nil
  end
end