Sha256: f620763891ced5873c04886ca4074741e49e8f0f01d495b200ad563c016f14ea

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 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[/(\w*\.\w*)/, 1].to_s.split('.')
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

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