humanized 0.0.1.alpha → 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -19,7 +19,12 @@ module Humanized
19
19
  # A source lets you lookup,store and load data needed for humanization.
20
20
  class Source
21
21
 
22
+ VALUE_KEY = :_
23
+
24
+ NOT_NIL_LAMBDA = lambda{|x| !x.nil? }
25
+
22
26
  def initialize(data = {})
27
+ #TODO: data should maybe deep-copied
23
28
  @source = data
24
29
  @sync = Sync.new
25
30
  @loaded = Set.new
@@ -29,11 +34,11 @@ class Source
29
34
  # Loads a data-file or a dir of data-files.
30
35
  #
31
36
  # @param [String] path to a dir or file
32
- # @option opts [Scope] :scope the root scope, where the loaded data will be stored ( default: L )
37
+ # @option opts [Query] :query the root query, where the loaded data will be stored ( default: Root )
33
38
  # @option opts [String] :grep a grep to be used when a dir is given ( default: '**/*.*' )
34
39
  # @return self
35
40
  def load(path,opts ={})
36
- options = {:scope => Scope::Root, :grep => '**/*.*'}.update(opts)
41
+ options = {:query => Query::Root, :grep => '**/*.*'}.update(opts)
37
42
  if File.directory?(path)
38
43
  f = File.join(path,options[:grep])
39
44
  package('grep:' + f) do
@@ -43,8 +48,8 @@ class Source
43
48
  if data
44
49
  xpath = file[path.size..(-1-File.extname(file).size)].split('/')
45
50
  xpath.shift if xpath.first == ''
46
- xscope = options[:scope]._(*xpath.map(&:to_sym))
47
- self.store(xscope.first,data)
51
+ xquery = options[:query]._(*xpath.map(&:to_sym))
52
+ self.store(xquery.first,data)
48
53
  end
49
54
  end
50
55
  end
@@ -53,7 +58,7 @@ class Source
53
58
  package('file:'+path) do
54
59
  data = self.read_file(path)
55
60
  if data
56
- self.store(options[:scope].first,data)
61
+ self.store(options[:query].first,data)
57
62
  end
58
63
  end
59
64
  end
@@ -90,12 +95,14 @@ class Source
90
95
  end
91
96
 
92
97
  # Retrieves data
93
- # @param [Scope, #each] scope a scope containing the paths to search for
98
+ # @param [Query, #each] query a query containing the paths to search for
94
99
  # @return [String, Object, nil] data
95
- def get(scope, default = nil)
96
- scope.each do |path|
97
- result = find(path, @source)
98
- return result unless result.nil?
100
+ def get(query, options = {})
101
+ default = options[:default]
102
+ accepts = options.fetch(:accepts,NOT_NIL_LAMBDA )
103
+ query.each do |path|
104
+ v = @source[path]
105
+ return v if accepts.call(v)
99
106
  end
100
107
  return default
101
108
  end
@@ -104,66 +111,42 @@ class Source
104
111
  # @param [Array] path a path to store the data at
105
112
  # @param [Object] data the data to store
106
113
  def store(path ,data)
107
- store!(path, data)
108
- end
109
-
110
- protected
111
-
112
- def store!(path ,str, hsh = @source)
113
114
  @sync.synchronize(Sync::EX){
114
- hshc = hsh
115
- l = path.length - 1
116
- if str.kind_of? Hash
117
- l += 1
118
- end
119
- (0...l).each do |i|
120
- a = path[i]
121
- unless hshc.key?(a)
122
- hshc[a] = {}
123
- end
124
- hshc = hshc[a]
125
- while hshc.kind_of? Humanized::Ref
126
- hshc = find(hshc, @source)
115
+ if data.kind_of? Hash
116
+ data.each do |key, value|
117
+ if key.kind_of? Array
118
+ store( path + key, value)
119
+ next
120
+ end
121
+ key = key.to_sym
122
+ if key == VALUE_KEY
123
+ store( path, value )
124
+ else
125
+ store( path + [key], value)
126
+ end
127
127
  end
128
- end
129
- if str.kind_of? Hash
130
- hshc.deep_merge!(str)
131
128
  else
132
- hshc[path[l]] = str
129
+ @source[path] = data
133
130
  end
134
- return nil
135
131
  }
136
132
  end
137
133
 
134
+ def inspect
135
+ "#<#{self.class.name}:#{self.object_id.to_s} | #{@source.size} key(s); loaded: #{@loaded.inspect}>"
136
+ end
137
+
138
+ protected
139
+
138
140
  def read_file(file)
139
141
  ext = File.extname(file)[1..-1]
140
142
  meth = "read_#{ext}".to_sym
141
143
  if self.respond_to?(meth)
142
144
  return self.send(meth,file)
143
145
  else
144
- warn "No reader found for #{ext}."
146
+ Humanized.logger.warn "No reader found for #{ext}."
145
147
  return nil
146
148
  end
147
149
  end
148
150
 
149
- def find(path, hsh)
150
- hshc = hsh
151
- l = path.length - 1
152
- (0...l).each do |i|
153
- a = path[i]
154
- return nil unless hshc.key?(a)
155
- hshc = hshc[a]
156
- while hshc.kind_of? Humanized::Ref
157
- hshc = find(hshc, @source)
158
- end
159
- return nil unless hshc.respond_to? :[]
160
- end
161
- hshc = hshc[path[l]]
162
- while hshc.kind_of? Humanized::Ref
163
- hshc = find(hshc, @source)
164
- end
165
- return hshc
166
- end
167
-
168
151
  end
169
152
  end
@@ -0,0 +1,89 @@
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 Parser
20
+
21
+ class Result
22
+
23
+ attr_reader :parsed_string,
24
+ :original_string,
25
+ :value,
26
+ :options,
27
+ :success
28
+
29
+ def success?
30
+ @success
31
+ end
32
+
33
+ def call
34
+ if success?
35
+ yield @value
36
+ end
37
+ end
38
+
39
+ def success
40
+ if block_given?
41
+ yield(@value) if @success
42
+ return self
43
+ end
44
+ @success
45
+ end
46
+
47
+ def failure
48
+ if block_given?
49
+ yield() unless success?
50
+ return self
51
+ end
52
+ !success?
53
+ end
54
+
55
+ def initialize(str, options ={}, &block)
56
+ s = str.dup.freeze
57
+ @original_string = s
58
+ @parsed_string = s
59
+ @options = options
60
+ @success = false
61
+ if block
62
+ instance_eval &block
63
+ end
64
+ end
65
+
66
+ def emit(value, parsed_or_options = nil)
67
+ @value = value
68
+ @success = true
69
+ if parsed_or_options.kind_of? String
70
+ @parsed_string = parsed.dup.freeze
71
+ elsif parsed_or_options.kind_of? Hash
72
+ @options.update parsed_or_options
73
+ end
74
+ return self
75
+ end
76
+
77
+ end
78
+
79
+ class ParserMissing < Result
80
+ end
81
+
82
+ def provides
83
+ []
84
+ end
85
+
86
+
87
+ end
88
+
89
+ end
File without changes
@@ -0,0 +1,80 @@
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 "bigdecimal"
19
+
20
+ module Humanized
21
+ class Parser
22
+ class NumericClass < self
23
+
24
+ def parse(string, options)
25
+
26
+ humanizer = options[:humanizer]
27
+ if options.key? :default
28
+ scope = [:numeric, :format]._[options[:format], :default]
29
+ else
30
+ scope = [:numeric, :format, :default]._
31
+ end
32
+
33
+ separator = humanizer.get(scope.separator)
34
+ delimiter = humanizer.get(scope.delimiter)
35
+ pedantic = options.fetch(:pedantic, false)
36
+
37
+ result = Result.new(string, options)
38
+
39
+
40
+ if pedantic
41
+ regexp = Regexp.new([
42
+ '^(-?)(\d{1,3}(?:',
43
+ Regexp.escape(separator),
44
+ '\d{3})?)(',
45
+ Regexp.escape(delimiter),
46
+ '(\d+))?$'
47
+ ].join)
48
+ else
49
+ regexp = Regexp.new([
50
+ '^(-?)([\d',
51
+ Regexp.escape(separator),
52
+ ']+)(',
53
+ Regexp.escape(delimiter),
54
+ '(\d+))?$'
55
+ ].join)
56
+ end
57
+
58
+ m = regexp.match(string)
59
+
60
+ if m.nil?
61
+ return result
62
+ end
63
+
64
+ result.emit( BigDecimal( [ m[1], m[2].gsub(separator,''), '.', m[4] ].join ), :precision => (m[4] ? m[4].length : 0) )
65
+
66
+ return result
67
+
68
+ end
69
+
70
+ def provides
71
+ return [:numeric]
72
+ end
73
+
74
+
75
+ end
76
+
77
+ Numeric = NumericClass.new
78
+
79
+ end
80
+ end
@@ -0,0 +1,73 @@
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
metadata CHANGED
@@ -1,149 +1,115 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: humanized
3
- version: !ruby/object:Gem::Version
4
- hash: -1851332166
5
- prerelease: 6
6
- segments:
7
- - 0
8
- - 0
9
- - 1
10
- - alpha
11
- version: 0.0.1.alpha
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
12
6
  platform: ruby
13
- authors:
7
+ authors:
14
8
  - HannesG
15
9
  autorequire:
16
10
  bindir: bin
17
11
  cert_chain: []
18
-
19
- date: 2011-03-23 00:00:00 +01:00
20
- default_executable:
21
- dependencies:
22
- - !ruby/object:Gem::Dependency
12
+ date: 2011-10-09 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
23
15
  name: facets
24
- version_requirements: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &8246820 !ruby/object:Gem::Requirement
25
17
  none: false
26
- requirements:
27
- - - ">="
28
- - !ruby/object:Gem::Version
29
- hash: 3
30
- segments:
31
- - 0
32
- version: "0"
33
- prerelease: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
34
22
  type: :runtime
35
- requirement: *id001
36
- - !ruby/object:Gem::Dependency
23
+ prerelease: false
24
+ version_requirements: *8246820
25
+ - !ruby/object:Gem::Dependency
37
26
  name: rspec
38
- version_requirements: &id002 !ruby/object:Gem::Requirement
27
+ requirement: &8246040 !ruby/object:Gem::Requirement
39
28
  none: false
40
- requirements:
41
- - - ">="
42
- - !ruby/object:Gem::Version
43
- hash: 3
44
- segments:
45
- - 0
46
- version: "0"
47
- prerelease: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
48
33
  type: :development
49
- requirement: *id002
50
- - !ruby/object:Gem::Dependency
34
+ prerelease: false
35
+ version_requirements: *8246040
36
+ - !ruby/object:Gem::Dependency
51
37
  name: simplecov
52
- version_requirements: &id003 !ruby/object:Gem::Requirement
38
+ requirement: &8245520 !ruby/object:Gem::Requirement
53
39
  none: false
54
- requirements:
55
- - - ">="
56
- - !ruby/object:Gem::Version
57
- hash: 3
58
- segments:
59
- - 0
60
- version: "0"
61
- prerelease: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
62
44
  type: :development
63
- requirement: *id003
64
- - !ruby/object:Gem::Dependency
45
+ prerelease: false
46
+ version_requirements: *8245520
47
+ - !ruby/object:Gem::Dependency
65
48
  name: rake
66
- version_requirements: &id004 !ruby/object:Gem::Requirement
49
+ requirement: &8245060 !ruby/object:Gem::Requirement
67
50
  none: false
68
- requirements:
69
- - - ">="
70
- - !ruby/object:Gem::Version
71
- hash: 3
72
- segments:
73
- - 0
74
- version: "0"
75
- prerelease: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
76
55
  type: :development
77
- requirement: *id004
56
+ prerelease: false
57
+ version_requirements: *8245060
78
58
  description: Sick of writing culture dependent code? humanized could be for you.
79
59
  email: hannes.georg@googlemail.com
80
60
  executables: []
81
-
82
61
  extensions: []
83
-
84
62
  extra_rdoc_files: []
85
-
86
- files:
63
+ files:
87
64
  - lib/more/humanized/yaml_source.rb
65
+ - lib/more/humanized/parser.rb
66
+ - lib/more/humanized/parser/date.rb
67
+ - lib/more/humanized/parser/numeric.rb
88
68
  - lib/more/humanized/json_source.rb
69
+ - lib/more/humanized/parsing_humanizer.rb
89
70
  - lib/humanized/humanizer.rb
90
71
  - lib/humanized/compiler.rb
91
- - lib/humanized/scope.rb
72
+ - lib/humanized/query.rb
73
+ - lib/humanized/interpolater.rb
92
74
  - lib/humanized/interpolation/date.rb
93
75
  - lib/humanized/interpolation/german.rb
94
76
  - lib/humanized/interpolation/number.rb
95
77
  - lib/humanized/interpolation/english.rb
78
+ - lib/humanized/interpolation/default.rb
96
79
  - lib/humanized/interpolation/kng.rb
97
80
  - lib/humanized/interpolation/conjunctions.rb
98
81
  - lib/humanized/core_ext/hash.rb
99
- - lib/humanized/core_ext/date.rb
100
82
  - lib/humanized/core_ext/array.rb
101
- - lib/humanized/core_ext/time.rb
102
83
  - lib/humanized/core_ext/object.rb
103
84
  - lib/humanized/core_ext/module.rb
104
85
  - lib/humanized/core_ext/string.rb
105
86
  - lib/humanized/core_ext/nilclass.rb
106
- - lib/humanized/core_ext/numeric.rb
107
87
  - lib/humanized/core_ext/symbol.rb
108
88
  - lib/humanized/source.rb
109
- - lib/humanized/ref.rb
110
89
  - lib/humanized/wrapper.rb
111
90
  - lib/humanized.rb
112
- has_rdoc: true
113
91
  homepage: http://github.com/hannesg/humanized
114
92
  licenses: []
115
-
116
93
  post_install_message:
117
94
  rdoc_options: []
118
-
119
- require_paths:
95
+ require_paths:
120
96
  - lib
121
- required_ruby_version: !ruby/object:Gem::Requirement
97
+ required_ruby_version: !ruby/object:Gem::Requirement
122
98
  none: false
123
- requirements:
124
- - - ">="
125
- - !ruby/object:Gem::Version
126
- hash: 3
127
- segments:
128
- - 0
129
- version: "0"
130
- required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ! '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
104
  none: false
132
- requirements:
133
- - - ">"
134
- - !ruby/object:Gem::Version
135
- hash: 25
136
- segments:
137
- - 1
138
- - 3
139
- - 1
140
- version: 1.3.1
105
+ requirements:
106
+ - - ! '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
141
109
  requirements: []
142
-
143
110
  rubyforge_project:
144
- rubygems_version: 1.5.0
111
+ rubygems_version: 1.8.10
145
112
  signing_key:
146
113
  specification_version: 3
147
114
  summary: advanced i18n for ruby
148
115
  test_files: []
149
-