Class: ActiveText::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/active_text/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, options = {}) ⇒ Base

Returns a new instance of Base.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/active_text/base.rb', line 6

def initialize(text, options={})
  @text = text
  @variables = {}

  options[:eol] ||= '\n'
  options[:context_lines] ||= 3
  options[:comment] ||= /\/\//
  options[:context] ||= "(^(?:#{options[:comment]}\s@.*#{options[:eol]}){#{options[:context_lines]}})"
  @options = options

  # instantiate all variables
  if @text
    @text.scan(/^\${1}(.+): .+;/).flatten.each do |variable_name|
      if has_context?(variable_name)
        variable = ActiveText::Variable.new(variable_name, context_of(variable_name), @options[:comment])
        #puts "Instantiating variable: :#{variable_name} => #{variable.value}"
        @variables.merge!({variable_name.to_sym => variable})
      end
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object (protected)

Whenever a variable is requested for, it falls into this.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/active_text/base.rb', line 58

def method_missing(method_name, *args, &block)
  attr_key = method_name.to_s.sub(/\=$/, '').to_sym
  variable = @variables[attr_key]
  if variable
    if method_name.to_s =~ /\=$/
      value = args[0]
      variable.value = value unless value.nil?
    else
      variable.value
    end
  else
    raise NoMethodError, "Variable does not exist"
  end
end

Instance Attribute Details

#options=(value) ⇒ Object (writeonly)

Sets the attribute options

Parameters:

  • value

    the value to set the attribute options to.



4
5
6
# File 'lib/active_text/base.rb', line 4

def options=(value)
  @options = value
end

#textObject (readonly)

Returns the value of attribute text.



3
4
5
# File 'lib/active_text/base.rb', line 3

def text
  @text
end

#variablesObject (readonly)

Returns the value of attribute variables.



3
4
5
# File 'lib/active_text/base.rb', line 3

def variables
  @variables
end

Instance Method Details

#[](key) ⇒ Object



42
43
44
45
# File 'lib/active_text/base.rb', line 42

def [](key)
  variable = @variables[key]
  {:name => variable.name, :description => variable.description, :value => variable.value, :kind => variable.kind}
end

#applyObject

Used to update the text



35
36
37
38
39
40
# File 'lib/active_text/base.rb', line 35

def apply
  @variables.each do |key, var|
    @text.gsub!(/^\$#{key}: .+;/, %Q($#{key}: #{var.value};))
  end
  @text
end

#attributesObject



47
48
49
50
51
52
53
# File 'lib/active_text/base.rb', line 47

def attributes
  h = {}
  @variables.each do |key, variable|
    h.merge!({key => variable.value})
  end
  h
end

#update_attributes(args) ⇒ Object



28
29
30
31
32
# File 'lib/active_text/base.rb', line 28

def update_attributes(args)
  args.each do |k, v|
    send "#{k.to_s}=", v
  end
end