humanized 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/humanized/humanizer.rb +1 -15
- data/lib/humanized/query.rb +22 -0
- data/lib/more/humanized/has_language_code.rb +36 -0
- data/lib/more/humanized/has_parser.rb +78 -0
- data/lib/more/humanized/localized.rb +32 -0
- data/lib/more/humanized/parser/numeric.rb +2 -2
- data/lib/more/humanized/predefined_packages.rb +54 -0
- data/lib/more/humanized/ruby_compiler.rb +52 -0
- data/lib/more/humanized/uses_calendar.rb +50 -0
- metadata +15 -10
- data/lib/more/humanized/parsing_humanizer.rb +0 -73
data/lib/humanized/humanizer.rb
CHANGED
@@ -223,21 +223,7 @@ RB
|
|
223
223
|
# @return [String]
|
224
224
|
def [](*args)
|
225
225
|
it = args._
|
226
|
-
|
227
|
-
vars = it.variables
|
228
|
-
default = it.default
|
229
|
-
result = @source.get(it, :default=>default, :accepts=>IS_STRING)
|
230
|
-
result = default unless result.kind_of? String
|
231
|
-
if result.kind_of? String
|
232
|
-
return interpolate(result,vars)
|
233
|
-
else
|
234
|
-
if logger
|
235
|
-
logger.error do
|
236
|
-
"Expected to retrieve a String, but got: #{result.inspect}\n\tQuery: #{it.inspect}"
|
237
|
-
end
|
238
|
-
end
|
239
|
-
return ""
|
240
|
-
end
|
226
|
+
return it.to_humanized(self)
|
241
227
|
end
|
242
228
|
|
243
229
|
# Stores a translation
|
data/lib/humanized/query.rb
CHANGED
@@ -283,6 +283,28 @@ module Humanized
|
|
283
283
|
return self
|
284
284
|
end
|
285
285
|
|
286
|
+
IS_STRING = lambda{|x| x.kind_of? String }
|
287
|
+
|
288
|
+
def to_humanized(humanizer)
|
289
|
+
vars = self.variables
|
290
|
+
if vars[:self].respond_to? :to_humanized
|
291
|
+
return vars[:self].to_humanized(humanizer)
|
292
|
+
end
|
293
|
+
default = self.default
|
294
|
+
result = humanizer.source.get(self, :default=>default, :accepts=>IS_STRING)
|
295
|
+
result = default unless result.kind_of? String
|
296
|
+
if result.kind_of? String
|
297
|
+
return humanizer.interpolate(result,vars)
|
298
|
+
else
|
299
|
+
if humanizer.logger
|
300
|
+
humanizer.logger.error do
|
301
|
+
"Expected to retrieve a String, but got: #{result.inspect}\n\tQuery: #{it.path.inspect}"
|
302
|
+
end
|
303
|
+
end
|
304
|
+
return ""
|
305
|
+
end
|
306
|
+
end
|
307
|
+
|
286
308
|
Root = self.new([[]],1)
|
287
309
|
None = self.new([],0)
|
288
310
|
Meta = self.new([[:__meta__]],1)
|
@@ -0,0 +1,36 @@
|
|
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
|
+
module Humanized
|
18
|
+
|
19
|
+
module HasLanguageCode
|
20
|
+
|
21
|
+
def self.included(base)
|
22
|
+
puts "language code added!"
|
23
|
+
base.class_eval do
|
24
|
+
component :language_code
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.extended(object)
|
29
|
+
|
30
|
+
included( (class << object; self; end) )
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
@@ -0,0 +1,78 @@
|
|
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
|
+
module Humanized
|
18
|
+
module HasParser
|
19
|
+
|
20
|
+
def self.included(base)
|
21
|
+
|
22
|
+
base.class_eval do
|
23
|
+
|
24
|
+
component :parser do |value, old|
|
25
|
+
|
26
|
+
if old.kind_of? Hash
|
27
|
+
result = old.dup
|
28
|
+
else
|
29
|
+
result = {}
|
30
|
+
end
|
31
|
+
|
32
|
+
if value.kind_of? Hash
|
33
|
+
result.update value
|
34
|
+
elsif value.kind_of? Array
|
35
|
+
value.each do |parser|
|
36
|
+
if parser.respond_to? :provides
|
37
|
+
parser.provides.each do |provided|
|
38
|
+
result[provided] = parser
|
39
|
+
end
|
40
|
+
else
|
41
|
+
raise ArgumentError, "A parser should respond to :provides, got #{parser.inspect}."
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
result
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
def parse(type, string, options={})
|
54
|
+
|
55
|
+
options = options.dup
|
56
|
+
options[:humanizer] = self
|
57
|
+
options[:type] = type
|
58
|
+
|
59
|
+
p = parser[type]
|
60
|
+
|
61
|
+
if p.nil?
|
62
|
+
result = Parser::ParserMissing.new(string, options)
|
63
|
+
else
|
64
|
+
result = p.parse(string, options)
|
65
|
+
end
|
66
|
+
|
67
|
+
raise "Expected result to be a kind of Humanized::Parser::Result but #{result.inspect} given." unless result.kind_of? Parser::Result
|
68
|
+
|
69
|
+
if block_given? and result.success?
|
70
|
+
yield result.value
|
71
|
+
end
|
72
|
+
|
73
|
+
return result
|
74
|
+
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,32 @@
|
|
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
|
+
module Humanized
|
18
|
+
|
19
|
+
class Localized < Hash
|
20
|
+
|
21
|
+
def to_humanized(humanizer)
|
22
|
+
lc = humanizer.language_code
|
23
|
+
return self[lc]
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
=begin
|
28
|
+
def Localized(klass)
|
29
|
+
klass::Localized ||= Class.new(Localized)
|
30
|
+
end
|
31
|
+
=end
|
32
|
+
end
|
@@ -0,0 +1,54 @@
|
|
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
|
+
module Humanized
|
18
|
+
|
19
|
+
module PredefinedPackages
|
20
|
+
|
21
|
+
def predefined_packages
|
22
|
+
@predefined_packages ||= {}
|
23
|
+
end
|
24
|
+
|
25
|
+
def define(name, &block)
|
26
|
+
raise ArgumentError, "define requires a block" unless block_given?
|
27
|
+
@sync.synchronize(Sync::EX){
|
28
|
+
if predefined_packages.key? name
|
29
|
+
raise ArgumentError, "Package already defined: '#{name}'"
|
30
|
+
end
|
31
|
+
predefined_packages[name] = block
|
32
|
+
}
|
33
|
+
end
|
34
|
+
|
35
|
+
def use(*names)
|
36
|
+
unloadeable = []
|
37
|
+
names.each do |name|
|
38
|
+
if predefined_packages.key? name
|
39
|
+
package(name) do
|
40
|
+
self.instance_eval( &predefined_packages[name] )
|
41
|
+
end
|
42
|
+
else
|
43
|
+
unloadeable << name
|
44
|
+
end
|
45
|
+
end
|
46
|
+
if unloadable.any?
|
47
|
+
raise ArgumentError, "Trying to load undefined packages: '#{unloadable.join ', '}'"
|
48
|
+
end
|
49
|
+
return self
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
@@ -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
|
+
module Humanized
|
18
|
+
|
19
|
+
class RubyCompiler < CompilerBase
|
20
|
+
|
21
|
+
class Environment
|
22
|
+
|
23
|
+
def initialize(humanizer, variables)
|
24
|
+
@humanizer = humanizer
|
25
|
+
@variables = variables
|
26
|
+
end
|
27
|
+
|
28
|
+
def method_missing(name, *args, &block)
|
29
|
+
if @variables.key? name
|
30
|
+
return @variables[name]
|
31
|
+
else
|
32
|
+
return eval("@humanizer.interpolater.object.#{name.to_s}(@humanizer,*args,&block)")
|
33
|
+
end
|
34
|
+
super
|
35
|
+
end
|
36
|
+
|
37
|
+
def __eval__(str)
|
38
|
+
eval(str)
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
protected
|
44
|
+
def compile!(str)
|
45
|
+
return Compiled.new(str){|humanizer, variables|
|
46
|
+
Environment.new(humanizer, variables).__eval__('%{'+str+'}')
|
47
|
+
}
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
@@ -0,0 +1,50 @@
|
|
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
|
+
|
20
|
+
module UsesCalendar
|
21
|
+
|
22
|
+
class << self
|
23
|
+
|
24
|
+
def included(base)
|
25
|
+
|
26
|
+
raise ArgumentError, "You can only include UsesCalendar in a Humanizer" unless base <= Humanizer
|
27
|
+
|
28
|
+
base.class_eval do
|
29
|
+
|
30
|
+
component :calendar do |value, old|
|
31
|
+
|
32
|
+
value || old || GregorianCalendar.new
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
def extended(object)
|
41
|
+
|
42
|
+
included( (class << object; self; end) )
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: humanized
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2011-10-09 00:00:00.000000000Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: facets
|
16
|
-
requirement: &
|
16
|
+
requirement: &10447780 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *10447780
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rspec
|
27
|
-
requirement: &
|
27
|
+
requirement: &10445680 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *10445680
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: simplecov
|
38
|
-
requirement: &
|
38
|
+
requirement: &10444840 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *10444840
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: rake
|
49
|
-
requirement: &
|
49
|
+
requirement: &10441420 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,19 +54,24 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *10441420
|
58
58
|
description: Sick of writing culture dependent code? humanized could be for you.
|
59
59
|
email: hannes.georg@googlemail.com
|
60
60
|
executables: []
|
61
61
|
extensions: []
|
62
62
|
extra_rdoc_files: []
|
63
63
|
files:
|
64
|
+
- lib/more/humanized/has_language_code.rb
|
65
|
+
- lib/more/humanized/ruby_compiler.rb
|
66
|
+
- lib/more/humanized/uses_calendar.rb
|
64
67
|
- lib/more/humanized/yaml_source.rb
|
68
|
+
- lib/more/humanized/localized.rb
|
69
|
+
- lib/more/humanized/predefined_packages.rb
|
65
70
|
- lib/more/humanized/parser.rb
|
66
71
|
- lib/more/humanized/parser/date.rb
|
67
72
|
- lib/more/humanized/parser/numeric.rb
|
68
73
|
- lib/more/humanized/json_source.rb
|
69
|
-
- lib/more/humanized/
|
74
|
+
- lib/more/humanized/has_parser.rb
|
70
75
|
- lib/humanized/humanizer.rb
|
71
76
|
- lib/humanized/compiler.rb
|
72
77
|
- lib/humanized/query.rb
|
@@ -1,73 +0,0 @@
|
|
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
|
-
require 'humanized/humanizer'
|
18
|
-
require 'more/humanized/parser'
|
19
|
-
|
20
|
-
module Humanized
|
21
|
-
|
22
|
-
class ParsingHumanizer < Humanizer
|
23
|
-
|
24
|
-
component :parser do |value, old|
|
25
|
-
|
26
|
-
if old.kind_of? Hash
|
27
|
-
result = old.dup
|
28
|
-
else
|
29
|
-
result = {}
|
30
|
-
end
|
31
|
-
|
32
|
-
if value.kind_of? Hash
|
33
|
-
result.update value
|
34
|
-
elsif value.kind_of? Array
|
35
|
-
value.each do |parser|
|
36
|
-
if parser.respond_to? :provides
|
37
|
-
parser.provides.each do |provided|
|
38
|
-
result[provided] = parser
|
39
|
-
end
|
40
|
-
else
|
41
|
-
raise ArgumentError, "A parser should respond to :provides, got #{parser.inspect}."
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
result
|
46
|
-
end
|
47
|
-
|
48
|
-
def parse(type, string, options={})
|
49
|
-
|
50
|
-
options = options.dup
|
51
|
-
options[:humanizer] = self
|
52
|
-
options[:type] = type
|
53
|
-
|
54
|
-
p = parser[type]
|
55
|
-
|
56
|
-
if p.nil?
|
57
|
-
result = Parser::ParserMissing.new(string, options)
|
58
|
-
else
|
59
|
-
result = p.parse(string, options)
|
60
|
-
end
|
61
|
-
|
62
|
-
raise "Expected result to be a kind of Humanized::Parser::Result but #{result.inspect} given." unless result.kind_of? Parser::Result
|
63
|
-
|
64
|
-
if block_given? and result.success?
|
65
|
-
yield result.value
|
66
|
-
end
|
67
|
-
|
68
|
-
return result
|
69
|
-
|
70
|
-
end
|
71
|
-
|
72
|
-
end
|
73
|
-
end
|