Sha256: 8b325ad87aad5390880319553ac0c430102b2c0712b8c21c5daf3eb6260b933e

Contents?: true

Size: 1.13 KB

Versions: 7

Compression:

Stored size: 1.13 KB

Contents

module ActiveText
  # These are currently the kinds of metadata that we can fetch
  # This should be configurable in the future (or remove it entirely)
  METADATA = %w(name kind description)

  class Variable
    attr_reader :key, :context, :comment

    def initialize(key, context, comment)
      # What the short name of this variable is
      @key = key

      # Text surrounding the variable, as determined by Base
      @context = context

      # What is considered a comment
      @comment = comment
    end

    # if any of the metadata string methods are called
    # then we return what the value is
    def method_missing(method_name)
      if METADATA.include? method_name.to_s
        content_of(method_name.to_s)
      end
    end

    def value
      @context.each do |string|
        string =~ /^\${1}#{@key}: (.+);/
        return $1 if $1
      end
    end

    def value=(val)
      @context.gsub!(/^\$#{@key}: .+;/, "$#{@key}: #{val};")
    end

    private

    def content_of(metadata)
      @context.each do |string|
        string =~ /^#{@comment} @([\w]+[^\s]) (.+)/
        return $2 if $1 == metadata
      end
    end

  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
active_text-0.0.8 lib/active_text/variable.rb
active_text-0.0.7 lib/active_text/variable.rb
active_text-0.0.6 lib/active_text/variable.rb
active_text-0.0.5 lib/active_text/variable.rb
active_text-0.0.4 lib/active_text/variable.rb
active_text-0.0.3 lib/active_text/variable.rb
active_text-0.0.2 lib/active_text/variable.rb