Class: ActiveText::Base
- Inherits:
-
Object
- Object
- ActiveText::Base
- Defined in:
- lib/active_text/base.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
writeonly
Sets the attribute options.
-
#text ⇒ Object
readonly
Returns the value of attribute text.
-
#variables ⇒ Object
readonly
Returns the value of attribute variables.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #attributes ⇒ Object
-
#initialize(text, options = {}) ⇒ Base
constructor
A new instance of Base.
-
#render ⇒ Object
Used to update the text.
- #update_attributes(args) ⇒ Object
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 |
# File 'lib/active_text/base.rb', line 6 def initialize(text, ={}) @text = text @variables = {} [:eol] ||= '\n' [:context_lines] ||= 3 [:comment] ||= /\/\// [:context] ||= "(^(?:#{[:comment]}\s@.*#{[:eol]}){#{[:context_lines]}})" @options = # instantiate all variables @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]) @variables.merge!({variable_name.to_sym => variable}) 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.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/active_text/base.rb', line 55 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
4 5 6 |
# File 'lib/active_text/base.rb', line 4 def (value) @options = value end |
#text ⇒ Object (readonly)
Returns the value of attribute text.
3 4 5 |
# File 'lib/active_text/base.rb', line 3 def text @text end |
#variables ⇒ Object (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
39 40 41 42 |
# File 'lib/active_text/base.rb', line 39 def [](key) variable = @variables[key] {:name => variable.name, :description => variable.description, :value => variable.value, :kind => variable.kind} end |
#attributes ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/active_text/base.rb', line 44 def attributes h = {} @variables.each do |key, variable| h.merge!({key => variable.value}) end h end |
#render ⇒ Object
Used to update the text
32 33 34 35 36 37 |
# File 'lib/active_text/base.rb', line 32 def render @variables.each do |key, var| @text.gsub!(/^\$#{key}: .+;/, %Q($#{key}: #{var.value};)) end @text end |
#update_attributes(args) ⇒ Object
25 26 27 28 29 |
# File 'lib/active_text/base.rb', line 25 def update_attributes(args) args.each do |k, v| send "#{k.to_s}=", v end end |