Sha256: fbaae973ff6394a72f874d30d187c461a78ca1bcf3db473fc337ff34524a3e72

Contents?: true

Size: 1.11 KB

Versions: 1

Compression:

Stored size: 1.11 KB

Contents

module Parsable
  class Parser

    attr_accessor :original_string, :strings

    def initialize args={}
      @original_string = args.fetch(:string).to_s
      @strings         = all_captures(@original_string)
    end

    def parse
      strings.uniq.collect do |string|
        function, object, attribute = capture(string)

        Parsable::ParsedItem.new(\
          :original  => string, 
          :function  => function,
          :object    => object,
          :attribute => attribute
        )
      end
    end

    private

    def capture string
      [capture_function_method(string), capture_object(string), capture_attribute(string)]
    end

    def all_captures string
      string.to_s.scan(/\{\{(\w*\(?\w*\.?\w*\)?)\}\}/).flatten
    end

    def capture_function_method string
      match = string.match(/(.*)\(.*\)/)
      match[1] if match
    end

    def capture_attribute string
      object_attribute(string).last
    end

    def capture_object string
      object_attribute(string).first
    end

    def object_attribute string
      string.match[/(\w*\.\w*)/, 1].to_s.split('.')
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
parsable-0.1.3 lib/parsable/parser.rb