active_text 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile CHANGED
@@ -25,19 +25,21 @@ You can pass that into a ActiveText:
25
25
 
26
26
  <pre>
27
27
  @at = ActiveText::Base.new(text_you_see_above)
28
- @at.bg_image.name # returns "Background image"
29
- @at.bg_image.kind # returns "file"
30
- @at.bg_image.value # returns %Q("http://someurl.com/bg.jpg") - note that there are double quotes in the string
31
- @at.link_color.kind # returns "color"
32
- @at.link_color.value # returns "#555"
28
+ @at[:bg_image][:name] # "Background image"
29
+ @at[:bg_image][:kind] # "file"
30
+ @at[:bg_image][:value] # %Q("http://someurl.com/bg.jpg") - note that there are double quotes in the string
31
+ @at.bg_image # same as @at[:bg_image][:value]
32
+ @at[:link_color][:kind] # "color"
33
+ @at[:link_color][:value] # "#555"
34
+ @at.link_color # same as @at[:link_color][:value]
33
35
  </pre>
34
36
 
35
37
  You can also update the values:
36
38
 
37
39
  <pre>
38
40
  @at = ActiveText::Base.new(text_you_see_above)
39
- @at.bg_image.value = "'/path/to/image.png'"
40
- @at.apply # returns the text passed into base, with values replaced:
41
+ @at.bg_image = "'/path/to/image.png'"
42
+ @at.render # returns the text passed into base, with values replaced:
41
43
  </pre>
42
44
 
43
45
  <pre>
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.4
1
+ 0.0.5
data/active_text.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{active_text}
8
- s.version = "0.0.4"
8
+ s.version = "0.0.5"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Ramon Tayag"]
12
- s.date = %q{2011-03-18}
12
+ s.date = %q{2011-04-15}
13
13
  s.description = %q{Aims to be able to read and replace "variables" in text in an active record manner. I don't claim that it behaves exactly like ActiveRecord - that is a much more complex beast than this will ever be.}
14
14
  s.email = %q{ramon@tayag.net}
15
15
  s.extra_rdoc_files = [
@@ -13,51 +13,57 @@ module ActiveText
13
13
  options[:context] ||= "(^(?:#{options[:comment]}\s@.*#{options[:eol]}){#{options[:context_lines]}})"
14
14
  @options = options
15
15
 
16
- scan
16
+ # instantiate all variables
17
+ @text.scan(/^\${1}(.+): .+;/).flatten.each do |variable_name|
18
+ if has_context?(variable_name)
19
+ variable = ActiveText::Variable.new(variable_name, context_of(variable_name), @options[:comment])
20
+ @variables.merge!({variable_name.to_sym => variable})
21
+ end
22
+ end
17
23
  end
18
24
 
19
25
  def update_attributes(args)
20
26
  args.each do |k, v|
21
- send(k) # Instantiate it, so it will exist in @variables
22
- @variables[k].value = v unless @variables[k].nil? || v.nil?
27
+ send "#{k.to_s}=", v
23
28
  end
24
29
  end
25
30
 
26
31
  # Used to update the text
27
- def apply
32
+ def render
28
33
  @variables.each do |key, var|
29
34
  @text.gsub!(/^\$#{key}: .+;/, %Q($#{key}: #{var.value};))
30
35
  end
31
36
  @text
32
37
  end
33
38
 
34
- def attributes
35
- @variables
39
+ def [](key)
40
+ variable = @variables[key]
41
+ {:name => variable.name, :description => variable.description, :value => variable.value, :kind => variable.kind}
36
42
  end
37
43
 
38
- protected
39
-
40
- # scans though the text and instantiates all the variables in @variables
41
- def scan
42
- @text.each do |string|
43
- string =~ /^\${1}([a-zA-Z0-9_]+):.+;/
44
- @variables[$1] = ActiveText::Variable.new($1, context_of($1), @options[:comment]) if $1
44
+ def attributes
45
+ h = {}
46
+ @variables.each do |key, variable|
47
+ h.merge!({key => variable.value})
45
48
  end
49
+ h
46
50
  end
47
51
 
48
- # Whenever a variable is requested for, it falls into this.
49
- def method_missing(method_name)
50
- if method_name.to_s =~ /[\w]+/
51
- context = context_of(method_name)
52
+ protected
52
53
 
53
- # If there's no context (no variable with the proper
54
- # commented metadata), then it should not be accessible
55
- # and it won't be accessible
56
- if context.nil? || context == ""
57
- nil
54
+ # Whenever a variable is requested for, it falls into this.
55
+ def method_missing(method_name, *args, &block)
56
+ attr_key = method_name.to_s.sub(/\=$/, '').to_sym
57
+ variable = @variables[attr_key]
58
+ if variable
59
+ if method_name.to_s =~ /\=$/
60
+ value = args[0]
61
+ variable.value = value unless value.nil?
58
62
  else
59
- @variables[method_name] ||= ActiveText::Variable.new(method_name, context, @options[:comment])
63
+ variable.value
60
64
  end
65
+ else
66
+ raise NoMethodError, "Variable does not exist"
61
67
  end
62
68
  end
63
69
 
@@ -70,5 +76,10 @@ module ActiveText
70
76
  before, match = $1, $2
71
77
  "#{before}#{match}"
72
78
  end
79
+
80
+ def has_context?(s)
81
+ context = context_of(s)
82
+ !(context.nil? || context == "")
83
+ end
73
84
  end
74
85
  end
@@ -17,15 +17,15 @@ $mbc: #555;}
17
17
  it "should be able to set variables and return them properly" do
18
18
  @atb = ActiveText::Base.new(@text)
19
19
 
20
- @atb.mbc.name.should == "Masthead BG Color"
21
- @atb.mbc.kind.should == "color"
22
- @atb.mbc.description.should == "Background color."
23
- @atb.mbc.value.should == "#555"
24
-
25
- @atb.mbc2.name.should == "Masthead Background Image"
26
- @atb.mbc2.kind.should == "file"
27
- @atb.mbc2.description.should == "Background image."
28
- @atb.mbc2.value.should == %Q("http://someurl.com/image.jpg")
20
+ @atb[:mbc][:name].should == "Masthead BG Color"
21
+ @atb[:mbc][:kind].should == "color"
22
+ @atb[:mbc][:description].should == "Background color."
23
+ @atb[:mbc][:value].should == "#555"
24
+
25
+ @atb[:mbc2][:name].should == "Masthead Background Image"
26
+ @atb[:mbc2][:kind].should == "file"
27
+ @atb[:mbc2][:description].should == "Background image."
28
+ @atb[:mbc2][:value].should == %Q("http://someurl.com/image.jpg")
29
29
  end
30
30
  end
31
31
 
@@ -36,16 +36,16 @@ $mbc: #555;}
36
36
  // @description Background image.
37
37
  $mbc2: "http://someurl.com/image.jpg";}
38
38
  @s = ActiveText::Base.new(text)
39
- @s.mbc2.value = %Q("Another URL")
40
- @s.mbc2.value.should == %Q("Another URL")
39
+ @s.mbc2 = %Q("Another URL")
40
+ @s.mbc2.should == %Q("Another URL")
41
41
 
42
- rendered_text = @s.apply
42
+ rendered_text = @s.render
43
43
  rendered_text.should_not match(/http:\/\/someurl\.com\/image\.jpg/)
44
44
  rendered_text.should match(/\$mbc2: "Another URL";/)
45
45
 
46
- @s.mbc2.value = %Q("Some third URL")
47
- @s.mbc2.value.should == %Q("Some third URL")
48
- rendered_text = @s.apply
46
+ @s.mbc2 = %Q("Some third URL")
47
+ @s.mbc2.should == %Q("Some third URL")
48
+ rendered_text = @s.render
49
49
  rendered_text.should_not match(/\$mbc2: "Another URL";/)
50
50
  rendered_text.should match(/\$mbc2: "Some third URL";/)
51
51
  end
@@ -60,20 +60,43 @@ $mbc2: "http://someurl.com/image.jpg";}
60
60
  // @description Background image.
61
61
  $mbc2: "Another URL";}
62
62
  @s = ActiveText::Base.new(text_old)
63
- @s.mbc2.value = %Q("Another URL")
64
- @s.apply.should == text_new
63
+ @s.mbc2 = %Q("Another URL")
64
+ @s.render.should == text_new
65
65
  end
66
66
  end
67
67
 
68
68
  describe "when there's no metadata" do
69
+ before do
70
+ @text = %{// @name Masthead Background Image
71
+ // @kind file
72
+ // @description Background image.
73
+ $mbc2: "http://someurl.com/image.jpg";
74
+ $mbc: #555;}
75
+ @s = ActiveText::Base.new(@text)
76
+ end
77
+
69
78
  it "should not allow reading of data" do
70
- text = %{// @name Masthead Background Image
79
+ lambda {@s.mbc}.should raise_error(NoMethodError)
80
+ end
81
+ end
82
+
83
+ describe ".attributes" do
84
+ it ".attributes should return an empty hash when there are no valid variables" do
85
+ @text = %{
86
+ $mbc2: "http://someurl.com/image.jpg";
87
+ $mbc: #555;}
88
+ @s = ActiveText::Base.new(@text)
89
+ @s.attributes.should == {}
90
+ end
91
+
92
+ it "should only return the valid variables" do
93
+ @text = %{// @name Masthead Background Image
71
94
  // @kind file
72
95
  // @description Background image.
73
96
  $mbc2: "http://someurl.com/image.jpg";
74
97
  $mbc: #555;}
75
- @s = ActiveText::Base.new(text)
76
- @s.mbc.should be_nil
98
+ @s = ActiveText::Base.new(@text)
99
+ @s.attributes.should == {:mbc2 => %Q("http://someurl.com/image.jpg")}
77
100
  end
78
101
  end
79
102
 
@@ -89,21 +112,29 @@ $mbc2: "http://someurl.com/image.jpg";
89
112
  $mbc: #555;}
90
113
  @s = ActiveText::Base.new(text)
91
114
  @s.update_attributes(:mbc2 => %Q("Another URL"), :mbc => nil)
92
- @s.mbc2.value.should == %Q("Another URL")
93
- @s.mbc.value.should == "#555"
115
+ @s.mbc2.should == %Q("Another URL")
116
+ @s.mbc.should == "#555"
94
117
  end
95
118
 
96
- it "should keep a record of what attributes are in it" do
97
- @text = %{// @name Masthead Background Image
119
+ it "should have setter method for variables" do
120
+ text = %{// @name Masthead Background Image
98
121
  // @kind file
99
122
  // @description Background image.
100
123
  $mbc2: "http://someurl.com/image.jpg";
124
+ }
125
+ @s = ActiveText::Base.new(text)
126
+ @s.mbc2 = "44"
127
+ @s.mbc2.should == "44"
128
+ end
101
129
 
102
- // @name Masthead BG Color
103
- // @kind color
104
- // @description Background color.
105
- $mbc: #555;}
106
- @at = ActiveText::Base.new(@text)
107
- @at.attributes.size.should == 2
130
+ it "should not allow to set to nil" do
131
+ text = %{// @name Masthead Background Image
132
+ // @kind file
133
+ // @description Background image.
134
+ $mbc2: "http://someurl.com/image.jpg";
135
+ }
136
+ @s = ActiveText::Base.new(text)
137
+ @s.mbc2 = nil
138
+ @s.mbc2.should == %Q("http://someurl.com/image.jpg")
108
139
  end
109
140
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_text
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 4
10
- version: 0.0.4
9
+ - 5
10
+ version: 0.0.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ramon Tayag
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-03-18 00:00:00 +08:00
18
+ date: 2011-04-15 00:00:00 +08:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency