Class: Capsula::Encapsulator
- Inherits:
-
Object
- Object
- Capsula::Encapsulator
- Defined in:
- lib/capsula/encapsulator.rb
Instance Method Summary collapse
- #encapsulate ⇒ Object
-
#initialize(items:, declarations:, keys:) ⇒ Encapsulator
constructor
Main encapsulator functionality.
- #preload ⇒ Object
- #preload_and_encapsulate ⇒ Object
Constructor Details
#initialize(items:, declarations:, keys:) ⇒ Encapsulator
Main encapsulator functionality
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/capsula/encapsulator.rb', line 8 def initialize items:, declarations:, keys: # wrap up not wrapped obects @items = wrap_non_wrapped(items) @declarations = declarations @keys = [] @keys_values = {} ## # Parse all declarations by first-level plans. # # We get a nested plans structure: # [ # {fruits: [:tree, {bugs: [:locations]]}, # :vegetables # ] # For current objects which we will encapsulate now # (call it first-level objects) # we need to preload only :fruits and :vegetables, # But when the turn comes to encapsulate :fruits, # we will transfer plans for :fruits to fruits-encapsulator, # and let him deside how to encapsulate tree and bugs into fruits, # and then, bugs-encapsulator shoud encapsulate locations and so on. # keys.each do |i| if i.is_a?(Hash) k, v = i.first @keys << k @keys_values[k] = v else @keys << i end end # for preloaded objects @collections = {} end |
Instance Method Details
#encapsulate ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/capsula/encapsulator.rb', line 73 def encapsulate @collections.each do |plan_key, accessories| if accessories.has_key?(:delegator) # Delegate to user's encapsulator encapsulate_by_delegator( accessories[:delegator], plan_key ) else # Use native encapsulator native_encapsulation( plan_key, accessories[:collection], accessories[:declaration] ) end end end |
#preload ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/capsula/encapsulator.rb', line 58 def preload @keys.each do |key| dec = @declarations[key] opt = (key) if dec.has_key?(:delegate_to) # Delegate to user's encapsulator preload_by_delegator(dec[:delegate_to], key, opt) else # Use native encapsulator native_preload(key, dec, opt) end end end |
#preload_and_encapsulate ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/capsula/encapsulator.rb', line 46 def preload_and_encapsulate if (@keys - @declarations.keys).any? raise StandardError.new("unknown relation keys: #{ (@keys - @declarations.keys) }") end preload encapsulate @items end |