humanized 0.0.1.alpha
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/lib/humanized.rb +52 -0
- data/lib/humanized/compiler.rb +127 -0
- data/lib/humanized/core_ext/array.rb +25 -0
- data/lib/humanized/core_ext/date.rb +21 -0
- data/lib/humanized/core_ext/hash.rb +25 -0
- data/lib/humanized/core_ext/module.rb +47 -0
- data/lib/humanized/core_ext/nilclass.rb +21 -0
- data/lib/humanized/core_ext/numeric.rb +21 -0
- data/lib/humanized/core_ext/object.rb +24 -0
- data/lib/humanized/core_ext/string.rb +21 -0
- data/lib/humanized/core_ext/symbol.rb +21 -0
- data/lib/humanized/core_ext/time.rb +21 -0
- data/lib/humanized/humanizer.rb +122 -0
- data/lib/humanized/interpolation/conjunctions.rb +34 -0
- data/lib/humanized/interpolation/date.rb +36 -0
- data/lib/humanized/interpolation/english.rb +83 -0
- data/lib/humanized/interpolation/german.rb +74 -0
- data/lib/humanized/interpolation/kng.rb +217 -0
- data/lib/humanized/interpolation/number.rb +36 -0
- data/lib/humanized/ref.rb +29 -0
- data/lib/humanized/scope.rb +264 -0
- data/lib/humanized/source.rb +169 -0
- data/lib/humanized/wrapper.rb +74 -0
- data/lib/more/humanized/json_source.rb +50 -0
- data/lib/more/humanized/yaml_source.rb +28 -0
- metadata +149 -0
data/lib/humanized.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
# This program is free software: you can redistribute it and/or modify
|
3
|
+
# it under the terms of the Affero GNU General Public License as published by
|
4
|
+
# the Free Software Foundation, either version 3 of the License, or
|
5
|
+
# (at your option) any later version.
|
6
|
+
#
|
7
|
+
# This program is distributed in the hope that it will be useful,
|
8
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
9
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
10
|
+
# GNU General Public License for more details.
|
11
|
+
#
|
12
|
+
# You should have received a copy of the GNU General Public License
|
13
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
14
|
+
#
|
15
|
+
# (c) 2011 by Hannes Georg
|
16
|
+
#
|
17
|
+
|
18
|
+
require "facets/module/home.rb"
|
19
|
+
require "facets/module/basename.rb"
|
20
|
+
require "facets/module/anonymous.rb"
|
21
|
+
require "facets/module/alias_method_chain.rb"
|
22
|
+
|
23
|
+
# Humanized is library which really helps you create human
|
24
|
+
# readable output.
|
25
|
+
module Humanized
|
26
|
+
|
27
|
+
module HasNaturalGenus
|
28
|
+
|
29
|
+
def self.included(base)
|
30
|
+
base.class_eval do
|
31
|
+
alias_method_chain :humanization_key, :genus
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def genus
|
36
|
+
return super if defined? super
|
37
|
+
raise NoMethodError, "Please implent a method `genus`!"
|
38
|
+
end
|
39
|
+
|
40
|
+
def humanization_key_with_genus
|
41
|
+
return humanization_key_without_genus.optionally(self.genus)
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
require "humanized/ref"
|
48
|
+
require "humanized/humanizer"
|
49
|
+
require "humanized/scope"
|
50
|
+
Dir[File.expand_path('humanized/core_ext/*.rb', File.dirname(__FILE__))].each do |file|
|
51
|
+
require file
|
52
|
+
end
|
@@ -0,0 +1,127 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
# This program is free software: you can redistribute it and/or modify
|
3
|
+
# it under the terms of the Affero GNU General Public License as published by
|
4
|
+
# the Free Software Foundation, either version 3 of the License, or
|
5
|
+
# (at your option) any later version.
|
6
|
+
#
|
7
|
+
# This program is distributed in the hope that it will be useful,
|
8
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
9
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
10
|
+
# GNU General Public License for more details.
|
11
|
+
#
|
12
|
+
# You should have received a copy of the GNU General Public License
|
13
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
14
|
+
#
|
15
|
+
# (c) 2011 by Hannes Georg
|
16
|
+
#
|
17
|
+
|
18
|
+
module Humanized
|
19
|
+
class Compiler
|
20
|
+
|
21
|
+
class Compiled < Proc
|
22
|
+
|
23
|
+
attr_accessor :str
|
24
|
+
|
25
|
+
def initialize(str)
|
26
|
+
@str = str
|
27
|
+
super()
|
28
|
+
return self
|
29
|
+
end
|
30
|
+
|
31
|
+
def to_s
|
32
|
+
@str
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
def initialize
|
38
|
+
@compiled = Hash.new{|hsh,x| hsh[x] = compile!(x)}
|
39
|
+
end
|
40
|
+
|
41
|
+
# Compiles a String into a Proc
|
42
|
+
# @param [String] str A formated String
|
43
|
+
# @return [Compiled] A Proc, which will handle interpolation.
|
44
|
+
#
|
45
|
+
def compile(str)
|
46
|
+
@compiled[str]
|
47
|
+
end
|
48
|
+
protected
|
49
|
+
|
50
|
+
VAR_REGEXP = /^%([a-z_]+)/
|
51
|
+
CALL_START_REGEXP = /^\[([a-z]+)[\]\|]/
|
52
|
+
END_REGEXP = /[\[\]%\|]/
|
53
|
+
|
54
|
+
TRANSFORMER = lambda{|token|
|
55
|
+
if token.kind_of? Array
|
56
|
+
"[#{token.map(&TRANSFORMER).join(',')}].join()"
|
57
|
+
elsif token.kind_of? String
|
58
|
+
token.inspect
|
59
|
+
elsif token.kind_of? Symbol
|
60
|
+
"variables[#{token.inspect}]"
|
61
|
+
elsif token.kind_of? Hash
|
62
|
+
"interpolater.#{token[:method]}(humanizer,#{token[:args].map(&TRANSFORMER).join(',')})"
|
63
|
+
end
|
64
|
+
}
|
65
|
+
|
66
|
+
def compile!(str)
|
67
|
+
return eval('Compiled.new(str){|humanizer,interpolater,variables| ' + TRANSFORMER.call(read(str)) +' }')
|
68
|
+
end
|
69
|
+
|
70
|
+
def read(str)
|
71
|
+
result = []
|
72
|
+
rest = str
|
73
|
+
while( rest.size > 0 )
|
74
|
+
token, rest = read_one(rest)
|
75
|
+
if token.kind_of? String and result.last.kind_of? String
|
76
|
+
result.last << token
|
77
|
+
else
|
78
|
+
result << token
|
79
|
+
end
|
80
|
+
end
|
81
|
+
return result
|
82
|
+
end
|
83
|
+
|
84
|
+
def read_one(str)
|
85
|
+
return str,str if str.size == 0
|
86
|
+
match = nil
|
87
|
+
if str =~ VAR_REGEXP
|
88
|
+
return $1.to_sym, str[($1.size+1)..-1]
|
89
|
+
elsif str =~ CALL_START_REGEXP
|
90
|
+
method = $1
|
91
|
+
args = []
|
92
|
+
rest = str[($1.size+1)..-1]
|
93
|
+
while rest[0] != ?]
|
94
|
+
arg = []
|
95
|
+
rest = rest[1..-1]
|
96
|
+
if rest.size == 0
|
97
|
+
return str, ''
|
98
|
+
end
|
99
|
+
while rest[0] != ?| and rest[0] != ?]
|
100
|
+
token, rest = read_one(rest)
|
101
|
+
if rest.size == 0
|
102
|
+
return str, ''
|
103
|
+
end
|
104
|
+
arg << token
|
105
|
+
end
|
106
|
+
if arg.size == 0
|
107
|
+
args << ''
|
108
|
+
elsif arg.size == 1
|
109
|
+
args << arg.first
|
110
|
+
else
|
111
|
+
args << arg
|
112
|
+
end
|
113
|
+
|
114
|
+
end
|
115
|
+
return {:method=>method,:args=>args},rest[1..-1]
|
116
|
+
elsif match = END_REGEXP.match(str)
|
117
|
+
if match.pre_match.size == 0
|
118
|
+
return str[0...1], str[1..-1]
|
119
|
+
end
|
120
|
+
return match.pre_match, match[0] + match.post_match
|
121
|
+
end
|
122
|
+
return str, ''
|
123
|
+
end
|
124
|
+
|
125
|
+
end
|
126
|
+
|
127
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
# This program is free software: you can redistribute it and/or modify
|
3
|
+
# it under the terms of the Affero GNU General Public License as published by
|
4
|
+
# the Free Software Foundation, either version 3 of the License, or
|
5
|
+
# (at your option) any later version.
|
6
|
+
#
|
7
|
+
# This program is distributed in the hope that it will be useful,
|
8
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
9
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
10
|
+
# GNU General Public License for more details.
|
11
|
+
#
|
12
|
+
# You should have received a copy of the GNU General Public License
|
13
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
14
|
+
#
|
15
|
+
# (c) 2011 by Hannes Georg
|
16
|
+
#
|
17
|
+
class Array
|
18
|
+
def _(*args,&block)
|
19
|
+
if self.any?
|
20
|
+
return self[0]._(*self[1..-1])._(*args,&block)
|
21
|
+
else
|
22
|
+
Humanized::Scope::None
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
# This program is free software: you can redistribute it and/or modify
|
3
|
+
# it under the terms of the Affero GNU General Public License as published by
|
4
|
+
# the Free Software Foundation, either version 3 of the License, or
|
5
|
+
# (at your option) any later version.
|
6
|
+
#
|
7
|
+
# This program is distributed in the hope that it will be useful,
|
8
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
9
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
10
|
+
# GNU General Public License for more details.
|
11
|
+
#
|
12
|
+
# You should have received a copy of the GNU General Public License
|
13
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
14
|
+
#
|
15
|
+
# (c) 2011 by Hannes Georg
|
16
|
+
#
|
17
|
+
class Date
|
18
|
+
def _(*args,&block)
|
19
|
+
self.humanization_key.with_variables(:time=>self.dup.freeze, :format =>:default).with_default('[date|%time|%format]')._(*args,&block)
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
# This program is free software: you can redistribute it and/or modify
|
3
|
+
# it under the terms of the Affero GNU General Public License as published by
|
4
|
+
# the Free Software Foundation, either version 3 of the License, or
|
5
|
+
# (at your option) any later version.
|
6
|
+
#
|
7
|
+
# This program is distributed in the hope that it will be useful,
|
8
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
9
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
10
|
+
# GNU General Public License for more details.
|
11
|
+
#
|
12
|
+
# You should have received a copy of the GNU General Public License
|
13
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
14
|
+
#
|
15
|
+
# (c) 2011 by Hannes Georg
|
16
|
+
#
|
17
|
+
class Hash
|
18
|
+
def _(*args,&block)
|
19
|
+
if self.class == Hash
|
20
|
+
Humanized::Scope::None._(*args,&block).with_variables(self)
|
21
|
+
else
|
22
|
+
super
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
# This program is free software: you can redistribute it and/or modify
|
3
|
+
# it under the terms of the Affero GNU General Public License as published by
|
4
|
+
# the Free Software Foundation, either version 3 of the License, or
|
5
|
+
# (at your option) any later version.
|
6
|
+
#
|
7
|
+
# This program is distributed in the hope that it will be useful,
|
8
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
9
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
10
|
+
# GNU General Public License for more details.
|
11
|
+
#
|
12
|
+
# You should have received a copy of the GNU General Public License
|
13
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
14
|
+
#
|
15
|
+
# (c) 2011 by Hannes Georg
|
16
|
+
#
|
17
|
+
class Module
|
18
|
+
|
19
|
+
# Generates a {Humanized::Scope scope} for a Module or Class. This will be used by default by
|
20
|
+
# this Module and by all Objects of this Class.
|
21
|
+
def humanization_key!
|
22
|
+
if self.anonymous?
|
23
|
+
return self.superclass.humanization_key
|
24
|
+
end
|
25
|
+
h = self.home
|
26
|
+
if h != Object and h.respond_to? :humanization_key
|
27
|
+
result = h.humanization_key + self.basename.downcase.to_sym
|
28
|
+
else
|
29
|
+
result = Humanized::Scope::Root.+(*self.name.split('::').map{|s| s.downcase.to_sym })
|
30
|
+
end
|
31
|
+
thiz = self
|
32
|
+
if defined? thiz.superclass and self.superclass != Object
|
33
|
+
return result | self.superclass.humanization_key
|
34
|
+
end
|
35
|
+
return result
|
36
|
+
end
|
37
|
+
|
38
|
+
# Like {Module#humanization_key!}, but cached.
|
39
|
+
def humanization_key
|
40
|
+
@humanization_key ||= humanization_key!
|
41
|
+
end
|
42
|
+
|
43
|
+
def _(*args,&block)
|
44
|
+
humanization_key._(*args,&block)
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
# This program is free software: you can redistribute it and/or modify
|
3
|
+
# it under the terms of the Affero GNU General Public License as published by
|
4
|
+
# the Free Software Foundation, either version 3 of the License, or
|
5
|
+
# (at your option) any later version.
|
6
|
+
#
|
7
|
+
# This program is distributed in the hope that it will be useful,
|
8
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
9
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
10
|
+
# GNU General Public License for more details.
|
11
|
+
#
|
12
|
+
# You should have received a copy of the GNU General Public License
|
13
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
14
|
+
#
|
15
|
+
# (c) 2011 by Hannes Georg
|
16
|
+
#
|
17
|
+
class NilClass
|
18
|
+
def _(*args,&block)
|
19
|
+
Humanized::Scope::None._(*args,&block)
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
# This program is free software: you can redistribute it and/or modify
|
3
|
+
# it under the terms of the Affero GNU General Public License as published by
|
4
|
+
# the Free Software Foundation, either version 3 of the License, or
|
5
|
+
# (at your option) any later version.
|
6
|
+
#
|
7
|
+
# This program is distributed in the hope that it will be useful,
|
8
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
9
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
10
|
+
# GNU General Public License for more details.
|
11
|
+
#
|
12
|
+
# You should have received a copy of the GNU General Public License
|
13
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
14
|
+
#
|
15
|
+
# (c) 2011 by Hannes Georg
|
16
|
+
#
|
17
|
+
class Numeric
|
18
|
+
def _(*args,&block)
|
19
|
+
self.humanization_key.with_variables(:number=>self, :format =>:default).with_default('[number|%number|%format]')._(*args,&block)
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
# This program is free software: you can redistribute it and/or modify
|
3
|
+
# it under the terms of the Affero GNU General Public License as published by
|
4
|
+
# the Free Software Foundation, either version 3 of the License, or
|
5
|
+
# (at your option) any later version.
|
6
|
+
#
|
7
|
+
# This program is distributed in the hope that it will be useful,
|
8
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
9
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
10
|
+
# GNU General Public License for more details.
|
11
|
+
#
|
12
|
+
# You should have received a copy of the GNU General Public License
|
13
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
14
|
+
#
|
15
|
+
# (c) 2011 by Hannes Georg
|
16
|
+
#
|
17
|
+
class Object
|
18
|
+
def humanization_key
|
19
|
+
self.class.humanization_key
|
20
|
+
end
|
21
|
+
def _(*args,&block)
|
22
|
+
self.humanization_key._(*args,&block)
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
# This program is free software: you can redistribute it and/or modify
|
3
|
+
# it under the terms of the Affero GNU General Public License as published by
|
4
|
+
# the Free Software Foundation, either version 3 of the License, or
|
5
|
+
# (at your option) any later version.
|
6
|
+
#
|
7
|
+
# This program is distributed in the hope that it will be useful,
|
8
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
9
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
10
|
+
# GNU General Public License for more details.
|
11
|
+
#
|
12
|
+
# You should have received a copy of the GNU General Public License
|
13
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
14
|
+
#
|
15
|
+
# (c) 2011 by Hannes Georg
|
16
|
+
#
|
17
|
+
class String
|
18
|
+
def _(*args,&block)
|
19
|
+
Humanized::Scope::None._(*args,&block).with_default(self)
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
# This program is free software: you can redistribute it and/or modify
|
3
|
+
# it under the terms of the Affero GNU General Public License as published by
|
4
|
+
# the Free Software Foundation, either version 3 of the License, or
|
5
|
+
# (at your option) any later version.
|
6
|
+
#
|
7
|
+
# This program is distributed in the hope that it will be useful,
|
8
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
9
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
10
|
+
# GNU General Public License for more details.
|
11
|
+
#
|
12
|
+
# You should have received a copy of the GNU General Public License
|
13
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
14
|
+
#
|
15
|
+
# (c) 2011 by Hannes Georg
|
16
|
+
#
|
17
|
+
class Symbol
|
18
|
+
def _(*args,&block)
|
19
|
+
Humanized::Scope.new([[self]])._(*args,&block)
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
# This program is free software: you can redistribute it and/or modify
|
3
|
+
# it under the terms of the Affero GNU General Public License as published by
|
4
|
+
# the Free Software Foundation, either version 3 of the License, or
|
5
|
+
# (at your option) any later version.
|
6
|
+
#
|
7
|
+
# This program is distributed in the hope that it will be useful,
|
8
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
9
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
10
|
+
# GNU General Public License for more details.
|
11
|
+
#
|
12
|
+
# You should have received a copy of the GNU General Public License
|
13
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
14
|
+
#
|
15
|
+
# (c) 2011 by Hannes Georg
|
16
|
+
#
|
17
|
+
class Time
|
18
|
+
def _(*args,&block)
|
19
|
+
self.humanization_key.with_variables(:time=>self.dup.freeze, :format =>:default).with_default('[date|%time|%format]')._(*args,&block)
|
20
|
+
end
|
21
|
+
end
|