Class: Connect::Selector
- Inherits:
 - 
      Object
      
        
- Object
 - Connect::Selector
 
 
- Defined in:
 - lib/connect/selector.rb
 
Overview
This class implements the functionality to select parts of the current value
Constant Summary collapse
- TO_RESOURCE_REGEX =
 /^\.to_resource\('([a-zA-Z]+)'\)/- SLICE_REGEX =
 /^\.slice\(\s*(['|"]\w*['|"],*\s*)+\)/- SLICE_CONTENT_REGEX =
 /^\.slice_content\(\s*(['|"]\w*['|"],*\s*)+\)/
Class Method Summary collapse
- 
  
    
      .run(value, selector)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
Convenience method.
 
Instance Method Summary collapse
- 
  
    
      #initialize(value, selector)  ⇒ Selector 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    
A new instance of Selector.
 - 
  
    
      #run  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
apply the current selector on the current value.
 
Constructor Details
#initialize(value, selector) ⇒ Selector
Returns a new instance of Selector.
      14 15 16 17 18  | 
    
      # File 'lib/connect/selector.rb', line 14 def initialize(value, selector) @value = value @selection_value = convert(value) @selector = selector end  | 
  
Class Method Details
.run(value, selector) ⇒ Object
Convenience method
      62 63 64 65  | 
    
      # File 'lib/connect/selector.rb', line 62 def self.run(value, selector) instance = new(value, selector) instance.run end  | 
  
Instance Method Details
#run ⇒ Object
apply the current selector on the current value
rubocop:disable PerceivedComplexity
      26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53  | 
    
      # File 'lib/connect/selector.rb', line 26 def run if @selector && @selection_value begin case when @selector =~ TO_RESOURCE_REGEX && @value.is_a?(Connect::ObjectRepresentation) # # The to_resource is a special selector when operating on an object. It will transform the # object into a valid resource hash and filter all attributes not available in the selected # object. # convert_to_resource when @selector =~ SLICE_REGEX && @value.is_a?(Connect::ObjectRepresentation) slice_object when @selector =~ SLICE_CONTENT_REGEX && @value.is_a?(Connect::ObjectRepresentation) slice_content when @selector =~ SLICE_REGEX && @value.is_a?(Hash) slice_hash else instance_eval("@selection_value#{@selector}") end rescue => e raise ArgumentError, "usage of invalid selector '#{@selector}' on value '#{@selection_value}', resulted in Ruby error #{e.}" end else @value end end  |