tas 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d138b1a4f3efad009800267e3bf046c590367808
4
+ data.tar.gz: b9a12c95197fb4766be6072477d8c8e04896329c
5
+ SHA512:
6
+ metadata.gz: 59d1fd3d53c32818b88f840f050fcade4dd29950c19ac37cb4fdfd09edfcf83a29ca11116b1d23c344dafe7db139af87ea11fc1b42b025cf322f104e4c8b63af
7
+ data.tar.gz: b50c7c5c99da864006ee88f6bb54944e09432108eaa1a937e790600b9f69bb62d4ed8ffc325a12b08efdbc613c7b2addf29b2542f0d58874fd8d2efef39ceeb8
data/.gems ADDED
@@ -0,0 +1 @@
1
+ cutest -v 1.2.2
File without changes
@@ -0,0 +1,19 @@
1
+ This code tries to solve a particular problem with a very simple
2
+ implementation. We try to keep the code to a minimum while making
3
+ it as clear as possible. The design is very likely finished, and
4
+ if some feature is missing it is possible that it was left out on
5
+ purpose. That said, new usage patterns may arise, and when that
6
+ happens we are ready to adapt if necessary.
7
+
8
+ A good first step for contributing is to meet us on IRC and discuss
9
+ ideas. We spend a lot of time on #lesscode at freenode, always ready
10
+ to talk about code and simplicity. If connecting to IRC is not an
11
+ option, you can create an issue explaining the proposed change and
12
+ a use case. We pay a lot of attention to use cases, because our
13
+ goal is to keep the code base simple. Usually the result of a
14
+ conversation is the creation of a different tool.
15
+
16
+ Please don't start the conversation with a pull request. The code
17
+ should come at last, and even though it may help to convey an idea,
18
+ more often than not it draws the attention to a particular
19
+ implementation.
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2015 Michel Martens
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
@@ -0,0 +1,144 @@
1
+ Tas
2
+ ===
3
+
4
+ Trees as strings
5
+
6
+ Community
7
+ ---------
8
+
9
+ Meet us on IRC: [#lesscode](irc://chat.freenode.net/#lesscode) on
10
+ [freenode.net](http://freenode.net/).
11
+
12
+ Description
13
+ -----------
14
+
15
+ An instance of `Tas` represents a tree of similar objects, together
16
+ with a reducing function. The goal of the reducing function is to
17
+ reach a string representation of the tree, and that's why the
18
+ reduction triggering method is `to_s`. The `inspect` method is an
19
+ alias of `to_s`, with the consequence that the result of applying
20
+ the reducing function becomes the object's representation.
21
+
22
+ Usage
23
+ -----
24
+
25
+ Let's start with a trivial example:
26
+
27
+ ```ruby
28
+ view = Tas.new do |params|
29
+ params.to_s
30
+ end
31
+
32
+ view[:foo] = 1
33
+ view[:bar] = 2
34
+
35
+ assert_equal view.to_s, "{:foo=>1, :bar=>2}"
36
+ ```
37
+
38
+ We can use other instances of `Tas` as leaves:
39
+
40
+ ```ruby
41
+ foo = Tas.new do |params|
42
+ params.to_s
43
+ end
44
+
45
+ bar = Tas.new do |params|
46
+ params.to_s
47
+ end
48
+
49
+ baz = Tas.new do |params|
50
+ params.to_s
51
+ end
52
+
53
+ foo[:a] = 1
54
+ foo[:b] = bar
55
+
56
+ bar[:c] = 2
57
+ bar[:d] = baz
58
+
59
+ baz[:e] = 3
60
+ baz[:f] = 4
61
+
62
+ assert_equal foo.to_s, "{:a=>1, :b=>{:c=>2, :d=>{:e=>3, :f=>4}}}"
63
+ ```
64
+
65
+ To avoid the repeated definition of the reducing function, you can
66
+ create a new `Tas` instance from an existing one:
67
+
68
+ ```ruby
69
+ foo = Tas.new do |params|
70
+ params.to_s
71
+ end
72
+
73
+ bar = foo.new
74
+ baz = bar.new
75
+
76
+ foo[:bar] = bar
77
+ bar[:baz] = baz
78
+
79
+ assert_equal foo.to_s, "{:bar=>{:baz=>{}}}"
80
+ ```
81
+
82
+ The following example renders some views with [Mote][mote]:
83
+
84
+ ```ruby
85
+ require "tas"
86
+ require "mote"
87
+
88
+ # Create a viewer and define the rendering function
89
+ viewer = Tas.new do |params|
90
+ mote(params[:src], params)
91
+ end
92
+
93
+ # Create two different components
94
+ page = viewer.new
95
+ view = viewer.new
96
+
97
+ # Each component has its own template
98
+ page[:src] = "views/layout.mote"
99
+ view[:src] = "views/index.mote"
100
+
101
+ # Both components have a title
102
+ page[:title] = "Hello"
103
+ view[:title] = "Welcome!"
104
+
105
+ # Insert one component into another
106
+ page[:content] = view
107
+
108
+ # Render all components
109
+ page.to_s
110
+ ```
111
+
112
+ [mote]: https://github.com/soveran/mote
113
+
114
+ API
115
+ ---
116
+
117
+ `params`: Hash with components.
118
+
119
+ `reduce`: Function for processing the `params` hash.
120
+
121
+ `new`: Create a new instance and propagate the `reduce` function.
122
+
123
+ `[](marker)`: Return the component at `params[marker]`.
124
+
125
+ `[]=(marker, component)`: Assign the component to `params[marker]`.
126
+
127
+ `fetch(marker)`: Return the element at `params[marker]` or raise
128
+ if the element is not present.
129
+
130
+ Errors
131
+ ------
132
+
133
+ If no `reduce` function was defined, an attempt to call `to_s` will
134
+ raise the `Tas::ReduceMissing` exception.
135
+
136
+ An attempt to `fetch` an undefined marker will raise the
137
+ `Tas::MarkerMissing` exception.
138
+
139
+ Installation
140
+ ------------
141
+
142
+ ```
143
+ $ gem install tas
144
+ ```
@@ -0,0 +1,66 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Copyright (c) 2015 Michel Martens
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.
22
+
23
+ class Tas
24
+ attr_accessor :params
25
+ attr_accessor :reduce
26
+
27
+ MarkerMissing = Class.new(ArgumentError)
28
+ ReduceMissing = Class.new(ArgumentError)
29
+
30
+ DEFAULT = -> (params) {
31
+ raise(ReduceMissing)
32
+ }
33
+
34
+ def initialize(params = {}, &reduce)
35
+ @params = params
36
+ @reduce = reduce || DEFAULT
37
+ end
38
+
39
+ def fetch(marker)
40
+ @params.fetch(marker) do
41
+ raise(MarkerMissing, marker)
42
+ end
43
+ end
44
+
45
+ def [](marker)
46
+ @params[marker]
47
+ end
48
+
49
+ def []=(marker, component)
50
+ @params[marker] = component
51
+ end
52
+
53
+ def update(params)
54
+ @params.update(params)
55
+ end
56
+
57
+ def to_s
58
+ @reduce[@params]
59
+ end
60
+
61
+ def new
62
+ self.class.new(&reduce)
63
+ end
64
+
65
+ alias inspect to_s
66
+ end
@@ -0,0 +1,4 @@
1
+ .PHONY: test
2
+
3
+ test:
4
+ cutest ./test/*.rb
@@ -0,0 +1,14 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "tas"
3
+ s.version = "0.0.1"
4
+ s.summary = "Trees as strings"
5
+ s.description = "Represent trees as strings"
6
+ s.authors = ["Michel Martens"]
7
+ s.email = ["michel@soveran.com"]
8
+ s.homepage = "https://github.com/soveran/tas"
9
+ s.license = "MIT"
10
+
11
+ s.files = `git ls-files`.split("\n")
12
+
13
+ s.add_development_dependency "cutest"
14
+ end
@@ -0,0 +1,52 @@
1
+ require_relative "../lib/tas"
2
+
3
+ test "basic" do
4
+ viewer = Tas.new do |params|
5
+ sprintf(params[:format] % params)
6
+ end
7
+
8
+ foo = viewer.new
9
+ bar = viewer.new
10
+
11
+ foo[:format] = "%{hoge} %{piyo} and %{fuga}"
12
+ bar[:format] = "%{hoge}!"
13
+
14
+ foo[:hoge] = "right"
15
+ foo[:piyo] = "here"
16
+ foo[:fuga] = bar
17
+
18
+ bar[:hoge] = "now"
19
+
20
+ assert_equal foo.to_s, "right here and now!"
21
+ end
22
+
23
+ test "errors" do
24
+ foo = Tas.new
25
+
26
+ assert_raise(Tas::ReduceMissing) do
27
+ foo.to_s
28
+ end
29
+
30
+ assert_raise(Tas::MarkerMissing) do
31
+ foo.fetch(:src)
32
+ end
33
+
34
+ foo.reduce = -> (params) {
35
+ params.to_s
36
+ }
37
+
38
+ assert_equal "{}", foo.to_s
39
+
40
+ foo[:src] = "foo"
41
+
42
+ assert_equal "foo", foo.fetch(:src)
43
+ end
44
+
45
+ test "update" do
46
+ foo = Tas.new
47
+
48
+ foo.update(hage: 1, piyo: 2)
49
+
50
+ assert_equal 1, foo[:hage]
51
+ assert_equal 2, foo[:piyo]
52
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tas
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Michel Martens
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-11-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: cutest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Represent trees as strings
28
+ email:
29
+ - michel@soveran.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - .gems
35
+ - CHANGELOG
36
+ - CONTRIBUTING
37
+ - LICENSE
38
+ - README.md
39
+ - lib/tas.rb
40
+ - makefile
41
+ - tas.gemspec
42
+ - test/tas_test.rb
43
+ homepage: https://github.com/soveran/tas
44
+ licenses:
45
+ - MIT
46
+ metadata: {}
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubyforge_project:
63
+ rubygems_version: 2.0.14
64
+ signing_key:
65
+ specification_version: 4
66
+ summary: Trees as strings
67
+ test_files: []