active_text 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm ree-1.8.7-2011.03@active_text
data/Gemfile CHANGED
@@ -6,8 +6,9 @@ source "http://rubygems.org"
6
6
  # Add dependencies to develop your gem here.
7
7
  # Include everything needed to run rake, tests, features, etc.
8
8
  group :development do
9
- gem "rspec", ">= 2.3.0"
9
+ gem "rspec", "2.3.0"
10
10
  gem "bundler", "~> 1.0.0"
11
11
  gem "jeweler", "~> 1.5.2"
12
12
  gem "rcov", ">= 0"
13
+ gem "autotest"
13
14
  end
data/Gemfile.lock CHANGED
@@ -1,6 +1,9 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
+ ZenTest (4.5.0)
5
+ autotest (4.4.6)
6
+ ZenTest (>= 4.4.1)
4
7
  diff-lcs (1.1.2)
5
8
  git (1.2.5)
6
9
  jeweler (1.5.2)
@@ -22,7 +25,8 @@ PLATFORMS
22
25
  ruby
23
26
 
24
27
  DEPENDENCIES
28
+ autotest
25
29
  bundler (~> 1.0.0)
26
30
  jeweler (~> 1.5.2)
27
31
  rcov
28
- rspec (>= 2.3.0)
32
+ rspec (= 2.3.0)
data/README.textile CHANGED
@@ -4,9 +4,69 @@ ActiveText is to text, as ActiveRecord is to the database. ActiveText is able to
4
4
 
5
5
  h2. Why would you want to do this?
6
6
 
7
- Ramon Tayag was looking for a way to edit SASS files in terms of variables. He wanted end-users who didn't know anything about SASS to be able to edit variables to change colors, images, etc, in the stylesheet. To acheive that, he needed the ability to parse through text files and easy replace variables in them.
7
+ Ramon Tayag was looking for a way to edit SASS files in terms of variables. He wanted end-users who didn't know anything about SASS to be able to edit variables to change colors, images, etc, in the stylesheet. To achieve that, he needed the ability to parse through text files and easy replace variables in them.
8
8
 
9
- == Contributing to active_text
9
+ h2. How does it work?
10
+
11
+ Given this text:
12
+
13
+ <pre>// @name Background image
14
+ // @kind file
15
+ // @description Background image. Tiled left to right, up to down, so the file has to be repeatable.
16
+ $bg_image: "http://someurl.com/bg.jpg"
17
+
18
+ // @name Link Color
19
+ // @kind color
20
+ // @description Color of the link text
21
+ $link_color: #555;
22
+ </pre>
23
+
24
+ You can pass that into a ActiveText:
25
+
26
+ <pre>
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"
33
+ </pre>
34
+
35
+ You can also update the values:
36
+
37
+ <pre>
38
+ @at = ActiveText::Base.new(text_you_see_above)
39
+ @at.bg_image.value = "'/path/to/image.png'"
40
+ @at.render # returns the text passed into base, with values replaced:
41
+ </pre>
42
+
43
+ <pre>
44
+ // @name Background image
45
+ // @kind file
46
+ // @description Background image. Tiled left to right, up to down, so the file has to be repeatable.
47
+ $bg_image: '/path/to/image.png'
48
+
49
+ // @name Link Color
50
+ // @kind color
51
+ // @description Color of the link text
52
+ $link_color: #555;
53
+ </pre>
54
+
55
+ You can also mass update: <pre>@at.update_attributes(:bg_image => %Q("http://anotherurl.com/img.jpg"), :link_color => "green")</pre>
56
+
57
+ h2. Gotchas
58
+
59
+ Right now, only the following metadata will work:
60
+
61
+ * name
62
+ * kind
63
+ * color
64
+
65
+ And only the following format to pickup values for variables will work:
66
+
67
+ <code>$variable_name: this is the value;</code>
68
+
69
+ h2. Contributing to active_text
10
70
 
11
71
  * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
12
72
  * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
@@ -16,7 +76,6 @@ Ramon Tayag was looking for a way to edit SASS files in terms of variables. He w
16
76
  * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
17
77
  * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
18
78
 
19
- == Copyright
79
+ h2. Copyright
20
80
 
21
- Copyright (c) 2011 Ramon Tayag. See LICENSE.txt for
22
- further details.
81
+ Copyright (c) 2011 Ramon Tayag. See LICENSE.txt for further details.
data/Rakefile CHANGED
@@ -8,15 +8,15 @@ rescue Bundler::BundlerError => e
8
8
  exit e.status_code
9
9
  end
10
10
  require 'rake'
11
-
12
11
  require 'jeweler'
12
+
13
13
  Jeweler::Tasks.new do |gem|
14
14
  # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
15
  gem.name = "active_text"
16
- gem.homepage = "http://github.com/ramon.tayag/active_text"
16
+ gem.homepage = "http://github.com/ramontayag/active_text"
17
17
  gem.license = "MIT"
18
- gem.summary = %Q{ActiveText : Text as ActiveRecord : Database. Sort of.}
19
- gem.description = %Q{ActiveText aims to be able to read and replace "variables" in text files. This won't be exactly feel like ActiveRecord, but similar where it seems it should.}
18
+ gem.summary = %Q{ActiveText : Text as ActiveRecord : Records. Sort of.}
19
+ gem.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.}
20
20
  gem.email = "ramon@tayag.net"
21
21
  gem.authors = ["Ramon Tayag"]
22
22
  # Include your dependencies below. Runtime dependencies are required when using your gem,
@@ -26,28 +26,25 @@ Jeweler::Tasks.new do |gem|
26
26
  end
27
27
  Jeweler::RubygemsDotOrgTasks.new
28
28
 
29
- require 'rake/testtask'
30
- Rake::TestTask.new(:test) do |test|
31
- test.libs << 'lib' << 'test'
32
- test.pattern = 'test/**/test_*.rb'
33
- test.verbose = true
29
+ require 'rspec/core'
30
+ require 'rspec/core/rake_task'
31
+ RSpec::Core::RakeTask.new(:spec) do |spec|
32
+ spec.pattern = FileList['spec/**/*_spec.rb']
34
33
  end
35
34
 
36
- require 'rcov/rcovtask'
37
- Rcov::RcovTask.new do |test|
38
- test.libs << 'test'
39
- test.pattern = 'test/**/test_*.rb'
40
- test.verbose = true
35
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
36
+ spec.pattern = 'spec/**/*_spec.rb'
37
+ spec.rcov = true
41
38
  end
42
39
 
43
- task :default => :test
40
+ task :default => :spec
44
41
 
45
42
  require 'rake/rdoctask'
46
43
  Rake::RDocTask.new do |rdoc|
47
44
  version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
45
 
49
46
  rdoc.rdoc_dir = 'rdoc'
50
- rdoc.title = "active_text #{version}"
47
+ rdoc.title = "at #{version}"
51
48
  rdoc.rdoc_files.include('README*')
52
49
  rdoc.rdoc_files.include('lib/**/*.rb')
53
50
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
data/active_text.gemspec CHANGED
@@ -5,12 +5,12 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{active_text}
8
- s.version = "0.0.1"
8
+ s.version = "0.0.2"
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
12
  s.date = %q{2011-03-18}
13
- s.description = %q{ActiveText aims to be able to read and replace "variables" in text files. This won't be exactly feel like ActiveRecord, but similar where it seems it should.}
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 = [
16
16
  "LICENSE.txt",
@@ -18,6 +18,8 @@ Gem::Specification.new do |s|
18
18
  ]
19
19
  s.files = [
20
20
  ".document",
21
+ ".rspec",
22
+ ".rvmrc",
21
23
  "Gemfile",
22
24
  "Gemfile.lock",
23
25
  "LICENSE.txt",
@@ -25,33 +27,44 @@ Gem::Specification.new do |s|
25
27
  "Rakefile",
26
28
  "VERSION",
27
29
  "active_text.gemspec",
28
- "lib/active_text.rb"
30
+ "lib/active_text.rb",
31
+ "lib/active_text/base.rb",
32
+ "lib/active_text/variable.rb",
33
+ "spec/active_text_spec.rb",
34
+ "spec/spec_helper.rb"
29
35
  ]
30
- s.homepage = %q{http://github.com/ramon.tayag/active_text}
36
+ s.homepage = %q{http://github.com/ramontayag/active_text}
31
37
  s.licenses = ["MIT"]
32
38
  s.require_paths = ["lib"]
33
39
  s.rubygems_version = %q{1.6.2}
34
- s.summary = %q{ActiveText : Text as ActiveRecord : Database. Sort of.}
40
+ s.summary = %q{ActiveText : Text as ActiveRecord : Records. Sort of.}
41
+ s.test_files = [
42
+ "spec/active_text_spec.rb",
43
+ "spec/spec_helper.rb"
44
+ ]
35
45
 
36
46
  if s.respond_to? :specification_version then
37
47
  s.specification_version = 3
38
48
 
39
49
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
40
- s.add_development_dependency(%q<rspec>, [">= 2.3.0"])
50
+ s.add_development_dependency(%q<rspec>, ["= 2.3.0"])
41
51
  s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
42
52
  s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
43
53
  s.add_development_dependency(%q<rcov>, [">= 0"])
54
+ s.add_development_dependency(%q<autotest>, [">= 0"])
44
55
  else
45
- s.add_dependency(%q<rspec>, [">= 2.3.0"])
56
+ s.add_dependency(%q<rspec>, ["= 2.3.0"])
46
57
  s.add_dependency(%q<bundler>, ["~> 1.0.0"])
47
58
  s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
48
59
  s.add_dependency(%q<rcov>, [">= 0"])
60
+ s.add_dependency(%q<autotest>, [">= 0"])
49
61
  end
50
62
  else
51
- s.add_dependency(%q<rspec>, [">= 2.3.0"])
63
+ s.add_dependency(%q<rspec>, ["= 2.3.0"])
52
64
  s.add_dependency(%q<bundler>, ["~> 1.0.0"])
53
65
  s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
54
66
  s.add_dependency(%q<rcov>, [">= 0"])
67
+ s.add_dependency(%q<autotest>, [">= 0"])
55
68
  end
56
69
  end
57
70
 
@@ -0,0 +1,60 @@
1
+ module ActiveText
2
+ class Base
3
+ attr_reader :text, :variables
4
+ attr_writer :options
5
+
6
+ def initialize(text, options={})
7
+ @text = text
8
+ @variables = {}
9
+
10
+ options[:eol] ||= '\n'
11
+ options[:context_lines] ||= 3
12
+ options[:comment] ||= /\/\//
13
+ options[:context] ||= "(^(?:#{options[:comment]}\s@.*#{options[:eol]}){#{options[:context_lines]}})"
14
+ @options = options
15
+ end
16
+
17
+ def update_attributes(args)
18
+ args.each do |k, v|
19
+ send(k) # Instantiate it, so it will exist in @variables
20
+ @variables[k].value = v unless @variables[k].nil? || v.nil?
21
+ end
22
+ end
23
+
24
+ # Used to update the text
25
+ def render
26
+ @variables.each do |key, var|
27
+ @text.gsub!(/^\$#{key}: .+;/, %Q($#{key}: #{var.value};))
28
+ end
29
+ @text
30
+ end
31
+
32
+ protected
33
+
34
+ # Whenever a variable is requested for, it falls into this.
35
+ def method_missing(method_name)
36
+ if method_name.to_s =~ /[\w]+/
37
+ context = context_of(method_name)
38
+
39
+ # If there's no context (no variable with the proper
40
+ # commented metadata), then it should not be accessible
41
+ # and it won't be accessible
42
+ if context.nil? || context == ""
43
+ nil
44
+ else
45
+ @variables[method_name] ||= ActiveText::Variable.new(method_name, context, @options[:comment])
46
+ end
47
+ end
48
+ end
49
+
50
+ # Works this way: http://rubular.com/r/jWSYvfVrjj
51
+ # From http://rubular.com/r/jWSYvfVrjj
52
+ # Derived from http://stackoverflow.com/questions/2760759/ruby-equivalent-to-grep-c-5-to-get-context-of-lines-around-the-match
53
+ def context_of(s)
54
+ regexp = /.*\${1}#{s}:.*;[#{@options[:eol]}]*/
55
+ @text =~ /^#{@options[:context]}(#{regexp})/
56
+ before, match = $1, $2
57
+ "#{before}#{match}"
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,49 @@
1
+ module ActiveText
2
+ # These are currently the kinds of metadata that we can fetch
3
+ # This should be configurable in the future (or remove it entirely)
4
+ METADATA = %w(name kind description)
5
+
6
+ class Variable
7
+ attr_reader :key, :context, :comment
8
+
9
+ def initialize(key, context, comment)
10
+ # What the short name of this variable is
11
+ @key = key
12
+
13
+ # Text surrounding the variable, as determined by Base
14
+ @context = context
15
+
16
+ # What is considered a comment
17
+ @comment = comment
18
+ end
19
+
20
+ # if any of the metadata string methods are called
21
+ # then we return what the value is
22
+ def method_missing(method_name)
23
+ if METADATA.include? method_name.to_s
24
+ content_of(method_name.to_s)
25
+ end
26
+ end
27
+
28
+ def value
29
+ @context.each do |string|
30
+ string =~ /^\${1}#{@key}: (.+);/
31
+ return $1 if $1
32
+ end
33
+ end
34
+
35
+ def value=(val)
36
+ @context.gsub!(/^\$#{@key}: .+;/, "$#{@key}: #{val};")
37
+ end
38
+
39
+ private
40
+
41
+ def content_of(metadata)
42
+ @context.each do |string|
43
+ string =~ /^#{@comment} @([\w]+[^\s]) (.+)/
44
+ return $2 if $1 == metadata
45
+ end
46
+ end
47
+
48
+ end
49
+ end
data/lib/active_text.rb CHANGED
@@ -0,0 +1,2 @@
1
+ require 'active_text/base'
2
+ require 'active_text/variable'
@@ -0,0 +1,96 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "ActiveText" do
4
+ describe "given properly formatted input" do
5
+ before do
6
+ @text = %{// @name Masthead Background Image
7
+ // @kind file
8
+ // @description Background image.
9
+ $mbc2: "http://someurl.com/image.jpg";
10
+
11
+ // @name Masthead BG Color
12
+ // @kind color
13
+ // @description Background color.
14
+ $mbc: #555;}
15
+ end
16
+
17
+ it "should be able to set variables and return them properly" do
18
+ @atb = ActiveText::Base.new(@text)
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")
29
+ end
30
+ end
31
+
32
+ describe "when assigning values" do
33
+ it "should update its values" do
34
+ text = %{// @name Masthead Background Image
35
+ // @kind file
36
+ // @description Background image.
37
+ $mbc2: "http://someurl.com/image.jpg";}
38
+ @s = ActiveText::Base.new(text)
39
+ @s.mbc2.value = %Q("Another URL")
40
+ @s.mbc2.value.should == %Q("Another URL")
41
+
42
+ rendered_text = @s.render
43
+ rendered_text.should_not match(/http:\/\/someurl\.com\/image\.jpg/)
44
+ rendered_text.should match(/\$mbc2: "Another URL";/)
45
+
46
+ @s.mbc2.value = %Q("Some third URL")
47
+ @s.mbc2.value.should == %Q("Some third URL")
48
+ rendered_text = @s.render
49
+ rendered_text.should_not match(/\$mbc2: "Another URL";/)
50
+ rendered_text.should match(/\$mbc2: "Some third URL";/)
51
+ end
52
+
53
+ it "should render the correct values" do
54
+ text_old = %{// @name Masthead Background Image
55
+ // @kind file
56
+ // @description Background image.
57
+ $mbc2: "http://someurl.com/image.jpg";}
58
+ text_new = %{// @name Masthead Background Image
59
+ // @kind file
60
+ // @description Background image.
61
+ $mbc2: "Another URL";}
62
+ @s = ActiveText::Base.new(text_old)
63
+ @s.mbc2.value = %Q("Another URL")
64
+ @s.render.should == text_new
65
+ end
66
+ end
67
+
68
+ describe "when there's no metadata" do
69
+ it "should not allow reading of data" 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
+ @s.mbc.should be_nil
77
+ end
78
+ end
79
+
80
+ it "should allow mass assignment" do
81
+ text = %{// @name Masthead Background Image
82
+ // @kind file
83
+ // @description Background image.
84
+ $mbc2: "http://someurl.com/image.jpg";
85
+
86
+ // @name Masthead BG Color
87
+ // @kind color
88
+ // @description Background color.
89
+ $mbc: #555;}
90
+ @s = ActiveText::Base.new(text)
91
+ @s.update_attributes(:mbc2 => %Q("Another URL"), :mbc => nil)
92
+ @s.mbc2.value.should == %Q("Another URL")
93
+ @s.mbc.value.should == "#555"
94
+ end
95
+
96
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'active_text'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ 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: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ramon Tayag
@@ -23,7 +23,7 @@ dependencies:
23
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
24
  none: false
25
25
  requirements:
26
- - - ">="
26
+ - - "="
27
27
  - !ruby/object:Gem::Version
28
28
  hash: 3
29
29
  segments:
@@ -80,7 +80,21 @@ dependencies:
80
80
  name: rcov
81
81
  version_requirements: *id004
82
82
  prerelease: false
83
- description: ActiveText aims to be able to read and replace "variables" in text files. This won't be exactly feel like ActiveRecord, but similar where it seems it should.
83
+ - !ruby/object:Gem::Dependency
84
+ type: :development
85
+ requirement: &id005 !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ hash: 3
91
+ segments:
92
+ - 0
93
+ version: "0"
94
+ name: autotest
95
+ version_requirements: *id005
96
+ prerelease: false
97
+ description: 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.
84
98
  email: ramon@tayag.net
85
99
  executables: []
86
100
 
@@ -91,6 +105,8 @@ extra_rdoc_files:
91
105
  - README.textile
92
106
  files:
93
107
  - .document
108
+ - .rspec
109
+ - .rvmrc
94
110
  - Gemfile
95
111
  - Gemfile.lock
96
112
  - LICENSE.txt
@@ -99,8 +115,12 @@ files:
99
115
  - VERSION
100
116
  - active_text.gemspec
101
117
  - lib/active_text.rb
118
+ - lib/active_text/base.rb
119
+ - lib/active_text/variable.rb
120
+ - spec/active_text_spec.rb
121
+ - spec/spec_helper.rb
102
122
  has_rdoc: true
103
- homepage: http://github.com/ramon.tayag/active_text
123
+ homepage: http://github.com/ramontayag/active_text
104
124
  licenses:
105
125
  - MIT
106
126
  post_install_message:
@@ -132,6 +152,7 @@ rubyforge_project:
132
152
  rubygems_version: 1.6.2
133
153
  signing_key:
134
154
  specification_version: 3
135
- summary: "ActiveText : Text as ActiveRecord : Database. Sort of."
136
- test_files: []
137
-
155
+ summary: "ActiveText : Text as ActiveRecord : Records. Sort of."
156
+ test_files:
157
+ - spec/active_text_spec.rb
158
+ - spec/spec_helper.rb