Class: DeepPreloader::Spec

Inherits:
AbstractSpec show all
Defined in:
lib/deep_preloader/spec.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractSpec

#for_type, #polymorphic?

Constructor Details

#initialize(association_specs = {}) ⇒ Spec

Returns a new instance of Spec.



28
29
30
# File 'lib/deep_preloader/spec.rb', line 28

def initialize(association_specs = {})
  @association_specs = association_specs.transform_keys(&:to_s)
end

Instance Attribute Details

#association_specsObject (readonly)

Returns the value of attribute association_specs.



4
5
6
# File 'lib/deep_preloader/spec.rb', line 4

def association_specs
  @association_specs
end

Class Method Details

.parse(data) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/deep_preloader/spec.rb', line 6

def self.parse(data)
  case data
  when Array
    data.inject(self.new) do |acc, v|
      acc.merge!(parse(v))
    end
  when Hash
    assoc_specs = data.each_with_object({}) do |(k, v), h|
      h[k.to_s] = parse(v)
    end
    self.new(assoc_specs)
  when String, Symbol
    self.new({ data.to_s => nil })
  when DeepPreloader::AbstractSpec
    data
  when nil
    nil
  else
    raise ArgumentError.new("Cannot parse invalid hash preload spec: #{hash.inspect}")
  end
end

Instance Method Details

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



54
55
56
# File 'lib/deep_preloader/spec.rb', line 54

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

#hashObject



50
51
52
# File 'lib/deep_preloader/spec.rb', line 50

def hash
  [self.class, self.association_specs].hash
end

#inspectObject



60
61
62
# File 'lib/deep_preloader/spec.rb', line 60

def inspect
  "Spec#{association_specs.inspect}"
end

#merge!(other) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/deep_preloader/spec.rb', line 32

def merge!(other)
  case other
  when nil
    return
  when DeepPreloader::Spec
    other.association_specs.each do |k, v|
      if association_specs[k]
        association_specs[k].merge!(v)
      else
        association_specs[k] = v
      end
    end
  else
    raise ArgumentError.new("Cannot merge #{other.class.name} into #{self.inspect}")
  end
  self
end