Class: DeepPreloader::Spec
- Inherits:
-
AbstractSpec
- Object
- AbstractSpec
- DeepPreloader::Spec
- Defined in:
- lib/deep_preloader/spec.rb
Instance Attribute Summary collapse
-
#association_specs ⇒ Object
readonly
Returns the value of attribute association_specs.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #hash ⇒ Object
-
#initialize(association_specs = {}) ⇒ Spec
constructor
A new instance of Spec.
- #inspect ⇒ Object
- #merge!(other) ⇒ Object
Methods inherited from AbstractSpec
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_specs ⇒ Object (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 |
#hash ⇒ Object
50 51 52 |
# File 'lib/deep_preloader/spec.rb', line 50 def hash [self.class, self.association_specs].hash end |
#inspect ⇒ Object
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 |