summary_judgement 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.0
1
+ 1.2.0
@@ -1,6 +1,12 @@
1
1
  class String
2
2
  def indefinite_article
3
- %w(a e i o u).include?(first.downcase) ? 'an' : 'a'
3
+ if WORDS_WITH_INITIAL_VOWELS_THAT_ACT_LIKE_WORDS_WITH_INITIAL_CONSONANTS.include? first_word.first_term
4
+ INDEFINITE_ARTICLES[:consonant]
5
+ elsif VOWELS.include? first.downcase
6
+ INDEFINITE_ARTICLES[:vowel]
7
+ else
8
+ INDEFINITE_ARTICLES[:consonant]
9
+ end
4
10
  end
5
11
 
6
12
  def with_indefinite_article(upcase = false)
@@ -10,4 +16,24 @@ class String
10
16
  def pluralize_on(qty)
11
17
  qty.is_a?(Numeric) and qty > 1 ? pluralize : self
12
18
  end
19
+
20
+ def first_word
21
+ split(' ').first
22
+ end
23
+
24
+ def first_term
25
+ split('-').first
26
+ end
27
+
28
+ WORDS_WITH_INITIAL_VOWELS_THAT_ACT_LIKE_WORDS_WITH_INITIAL_CONSONANTS = %w(one)
29
+ INDEFINITE_ARTICLES = { :vowel => 'an', :consonant => 'a'}
30
+ VOWELS = %w(a e i o u)
31
+ end
32
+
33
+ class Object
34
+ def recursive_send(*methods)
35
+ return self if methods.empty?
36
+ m = methods.shift
37
+ send(m).recursive_send(*methods)
38
+ end
13
39
  end
@@ -9,7 +9,7 @@ module SummaryJudgement
9
9
  end
10
10
 
11
11
  def term
12
- self.class.summary.class.render self.class.summary.term, self
12
+ self.class.summary.class.render self.class.summary.terms.find { |t| t.condition.nil? or self.class.summary.class.render(t.condition, self) }.phrase, self
13
13
  end
14
14
 
15
15
  def adjectives
@@ -21,7 +21,9 @@ module SummaryJudgement
21
21
  end
22
22
 
23
23
  def children
24
- self.class.summary.class.render self.class.summary.subordinates, self
24
+ subordinates = self.class.summary.class.render(self.class.summary.subordinates, self)
25
+ subordinates.flatten! if subordinates.respond_to? :flatten!
26
+ subordinates
25
27
  end
26
28
 
27
29
  def canopy
@@ -33,7 +35,7 @@ module SummaryJudgement
33
35
 
34
36
  def summarize_as_leaf(options = {})
35
37
  options.reverse_merge! :capitalize_indefinite_article => true
36
- "#{adjectives.join(' ').strip.with_indefinite_article(options[:capitalize_indefinite_article])} #{term} #{modifiers.join(' ')}".strip
38
+ "#{adjectives.join(' ').strip.concat(' ').concat(term).strip.with_indefinite_article(options[:capitalize_indefinite_article])} #{modifiers.join(' ')}".strip
37
39
  end
38
40
 
39
41
  def summarize_as_branch(options = {})
@@ -1,10 +1,10 @@
1
1
  module SummaryJudgement
2
2
  class Summary
3
- attr_reader :term, :adjectives, :modifiers, :subordinates, :base
3
+ attr_reader :terms, :adjectives, :modifiers, :subordinates, :base
4
4
 
5
5
  def initialize(base, options = {}, &blk)
6
6
  @base = base
7
- @term = options[:term] || base.to_s
7
+ @terms = options[:terms] || []
8
8
  @adjectives = options[:adjectives] || []
9
9
  @modifiers = options[:modifiers] || []
10
10
  @subordinates = options[:subordinates] || []
@@ -14,8 +14,8 @@ module SummaryJudgement
14
14
  yield self
15
15
  end
16
16
 
17
- def identity(t)
18
- @term = t
17
+ def identity(t = nil, options = {})
18
+ @terms << SummaryJudgement::Descriptor.new(t || @base.to_s.underscore.humanize.downcase, options)
19
19
  end
20
20
 
21
21
  def adjective(a, options = {})
@@ -26,12 +26,14 @@ module SummaryJudgement
26
26
  @modifiers << SummaryJudgement::Descriptor.new(m, options)
27
27
  end
28
28
 
29
- def children(collection_or_symbol)
30
- case collection_or_symbol
31
- when Symbol
32
- @subordinates << lambda { |parent| parent.send collection_or_symbol }
33
- else
34
- @subordinates << collection_or_symbol
29
+ def children(*collections_or_symbols)
30
+ collections_or_symbols.each do |collection_or_symbol|
31
+ case collection_or_symbol
32
+ when Symbol
33
+ @subordinates << lambda { |parent| parent.send collection_or_symbol }
34
+ else
35
+ @subordinates << collection_or_symbol
36
+ end
35
37
  end
36
38
  end
37
39
 
@@ -44,7 +46,16 @@ module SummaryJudgement
44
46
  end
45
47
 
46
48
  def to_hash
47
- { :adjectives => @adjectives.dup, :modifiers => @modifiers.dup, :subordinates => @subordinates.dup, :term => @term.dup }
49
+ [:adjectives, :modifiers, :subordinates, :terms].inject({}) do |properties, property|
50
+ val = instance_variable_get :"@#{property}"
51
+ case val
52
+ when Symbol
53
+ properties[property] = val
54
+ else
55
+ properties[property] = val.clone
56
+ end
57
+ properties
58
+ end
48
59
  end
49
60
 
50
61
  def dup(base)
@@ -55,7 +66,13 @@ module SummaryJudgement
55
66
  def render(obj, context)
56
67
  case obj
57
68
  when Array
58
- obj.empty? ? nil : obj.map {|o| render o, context}
69
+ if obj.empty?
70
+ nil
71
+ elsif obj.all? { |e| e.is_a? Symbol }
72
+ context.recursive_send(*obj)
73
+ else
74
+ obj.empty? ? nil : obj.map {|o| render o, context}
75
+ end
59
76
  when String
60
77
  obj
61
78
  when Symbol
@@ -3,15 +3,16 @@ require 'verbs'
3
3
 
4
4
  module SummaryJudgement
5
5
  def self.extended(base)
6
+ base.initialize_summary Summary.new(base)
7
+ base.send :include, InstanceMethods
6
8
  def base.inherited(subclass)
7
9
  subclass.initialize_summary @summary.dup(subclass)
10
+ super
8
11
  end
9
12
  end
10
13
 
11
14
  def summarize(&blk)
12
- @summary ||= Summary.new(self)
13
15
  @summary.define(&blk)
14
- send :include, InstanceMethods
15
16
  end
16
17
 
17
18
  def summary
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{summary_judgement}
8
- s.version = "1.1.0"
8
+ s.version = "1.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Andy Rossmeissl"]
12
- s.date = %q{2009-12-03}
12
+ s.date = %q{2009-12-07}
13
13
  s.description = %q{Constructs adaptive summaries of object hierarchies based on ActiveRecord associations and other simple relationship structures}
14
14
  s.email = %q{andy@rossmeissl.net}
15
15
  s.extra_rdoc_files = [
data/test/helper.rb CHANGED
@@ -68,8 +68,7 @@ class Library
68
68
 
69
69
  extend SummaryJudgement
70
70
  summarize do |has|
71
- has.children :books
72
- has.children :magazines
71
+ has.children :books, :magazines
73
72
  has.verb :have
74
73
  end
75
74
  end
@@ -86,3 +85,28 @@ class Catalog
86
85
  has.verb :contain
87
86
  end
88
87
  end
88
+
89
+ class Employee
90
+ extend SummaryJudgement
91
+ end
92
+
93
+ class Librarian < Employee
94
+ summarize do |has|
95
+ has.identity 'bookworm', :if => lambda { false }
96
+ has.identity
97
+ end
98
+ end
99
+
100
+ class Customer
101
+ extend SummaryJudgement
102
+
103
+ summarize do |has|
104
+ has.identity [:label, :downcase]
105
+ end
106
+ end
107
+
108
+ class Enthusiast < Customer
109
+ def label
110
+ 'AVID READER'
111
+ end
112
+ end
@@ -10,18 +10,21 @@ class TestSummaryJudgement < Test::Unit::TestCase
10
10
  @toilet = Library.new @neuromancer, @current_economist
11
11
  @bedstand = Library.new @neuromancer
12
12
  @catalog = Catalog.new @bookshelf, @toilet
13
+ @anne = Librarian.new
14
+ @seamus = Enthusiast.new
13
15
  end
14
16
 
15
17
  def test_setup
16
18
  assert_equal Book, @neuromancer.class
17
19
  assert_equal Library, @bookshelf.class
18
20
  assert_equal 2, @bookshelf.books.length
19
- assert_equal :accept, Verbs::Conjugator.conjugate(:accept, :tense => :present, :person => :first, :plurality => :singular)
21
+ assert_equal :accept, Verbs::Conjugator.conjugate(:accept, :tense => :present, :person => :first, :plurality => :singular)
22
+ assert_equal 'a', 'one'.indefinite_article
20
23
  end
21
24
 
22
25
  def test_summary_definition
23
26
  assert_equal SummaryJudgement::Summary, Book.summary.class
24
- assert_equal Proc, Book.summary.term.class
27
+ assert_equal Proc, Book.summary.terms.first.phrase.class
25
28
  assert_equal [SummaryJudgement::Descriptor], Book.summary.adjectives.collect { |a| a.class }.uniq
26
29
  assert_equal :have, Library.summary.predicate
27
30
  end
@@ -73,5 +76,14 @@ class TestSummaryJudgement < Test::Unit::TestCase
73
76
 
74
77
  def test_inheritance
75
78
  assert_equal 'A hardcover novel by William Gibson', @first_edition_neuromancer.summary
76
- end
79
+ assert_equal 'A librarian', @anne.summary
80
+ end
81
+
82
+ def test_lazily_inherited_summary_definitions
83
+ assert_equal 'An avid reader', @seamus.summary
84
+ end
85
+
86
+ def test_recursive_send_core_extension
87
+ assert_equal 'foo bar', 'foo_bar'.recursive_send(:humanize, :downcase)
88
+ end
77
89
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: summary_judgement
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Rossmeissl
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-03 00:00:00 -05:00
12
+ date: 2009-12-07 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies: []
15
15