Class: Protein::Pointer
- Inherits:
-
Object
- Object
- Protein::Pointer
- Defined in:
- lib/protein/pointer.rb
Instance Method Summary collapse
- #dump ⇒ Object
-
#initialize(base, base_name = "context", input = nil) ⇒ Pointer
constructor
A new instance of Pointer.
- #inspect_path ⇒ Object
- #load(input) ⇒ Object
- #parse(input) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(base, base_name = "context", input = nil) ⇒ Pointer
Returns a new instance of Pointer.
3 4 5 6 7 8 9 10 |
# File 'lib/protein/pointer.rb', line 3 def initialize(base, base_name = "context", input = nil) @base = base @base_name = base_name if input.is_a?(String) parse(input) end end |
Instance Method Details
#dump ⇒ Object
12 13 14 |
# File 'lib/protein/pointer.rb', line 12 def dump @access_path || raise(ProcessingError, "pointer can't be empty") end |
#inspect_path ⇒ Object
53 54 55 56 57 58 59 60 61 |
# File 'lib/protein/pointer.rb', line 53 def inspect_path @access_values.each_with_index do |access_value, index| if index > 0 accessor_type, accessor_key = @access_path[index - 1] puts "Accessing #{accessor_type} with key #{accessor_key.inspect}" end puts "At #{access_value.inspect}" end end |
#load(input) ⇒ Object
16 17 18 19 20 21 22 23 24 |
# File 'lib/protein/pointer.rb', line 16 def load(input) @access_path = input.map do |type, key| [type.to_sym, key] end traverse_access_path(traverse_values: false) self end |
#parse(input) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/protein/pointer.rb', line 26 def parse(input) unless input =~ Regexp.new("^#{@base_name}") raise PointerError, "access path should start with `#{@base_name}`" end access_path = input.scan(/(\.(\w+))|(\[(\d+)\])|(\[['"](\w+)['"]\])/).map do |match| if (key = match[1]) [:struct, key] elsif (key = match[3]) [:repeated, key.to_i] elsif (key = match[5]) [:map, key] end end if access_path.empty? raise PointerError, "access path should not be empty" end @access_path = access_path traverse_access_path end |
#to_s ⇒ Object
49 50 51 |
# File 'lib/protein/pointer.rb', line 49 def to_s @access_strings.join end |