nelson 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fe237670123049b19a549ca9fbb13e9e3a8ad7dd
4
- data.tar.gz: f306e36af7f6d2c5cebabc1726fa92e53eae712e
3
+ metadata.gz: 82ec4b93c37ae50aef8150c90dd14f1e959dca31
4
+ data.tar.gz: 443e8a03fb69dd634d459ce7e97cfcfd8c3e7ce3
5
5
  SHA512:
6
- metadata.gz: 95d5a5da40ff327ff1ee006b764e0375de747e947bdeddee81b10ccd358e90566478eab1ad71125a00d1b4dab2a31598babf87b3a48d57258aa72b827087185f
7
- data.tar.gz: 1b9e0f1ecf82f66514d483dbe89a1d3a89f1ab38db584d39efcdf92cbbbbef8a46a0bf4ed3c6b896f1682da011edeea545f1c9730d172e1323e8c8fb3c4c1962
6
+ metadata.gz: c05a5f614bf7788d3d71827af5bfc228b958e2ec1852d992022526da4ec5961520a9f33ac9b511da72c0462b9078af7e0d966423fd67318cbaff053159e321c3
7
+ data.tar.gz: 2060d09305d3dbbed963028e6ea063853a944a90ff4fa44aa8f39d76bd190bfe0cd5454b9377b08e526f1ea938adae231fd20d98760905e9c81cecdbd6196fbf
data/Guardfile ADDED
@@ -0,0 +1,50 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ ## Uncomment and set this to only include directories you want to watch
5
+ # directories %w(app lib config test spec features)
6
+
7
+ ## Uncomment to clear the screen before every task
8
+ # clearing :on
9
+
10
+ ## Guard internally checks for changes in the Guardfile and exits.
11
+ ## If you want Guard to automatically start up again, run guard in a
12
+ ## shell loop, e.g.:
13
+ ##
14
+ ## $ while bundle exec guard; do echo "Restarting Guard..."; done
15
+ ##
16
+ ## Note: if you are using the `directories` clause above and you are not
17
+ ## watching the project directory ('.'), then you will want to move
18
+ ## the Guardfile to a watched dir and symlink it back, e.g.
19
+ #
20
+ # $ mkdir config
21
+ # $ mv Guardfile config/
22
+ # $ ln -s config/Guardfile .
23
+ #
24
+ # and, you'll have to watch "config/Guardfile" instead of "Guardfile"
25
+
26
+ # Note: The cmd option is now required due to the increasing number of ways
27
+ # rspec may be run, below are examples of the most common uses.
28
+ # * bundler: 'bundle exec rspec'
29
+ # * bundler binstubs: 'bin/rspec'
30
+ # * spring: 'bin/rspec' (This will use spring if running and you have
31
+ # installed the spring binstubs per the docs)
32
+ # * zeus: 'zeus rspec' (requires the server to be started separately)
33
+ # * 'just' rspec: 'rspec'
34
+
35
+ guard :rspec, cmd: "bundle exec rspec" do
36
+ require "guard/rspec/dsl"
37
+ dsl = Guard::RSpec::Dsl.new(self)
38
+
39
+ # RSpec files
40
+ rspec = dsl.rspec
41
+ watch(rspec.spec_helper) { rspec.spec_dir }
42
+ watch(rspec.spec_support) { rspec.spec_dir }
43
+ watch(rspec.spec_files)
44
+
45
+ # Ruby files
46
+ dsl.watch_spec_files_for(dsl.ruby.lib_files)
47
+
48
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
49
+
50
+ end
data/lib/nelson/dsl.rb ADDED
@@ -0,0 +1,14 @@
1
+
2
+ module Nelson
3
+ module DSL
4
+ module_function
5
+
6
+ def product_of(*terms)
7
+ MultipicationExpressionBuilder.new(*terms)
8
+ end
9
+
10
+ def sum_of(*terms)
11
+ AdditionExpressionBuilder.new(*terms)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,32 @@
1
+
2
+ module Nelson
3
+ class Expression
4
+ attr_accessor :terms
5
+
6
+ def initialize(*terms)
7
+ raise ArgumentError, "At least 1 term must be specified" unless terms.length > 0
8
+ @terms = terms
9
+ end
10
+
11
+ def *(term)
12
+ rhs_value = self.call
13
+ lhs_value = try_to_eval term
14
+ MultipicationExpression.new(rhs_value, lhs_value).call
15
+ end
16
+
17
+ def +(term)
18
+ rhs_value = self.call
19
+ lhs_value = try_to_eval term
20
+ AdditionExpression.new(rhs_value, lhs_value).call
21
+ end
22
+
23
+ private
24
+
25
+ def try_to_eval(term)
26
+ term.respond_to?(:call) ? term.call : term
27
+ end
28
+ end
29
+
30
+ autoload :MultipicationExpression, "nelson/expressions/multipication_expression"
31
+ autoload :AdditionExpression, "nelson/expressions/addition_expression"
32
+ end
@@ -0,0 +1,27 @@
1
+
2
+ module Nelson
3
+ class ExpressionBuilder
4
+ attr_accessor :terms
5
+
6
+ def initialize(*terms)
7
+ raise ArgumentError, "At least 1 term must be specified" unless terms.length > 0
8
+ @terms = terms
9
+ end
10
+
11
+ def and(term)
12
+ terms << term
13
+ return self
14
+ end
15
+
16
+ def times(term)
17
+ MultipicationExpressionBuilder.new(self, term)
18
+ end
19
+
20
+ def plus(term)
21
+ AdditionExpressionBuilder.new(self, term)
22
+ end
23
+ end
24
+
25
+ autoload :MultipicationExpressionBuilder, "nelson/expression_builders/multipication_expression_builder"
26
+ autoload :AdditionExpressionBuilder, "nelson/expression_builders/addition_expression_builder"
27
+ end
@@ -0,0 +1,9 @@
1
+
2
+ module Nelson
3
+ class AdditionExpressionBuilder < ExpressionBuilder
4
+ def build
5
+ terms.map! { |e| e.is_a?(ExpressionBuilder) ? e.build : e }
6
+ AdditionExpression.new(*terms)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,19 @@
1
+
2
+ module Nelson
3
+ class MultipicationExpression < Expression
4
+ def call
5
+ terms.reduce(:*)
6
+ end
7
+
8
+ def to_s
9
+ terms.sort_by { |t| t.is_a?(Expression) ? 1 : 0 }.reduce("") do |c, t|
10
+ case t
11
+ when Expression
12
+ c << "(#{t})"
13
+ else
14
+ c = [c, t].map(&:to_s).reject(&:empty?).join("*")
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,15 @@
1
+
2
+ module Nelson
3
+ class AdditionExpression < Expression
4
+ def call
5
+ terms.reduce(:+)
6
+ end
7
+
8
+
9
+ def to_s
10
+ terms.map do |t|
11
+ t.is_a?(Expression) ? "(#{t})" : t
12
+ end.join("+")
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,9 @@
1
+
2
+ module Nelson
3
+ class MultipicationExpressionBuilder < ExpressionBuilder
4
+ def build
5
+ terms.map! { |e| e.is_a?(ExpressionBuilder) ? e.build : e }
6
+ MultipicationExpression.new(*terms)
7
+ end
8
+ end
9
+ end
@@ -1,3 +1,3 @@
1
1
  module Nelson
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/nelson.rb CHANGED
@@ -1,97 +1,8 @@
1
1
  require "nelson/version"
2
+ require "nelson/expression"
3
+ require "nelson/expression_builder"
4
+ require "nelson/dsl"
2
5
 
3
6
  module Nelson
4
- def self.product_of(*terms)
5
- MultipicationExpressionBuilder.new(*terms)
6
- end
7
-
8
- def self.sum_of(*terms)
9
- AdditionExpressionBuilder.new(*terms)
10
- end
11
-
12
-
13
- class Expression
14
- attr_accessor :terms
15
-
16
- def initialize(*terms)
17
- @terms = terms
18
- end
19
-
20
- def *(term)
21
- rhs_value = self.call
22
- lhs_value = term.respond_to?(:call) ? term.call : term
23
- MultipicationExpression.new(rhs_value, lhs_value).call
24
- end
25
-
26
- def +(term)
27
- rhs_value = self.call
28
- lhs_value = term.respond_to?(:call) ? term.call : term
29
- AdditionExpression.new(rhs_value, lhs_value).call
30
- end
31
- end
32
-
33
- class ExpressionBuilder
34
- attr_accessor :terms
35
-
36
- def initialize(*terms)
37
- @terms = terms
38
- end
39
-
40
- def and(term)
41
- terms << term
42
- return self
43
- end
44
-
45
- def times(term)
46
- MultipicationExpressionBuilder.new(self, term)
47
- end
48
-
49
- def plus(term)
50
- AdditionExpressionBuilder.new(self, term)
51
- end
52
- end
53
-
54
- class MultipicationExpression < Expression
55
- def call
56
- terms.reduce(:*)
57
- end
58
-
59
- def to_s
60
- terms.sort_by { |t| t.is_a?(Expression) ? 1 : 0 }.reduce("") do |c, t|
61
- case t
62
- when Expression
63
- c << "(#{t})"
64
- else
65
- c = [c, t].map(&:to_s).reject(&:empty?).join("*")
66
- end
67
- end
68
- end
69
- end
70
-
71
- class MultipicationExpressionBuilder < ExpressionBuilder
72
- def build
73
- terms.map! { |e| e.is_a?(ExpressionBuilder) ? e.build : e }
74
- MultipicationExpression.new(*terms)
75
- end
76
- end
77
-
78
- class AdditionExpression < Expression
79
- def call
80
- terms.reduce(:+)
81
- end
82
-
83
-
84
- def to_s
85
- terms.map do |t|
86
- t.is_a?(Expression) ? "(#{t})" : t
87
- end.join("+")
88
- end
89
- end
90
-
91
- class AdditionExpressionBuilder < ExpressionBuilder
92
- def build
93
- terms.map! { |e| e.is_a?(ExpressionBuilder) ? e.build : e }
94
- AdditionExpression.new(*terms)
95
- end
96
- end
7
+ extend DSL
97
8
  end
data/nelson.gemspec CHANGED
@@ -18,8 +18,12 @@ Gem::Specification.new do |spec|
18
18
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
19
  spec.require_paths = ["lib"]
20
20
 
21
+ spec.required_ruby_version = '~> 2.1'
22
+
21
23
  spec.add_development_dependency "bundler", "~> 1.10"
22
24
  spec.add_development_dependency "rake", "~> 10.0"
23
25
  spec.add_development_dependency "rspec"
24
26
  spec.add_development_dependency "pry"
27
+ spec.add_development_dependency "guard"
28
+ spec.add_development_dependency "guard-rspec"
25
29
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nelson
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Patrick Metcalfe
@@ -66,6 +66,34 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: guard
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: guard-rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
69
97
  description: This library provides a flient API to write math expressions that can
70
98
  be manipulated and solved programmatically.
71
99
  email:
@@ -78,12 +106,20 @@ files:
78
106
  - ".rspec"
79
107
  - ".travis.yml"
80
108
  - Gemfile
109
+ - Guardfile
81
110
  - LICENSE.txt
82
111
  - README.md
83
112
  - Rakefile
84
113
  - bin/console
85
114
  - bin/setup
86
115
  - lib/nelson.rb
116
+ - lib/nelson/dsl.rb
117
+ - lib/nelson/expression.rb
118
+ - lib/nelson/expression_builder.rb
119
+ - lib/nelson/expression_builders/addition_expression_builder.rb
120
+ - lib/nelson/expression_builders/multipication_expression_builder.rb
121
+ - lib/nelson/expressions/addition_expression.rb
122
+ - lib/nelson/expressions/multipication_expression.rb
87
123
  - lib/nelson/version.rb
88
124
  - nelson.gemspec
89
125
  homepage:
@@ -96,9 +132,9 @@ require_paths:
96
132
  - lib
97
133
  required_ruby_version: !ruby/object:Gem::Requirement
98
134
  requirements:
99
- - - ">="
135
+ - - "~>"
100
136
  - !ruby/object:Gem::Version
101
- version: '0'
137
+ version: '2.1'
102
138
  required_rubygems_version: !ruby/object:Gem::Requirement
103
139
  requirements:
104
140
  - - ">="