macrow 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7c9a2382b09ec40cb151e6216453fb9520fb4f1e
4
+ data.tar.gz: ec7e71ef918f6091d1e8d90a0b1973cec5d11e5b
5
+ SHA512:
6
+ metadata.gz: 46422e4b8361f7239c22ec4c0984c09b883335d1d8e22f0a8c3c5382b1e36decd12668542d9d74b9397121c2720791c1f83044c4f44409a81c1afc17a227cb02
7
+ data.tar.gz: 0cfe97c44b45f17487ea1f0b3b2d497158619dabeb123918579dcf9573d859ec1549da9d6be1b1ed3c12f8fc21e0e6bf53d56e0999ab32261cec3a58ac5121b4
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in macrow.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Izumiya Keisuke
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,83 @@
1
+ # Macrow
2
+ This gem provide DSL for define rules of text replacing. It makes the code more readable and helps avoinding fat model.
3
+
4
+ ## Installation
5
+
6
+ Add this line to your application's Gemfile:
7
+
8
+ ```ruby
9
+ gem 'macrow'
10
+ ```
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install macrow
19
+
20
+ ## Usage
21
+
22
+ ### Basic Usage
23
+ ```ruby
24
+ class HogeMacro < Macrow
25
+ rules do
26
+ # this rule means '${something}' -> 'trouble'
27
+ rule 'something' do
28
+ 'trouble'
29
+ end
30
+
31
+ # you can access an object
32
+ rule 'length' do |object|
33
+ object.length
34
+ end
35
+ end
36
+ end
37
+
38
+ macro = HogeMacro.new
39
+
40
+ macro.apply_all_rules("${something} happened")
41
+ # => "trouble happened"
42
+
43
+ macro.apply("${something} happened") # you can use short alias
44
+ # => "trouble happened"
45
+
46
+ array = [1, 2]
47
+ macro.apply("object length is ${length}", array) # you can pass an object
48
+ # => "object length is ${length}"
49
+
50
+ ```
51
+
52
+ ### Custom Usage
53
+ you can change syntax like ...
54
+
55
+ ```ruby
56
+ class HogeMacro < Macrow
57
+ macro_prefix '{{'
58
+ macro_suffix '}}'
59
+
60
+ rules do
61
+ # this rule means '{{something}}' -> 'trouble'
62
+ rule 'something' do
63
+ 'trouble'
64
+ end
65
+ end
66
+ end
67
+ ```
68
+
69
+ ## Development
70
+
71
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
72
+
73
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
74
+
75
+ ## Contributing
76
+
77
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/macrow.
78
+
79
+
80
+ ## License
81
+
82
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
83
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "macrow"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,76 @@
1
+ require 'macrow/version'
2
+ require 'macrow/error'
3
+
4
+ class Macrow
5
+ class << self
6
+ def inherited(child)
7
+ child.instance_variable_set(:@rule_words, rule_words.dup)
8
+ end
9
+
10
+ def rule_words
11
+ @rule_words ||= []
12
+ end
13
+
14
+ def rules
15
+ yield
16
+ end
17
+
18
+ def macro_prefix(prefix)
19
+ @macro_prefix = prefix
20
+ end
21
+
22
+ def macro_suffix(suffix)
23
+ @macro_suffix = suffix
24
+ end
25
+
26
+ def rule(keyword, &block)
27
+ rule_words << keyword
28
+
29
+ define_method "apply_rule_for_#{keyword}!" do |str, object|
30
+ if str.include? replace_string(keyword)
31
+ begin
32
+ str.gsub!(replace_string(keyword), block.call(object).to_s)
33
+ str
34
+ rescue NoMethodError => e
35
+ fail Macrow::ReplaceError, "NoMethodError is raised on applying rule: '#{keyword}'. Please check your rule. Error message is '#{e.message}'"
36
+ end
37
+ end
38
+ end
39
+ end
40
+
41
+ def replace_string(keyword)
42
+ "#{_macro_prefix}#{keyword}#{_macro_suffix}"
43
+ end
44
+
45
+ private
46
+
47
+ def _macro_prefix
48
+ @macro_prefix ||= '${'
49
+ end
50
+
51
+ def _macro_suffix
52
+ @macro_suffix ||= '}'
53
+ end
54
+ end
55
+
56
+ def apply_all_rules(str, object = nil)
57
+ dup_str = str.dup
58
+
59
+ self.class.rule_words.each do |rule_word|
60
+ apply_rule!(rule_word, dup_str, object)
61
+ end
62
+
63
+ dup_str
64
+ end
65
+ alias_method :apply, :apply_all_rules
66
+
67
+ def apply_rule!(rule, str, object)
68
+ __send__("apply_rule_for_#{rule}!", str, object)
69
+ end
70
+
71
+ private
72
+
73
+ def replace_string(keyword)
74
+ self.class.replace_string(keyword)
75
+ end
76
+ end
@@ -0,0 +1,3 @@
1
+ class Macrow
2
+ class ReplaceError < StandardError;end
3
+ end
@@ -0,0 +1,3 @@
1
+ class Macrow
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'macrow/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "macrow"
8
+ spec.version = Macrow::VERSION
9
+ spec.authors = ["Izumiya Keisuke"]
10
+ spec.email = ["ksk.i.530@gmail.com"]
11
+
12
+ spec.summary = %q{Small helper for text replacing that using DSL}
13
+ spec.description = %q{This gem provide DSL for define rules of text replacing. It makes the code more readable and helps avoinding fat model.}
14
+ spec.homepage = "https://github.com/syguer/macrow"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "bin"
19
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.10"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec"
25
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: macrow
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Izumiya Keisuke
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-11-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: This gem provide DSL for define rules of text replacing. It makes the
56
+ code more readable and helps avoinding fat model.
57
+ email:
58
+ - ksk.i.530@gmail.com
59
+ executables:
60
+ - console
61
+ - setup
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - ".gitignore"
66
+ - ".rspec"
67
+ - ".travis.yml"
68
+ - Gemfile
69
+ - LICENSE.txt
70
+ - README.md
71
+ - Rakefile
72
+ - bin/console
73
+ - bin/setup
74
+ - lib/macrow.rb
75
+ - lib/macrow/error.rb
76
+ - lib/macrow/version.rb
77
+ - macrow.gemspec
78
+ homepage: https://github.com/syguer/macrow
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.4.5.1
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Small helper for text replacing that using DSL
102
+ test_files: []