chronograph 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.
data/.autotest ADDED
@@ -0,0 +1,23 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'autotest/restart'
4
+
5
+ # Autotest.add_hook :initialize do |at|
6
+ # at.extra_files << "../some/external/dependency.rb"
7
+ #
8
+ # at.libs << ":../some/external"
9
+ #
10
+ # at.add_exception 'vendor'
11
+ #
12
+ # at.add_mapping(/dependency.rb/) do |f, _|
13
+ # at.files_matching(/test_.*rb$/)
14
+ # end
15
+ #
16
+ # %w(TestA TestB).each do |klass|
17
+ # at.extra_class_map[klass] = "test/test_misc.rb"
18
+ # end
19
+ # end
20
+
21
+ # Autotest.add_hook :run_command do |at|
22
+ # system "rake build"
23
+ # end
data/History.txt ADDED
@@ -0,0 +1,4 @@
1
+ === 0.0.1 / 2010-11-08
2
+
3
+ * First release, gem works, most planned features added
4
+
data/Manifest.txt ADDED
@@ -0,0 +1,15 @@
1
+ .autotest
2
+ History.txt
3
+ Manifest.txt
4
+ README.txt
5
+ Rakefile
6
+ lib/chronograph.rb
7
+ lib/chronograph/dsl.rb
8
+ lib/chronograph/event.rb
9
+ lib/chronograph/person.rb
10
+ lib/chronograph/timeline.rb
11
+ test/test_timeline.rb
12
+ test/test_dsl.rb
13
+ test/test_person.rb
14
+ test/test_event.rb
15
+ examples/programing_example.rb
data/README.txt ADDED
@@ -0,0 +1,67 @@
1
+ = chronograph
2
+
3
+ * http://geeklob.wordpress.com
4
+
5
+ == DESCRIPTION:
6
+
7
+ Chronograph is a Ruby DSL for making timelines
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ * Intuitive syntax
12
+ * Code easy to understand, good example DSL
13
+ * Multiple output formats
14
+
15
+ == SYNOPSIS:
16
+
17
+ require 'chronograph'
18
+
19
+ x = Chronograph::Timeline.new do
20
+ event "Chronograph released on github" Date.new(2010, 11, 7), "Chronograph is released in a github repo"
21
+ end
22
+
23
+ You can then get the timeline in html table format or in plain text.
24
+
25
+ For a better example, see example/ directory.
26
+
27
+ == REQUIREMENTS:
28
+
29
+ * kramdown
30
+
31
+ == INSTALL:
32
+
33
+ * gem install indigo747-chronograph # Warning, hasn't been tested
34
+
35
+ == DEVELOPERS:
36
+
37
+ After checking out the source, run:
38
+
39
+ $ rake newb
40
+
41
+ This task will install any missing dependencies, run the tests/specs,
42
+ and generate the RDoc.
43
+
44
+ == LICENSE:
45
+
46
+ (The MIT License)
47
+
48
+ Copyright (c) 2010 FIX
49
+
50
+ Permission is hereby granted, free of charge, to any person obtaining
51
+ a copy of this software and associated documentation files (the
52
+ 'Software'), to deal in the Software without restriction, including
53
+ without limitation the rights to use, copy, modify, merge, publish,
54
+ distribute, sublicense, and/or sell copies of the Software, and to
55
+ permit persons to whom the Software is furnished to do so, subject to
56
+ the following conditions:
57
+
58
+ The above copyright notice and this permission notice shall be
59
+ included in all copies or substantial portions of the Software.
60
+
61
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
62
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
63
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
64
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
65
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
66
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
67
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+
6
+ Hoe.spec 'chronograph' do
7
+ developer('Indigo Casson', 'atamiser@gmail.com')
8
+
9
+ # self.rubyforge_name = 'chronographx' # if different than 'chronograph'
10
+ end
11
+
12
+ # vim: syntax=ruby
@@ -0,0 +1,56 @@
1
+ #!/usr/bin/ruby
2
+ # Sorry to java lovers. Interestingly, I like java, but I needed a subject
3
+ # matter, and the French revolution didn't seem like a good one.
4
+
5
+ require 'rubygems'
6
+ require 'chronograph' # This is the all important line
7
+
8
+ t = Chronograph::Timeline.new do
9
+
10
+ # Groups affect html tags, but not, as of now, affect plain text
11
+ group :ruby do
12
+ # Group related events and people together. Whenever you recurse
13
+ # like this, you get the same DSL. Note that while this looks
14
+ # recursive, the recursivity of the data is not saved.
15
+ person "Mr. Ruby", 1654, 2010, "He was awesome" do
16
+ # This event could be here, it could be in the
17
+ # root of the block, or in one further in. It
18
+ # doesn't matter
19
+ event "Mr. Ruby leaves Japan", 2000, "It was _easy_"
20
+
21
+ # People, long_events, and wars are handled in
22
+ # essentially the same way, but with different
23
+ # wording for the start and end events.
24
+ war "War against Java", 2005, 2007, "It was needed" do
25
+
26
+ # When you need a more precise date, you can use the default
27
+ # constructor. When you just need a year, just use the year.
28
+ # In this case, you could just use 2005, though it will show up
29
+ # as January 1st of 2005.
30
+ event "Battle of the MRI", Date.new(2005, 5, 6), "Mr. Ruby almost lost"
31
+ # These events get added to the general event list. All events
32
+ # do regardless of where they are defined. Niether location nor
33
+ # order of the definition matters.
34
+ event "Battle of the JVM", Date.new(2005, 10, 31), "This was one scary halloween for Javaland"
35
+ end
36
+ end
37
+ end
38
+
39
+ group :java do
40
+ # This is the stupid and redundant way of doing things.
41
+ # Can you guess how we could improve this?
42
+ event "Javaland founded", Date.new(1990), "1000s die"
43
+
44
+ event "Clojure", 2000, "people are intrigued"
45
+
46
+ end
47
+
48
+
49
+ # This represents today.
50
+ today
51
+ end
52
+
53
+ # This first prints out the html table, and then the plain text version
54
+ puts t.get_html_table
55
+ puts t.get_text
56
+
@@ -0,0 +1,64 @@
1
+ require 'chronograph/event'
2
+ require 'chronograph/person'
3
+
4
+ module Chronograph
5
+ class EventDSL
6
+ attr_accessor :events, :people
7
+ def initialize()
8
+ @events = []
9
+ @people = []
10
+ end
11
+
12
+
13
+ def event(name, date, desc)
14
+ date = parse_date date
15
+ @events << Event.new(name, date, desc)
16
+ end
17
+
18
+ def today
19
+ event "Today", Date.today, "this day"
20
+ end
21
+
22
+ def person(name, birth, death, desc, &block)
23
+ birth = parse_date birth
24
+ death = parse_date death
25
+ t = EventDSL.new
26
+ t.instance_eval(&block) if block != nil
27
+
28
+ @people << Person.new(name, birth, death, desc, *(t.events + t.people.map { |x| x.events })) # TODO: this sort of combines the events and the people. Problem?
29
+ end
30
+
31
+ def long_event(name, birth, death, desc, &block)
32
+ birth = parse_date birth
33
+ death = parse_date death
34
+ t = EventDSL.new
35
+ t.instance_eval(&block) if block != nil
36
+
37
+ @people << LongEvent.new(name, birth, death, desc, *(t.events + t.people.map { |x| x.events }))
38
+ end
39
+
40
+ def war(name, birth, death, desc, &block)
41
+ birth = parse_date birth
42
+ death = parse_date death
43
+ t = EventDSL.new
44
+ t.instance_eval(&block) if block != nil
45
+
46
+ @people << War.new(name, birth, death, desc, *(t.events + t.people.map { |x| x.events }))
47
+ end
48
+
49
+ def parse_date x
50
+ x.is_a?(Integer) ? Date.new(x) : x
51
+ end
52
+
53
+ def group name, &block
54
+ events = []
55
+ t = EventDSL.new
56
+ t.instance_eval(&block) if block != nil
57
+ events << t.events
58
+ events << t.people.map { |x| x.events}
59
+ events.flatten!
60
+ events.each { |x| x.group = name}
61
+ @events << events.flatten
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,29 @@
1
+ require "kramdown"
2
+
3
+ module Chronograph
4
+ class Event
5
+ attr_accessor :name, :date, :desc, :group
6
+ def initialize(name, date, desc, group=:default)
7
+ raise TypeError.new("date must be a date") unless date.is_a? Date
8
+ raise TypeError.new("name has to be a name or symbol") unless name.is_a?(String) || name.is_a?(Symbol)
9
+ raise TypeError.new("desc must be a string") unless desc.is_a? String
10
+ raise TypeError.new("group has to be a name or symbol") unless group.is_a?(String) || group.is_a?(Symbol)
11
+ @name = name
12
+ @date = date
13
+ @desc = desc
14
+ @group = group
15
+ end
16
+
17
+ def to_table_html
18
+ "<tr class=\"#{@group}\">"+
19
+ "<td class=\"date\">#{@date}</td>"+
20
+ "<td class=\"name\">#{@name}</td>"+
21
+ "<td class=\"desc\">#{Kramdown::Document.new(@desc).to_html}</td>"+
22
+ "</tr>"
23
+ end
24
+ end
25
+ end
26
+
27
+ def parse_date x
28
+ x.is_a?(Integer) ? Date.new(x) : x
29
+ end
@@ -0,0 +1,44 @@
1
+ module Chronograph
2
+ class Person < Struct.new(:name, :birth, :death, :desc, :events)
3
+ attr_reader :name, :birth, :death, :desc
4
+ def initialize(name, birth, death, desc, *events)
5
+ raise TypeError.new("name has to be a name or symbol") unless name.is_a?(String) || name.is_a?(Symbol)
6
+ raise TypeError.new("birth and death must be a date") unless birth.is_a?(Date) && death.is_a?(Date)
7
+
8
+ raise TypeError.new("desc must be a string") unless desc.is_a? String
9
+
10
+ @name = name
11
+ @birth = birth
12
+ @death = death
13
+ @desc = desc
14
+ @events = events
15
+ end
16
+
17
+ def events
18
+ [begining_event, ending_event, @events].flatten
19
+ end
20
+
21
+ def begining_event
22
+ Event.new("Birth of #{name}", birth, "#{name} is born. #{@desc}")
23
+ end
24
+
25
+ def ending_event
26
+ Event.new("Death of #{name}", death, "#{name} dies. #{@desc}")
27
+ end
28
+
29
+
30
+ end
31
+
32
+ class LongEvent < Person
33
+ def begining_event
34
+ Event.new("Beginning of #{name}", birth, "#{name} begins. #{@desc}")
35
+ end
36
+
37
+ def ending_event
38
+ Event.new("End of #{name}", death, "#{name} ends. #{@desc}")
39
+ end
40
+ end
41
+
42
+
43
+ class War < LongEvent; end
44
+ end
@@ -0,0 +1,56 @@
1
+
2
+ require 'chronograph/dsl'
3
+
4
+ module Chronograph
5
+ class Timeline
6
+ include Enumerable
7
+
8
+ def initialize(&block)
9
+ @people = []
10
+ @events = []
11
+
12
+ t = EventDSL.new
13
+ t.instance_eval(&block)
14
+ @people << t.people
15
+ @events << t.events
16
+ end
17
+
18
+ def add &block
19
+ t = EventDSL.new
20
+ t.instance_eval(&block)
21
+ @people << t.people
22
+ @event << t.events
23
+ end
24
+
25
+ def each(&block)
26
+ consolidate.each(&block)
27
+ end
28
+
29
+ def event(name, date, desc)
30
+ @events << Event.new(name, date, desc)
31
+ end
32
+
33
+ def today
34
+ event "Today", Date.today, "this day"
35
+ end
36
+
37
+ def consolidate
38
+ t = []
39
+ t << @events << @people.flatten.map { |x| x.events }
40
+ return t.flatten.sort { |a, b| a.date <=> b.date }
41
+ end
42
+
43
+ def get_text
44
+ consolidate.map { |x| "#{x.date}: #{x.name}, #{x.desc}" }.join("\n")
45
+ end
46
+
47
+ def get_html_table
48
+ # warn "Warning, get_html_table has not tested"
49
+ r = "<table>"
50
+ consolidate.each do |x|
51
+ r += x.to_table_html
52
+ end
53
+ r += "</table>"
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,12 @@
1
+ libdir = File.dirname(__FILE__)
2
+ $LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir)
3
+
4
+
5
+ require 'date'
6
+ require 'chronograph/timeline'
7
+
8
+
9
+ module Chronograph
10
+ VERSION = "0.0.1"
11
+ end
12
+
data/test/test_dsl.rb ADDED
@@ -0,0 +1,40 @@
1
+ require "test/unit"
2
+ require "chronograph"
3
+
4
+ module Chronograph
5
+ class TestDSL < Test::Unit::TestCase
6
+ def test_sanity
7
+ assert_nothing_raised(Exception) { t = EventDSL.new }
8
+ end
9
+
10
+ def test_event
11
+ name, date, desc = "test", Date.today, "test_cool"
12
+ t = EventDSL.new
13
+ t.event name, date, desc
14
+ x = t.events[0]
15
+ assert(x.is_a?(Event), "Oh dear, the event wasn't an event!")
16
+ end
17
+
18
+ def test_person
19
+ t = EventDSL.new
20
+ t.person "test", Date.today-10, Date.today, "a good man" do ||
21
+ event "test event", Date.today-5, "test event"
22
+ end
23
+ p = t.people[0]
24
+ assert(p.is_a?(Person), "Oh god, the person wasn't a person!")
25
+ # Later, we could add more for long_event or war
26
+ end
27
+ def test_group
28
+ t = EventDSL.new
29
+ t.group :test do
30
+ event "Test", 6, "test"
31
+ person "Test", 6, 7, "test"
32
+ end
33
+
34
+ x = t.events.flatten
35
+
36
+ assert_equal(3, x.length)
37
+ x.each { |e| assert_equal(:test, e.group) }
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,50 @@
1
+ require "test/unit"
2
+ require "chronograph/event"
3
+
4
+ module Chronograph
5
+ class TestChronograph < Test::Unit::TestCase
6
+ def test_parse_date
7
+ assert_equal(Date.today, parse_date(Date.today))
8
+ assert_equal(Date.new(2010), parse_date(2010))
9
+ end
10
+
11
+ def test_errors
12
+ assert_raise(TypeError) { Event.new(nil, nil, nil, nil) }
13
+ end
14
+
15
+ def test_recall
16
+ name = :test
17
+ date = Date.today
18
+ desc = "cool"
19
+ group = :green
20
+ e = Event.new(name, date, desc, group)
21
+ assert_equal(name, e.name)
22
+ assert_equal(date, e.date)
23
+ assert_equal(desc, e.desc)
24
+ assert_equal(group, e.group)
25
+ end
26
+
27
+ def test_to_table_html
28
+ name = :test
29
+ date = Date.today
30
+ desc = "cool"
31
+ group = :green
32
+ e = Event.new(name, date, desc, group)
33
+ # First we test without making use of Kramdown
34
+ assert_equal("<tr class=\"#{group}\">"+
35
+ "<td class=\"date\">#{date}</td>"+
36
+ "<td class=\"name\">#{name}</td>"+
37
+ "<td class=\"desc\"><p>#{desc}</p>\n</td>"+
38
+ "</tr>", e.to_table_html)
39
+ desc = "_cool_"
40
+ e = Event.new(name, date, desc, group)
41
+ # Now we start messing around with Kramdown. Note the redefinition of desc to surround cool with underscores
42
+ assert_equal("<tr class=\"#{group}\">"+
43
+ "<td class=\"date\">#{date}</td>"+
44
+ "<td class=\"name\">#{name}</td>"+
45
+ "<td class=\"desc\"><p><em>cool</em></p>\n</td>"+
46
+ "</tr>", e.to_table_html)
47
+ end
48
+ # And that's all she wrote, folks. There isn't much else to test.
49
+ end
50
+ end
@@ -0,0 +1,35 @@
1
+ require "test/unit"
2
+ require "chronograph/person"
3
+
4
+ module Chronograph
5
+ class TestPerson < Test::Unit::TestCase
6
+ def setup
7
+ @t = nil
8
+ assert_nothing_raised(Exception) do
9
+ @t = Person.new("test", Date.today, Date.today+10, "this was awesome", Event.new("test event", Date.today+5, "test event"))
10
+ end
11
+ end
12
+
13
+ def test_begining_event
14
+ b = @t.begining_event
15
+ assert(b.is_a?(Event), "The begining event is not an event!")
16
+
17
+ e = @t.ending_event
18
+ assert(e.is_a?(Event), "The ending event is not an event!")
19
+ end
20
+
21
+ def test_recall
22
+ name = "test"
23
+ begining = Date.new(2000)
24
+ ending = Date.new(2010)
25
+ desc = "this is awesome"
26
+ event = Event.new("test", Date.new(2005), "awesome")
27
+
28
+ x = Person.new(name, begining, ending, desc, event)
29
+ assert_equal(name, x.name)
30
+ assert_equal(begining, x.birth)
31
+ assert_equal(ending, x.death)
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,14 @@
1
+ require "test/unit"
2
+ require "chronograph/timeline"
3
+
4
+ module Chronograph
5
+ class TestTimeline < Test::Unit::TestCase
6
+ def test_sanity
7
+ assert_nothing_raised(Exception) do
8
+ t = Timeline.new do
9
+ event "Test", 2010, "awesome"
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chronograph
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Indigo Casson
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-01-02 00:00:00 -08:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rubyforge
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 2
29
+ - 0
30
+ - 4
31
+ version: 2.0.4
32
+ type: :development
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: hoe
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 2
43
+ - 7
44
+ - 0
45
+ version: 2.7.0
46
+ type: :development
47
+ version_requirements: *id002
48
+ description: Chronograph is a Ruby DSL for making timelines
49
+ email:
50
+ - atamiser@gmail.com
51
+ executables: []
52
+
53
+ extensions: []
54
+
55
+ extra_rdoc_files:
56
+ - History.txt
57
+ - Manifest.txt
58
+ - README.txt
59
+ files:
60
+ - .autotest
61
+ - History.txt
62
+ - Manifest.txt
63
+ - README.txt
64
+ - Rakefile
65
+ - lib/chronograph.rb
66
+ - lib/chronograph/dsl.rb
67
+ - lib/chronograph/event.rb
68
+ - lib/chronograph/person.rb
69
+ - lib/chronograph/timeline.rb
70
+ - test/test_timeline.rb
71
+ - test/test_dsl.rb
72
+ - test/test_person.rb
73
+ - test/test_event.rb
74
+ - examples/programing_example.rb
75
+ has_rdoc: true
76
+ homepage: http://geeklob.wordpress.com
77
+ licenses: []
78
+
79
+ post_install_message:
80
+ rdoc_options:
81
+ - --main
82
+ - README.txt
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ segments:
90
+ - 0
91
+ version: "0"
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ segments:
97
+ - 0
98
+ version: "0"
99
+ requirements: []
100
+
101
+ rubyforge_project: chronograph
102
+ rubygems_version: 1.3.6
103
+ signing_key:
104
+ specification_version: 3
105
+ summary: Chronograph is a Ruby DSL for making timelines
106
+ test_files:
107
+ - test/test_timeline.rb
108
+ - test/test_person.rb
109
+ - test/test_event.rb
110
+ - test/test_dsl.rb