odm 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6d0492cc264952b0257f26144c4d44845b4e4772
4
- data.tar.gz: 67517e6cf32613e3b19c3533fa17fd10f566be68
3
+ metadata.gz: 9518ac3935b8d592921d9860e3910b06c1c4c2e7
4
+ data.tar.gz: dc28868ab840a32090382e233b70ca3c46a312af
5
5
  SHA512:
6
- metadata.gz: 7bd7befd4a122576f1ba3ea5a6c8d9572f6faf300d6cb4fef551832c9c3ea6c887c5883a8370d84134c2a74459a4a86a1e34ef0e8b4159515e2ca7d26674492f
7
- data.tar.gz: a13ddc9a3d73eb680763402b618e3862cd3eccc91ef2c123c8e62f7e6cbc59cfd89d66700a76ceef6231800868e4e0b59e83c9197f09e0a38e6beae93273e45b
6
+ metadata.gz: dd37c2a91f436337dedb2576ba0014b1b0aa60554d31608b887f9450b54ec118fba1f864cd87df409313d93d758b38907dee10761260cf5438beff9403b1a2df
7
+ data.tar.gz: 4768bc4d5dbe749a7fe49926860855cb553475926c69324432e5393aacae613004157a036c066fa96d68c1718ea8e4ade655240faf4382b2ee1f0efd2f217b08
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.3.0
data/examples/level.rb ADDED
@@ -0,0 +1,31 @@
1
+ require_relative "../lib/odm"
2
+
3
+ class ARRAYDEF < Array
4
+ end
5
+
6
+ class ARRAY1 < Array
7
+ end
8
+
9
+ class ARRAY2 < Array
10
+ end
11
+
12
+ #> for deepness level 0 | for deepness level 1
13
+ odm= ODM.new( { array: ARRAY1 }, { array: ARRAY2 } ) do |opts|
14
+
15
+ # defaults for else places (if block not given, no parse will be made)
16
+ opts.array= ARRAYDEF
17
+
18
+ end
19
+ #> else -> use core classes
20
+
21
+
22
+ obj= [[[]]]
23
+ var= odm.parse obj
24
+
25
+
26
+ puts obj.inspect
27
+ puts [obj.class,obj[0].class,obj[0][0].class].join(', '),"\n"
28
+
29
+ puts var.inspect
30
+ puts [var.class,var[0].class,var[0][0].class].join(', ')
31
+
@@ -16,7 +16,8 @@ odm= ODM.new do |opts|
16
16
  end
17
17
 
18
18
  var= odm.parse [ {hello: {"world" => "hey!"} }, 123 ]
19
- puts [var.class,var[0].class,var[0][:hello].keys[0].class].join(', ')
19
+ puts [var[0][:hello]["world"].class,var.class,var[0].class,var[0][:hello].keys[0].class].join(', ')
20
+
20
21
 
21
22
  require 'yaml'
22
23
  yaml_data= [ {hello: {"world" => "hey!"} }, 123 ].to_yaml
data/lib/odm.rb CHANGED
@@ -38,7 +38,7 @@ module ODM
38
38
  define_method(core_class.to_s.downcase+"=") do |klass|
39
39
  raise(ArgumentError,"Invalid object for class: #{klass}") unless klass.class == ::Class
40
40
  raise(ArgumentError,"Invalid parse class: #{klass}, must be or subclass of the #{core_class} class") unless klass <= core_class
41
- self.instance_variable_set( "@#{core_class.to_s.downcase}", klass )
41
+ self.instance_variable_set( "@#{core_class.to_s.downcase}", klass ) unless self.instance_variable_get("@#{core_class.to_s.downcase}") == klass
42
42
  end
43
43
 
44
44
  define_method(core_class.to_s.downcase) do
@@ -47,43 +47,97 @@ module ODM
47
47
 
48
48
  end
49
49
 
50
+ def __default__
51
+ ODM::CoreClasses.each{ |klass| self.instance_variable_set( "@#{klass.to_s.downcase}", nil ) }
52
+ __block__
53
+ end
54
+
55
+ def __block__
56
+ @block.call(self) unless @block.nil?
57
+ end
58
+
50
59
  def method_missing *args
51
60
  return nil
52
61
  end
53
62
 
54
- def initialize &block
55
- block.call(self)
63
+ def class_leveler(level)
64
+
65
+ self.__default__
66
+ unless @class_levels.empty?
67
+ unless @class_levels[level].nil?
68
+ @class_levels[level].each{|from_class,to_class| self.__send__( "#{from_class}=",to_class) }
69
+ end
70
+ end
71
+
72
+ end
73
+
74
+ def initialize *args, &block
75
+
76
+ @class_levels= args.select{|e|(e.class <= Hash)}.select{|hsh|(hsh.keys - ODM::CoreClasses.map{|e|e.to_s.downcase.to_sym}).empty?}
77
+ @block= block
78
+
79
+ __block__
80
+
56
81
  end
57
82
 
58
83
  end
59
84
 
60
85
  class Parser
61
86
 
62
- def initialize &block
63
- @class= Classes.new(&block)
87
+ def initialize *args, &block
88
+
89
+ @classes_instance= Classes.new(*args,&block)
90
+
91
+ #> set deepness level to 1
92
+ @level= 0
93
+
64
94
  end
65
95
 
66
- def parse!(obj)
96
+
97
+ def parse(obj,*args,&block)
98
+
99
+ # duplicate obj
100
+ begin
101
+ obj= obj.dup
102
+ rescue Exception
103
+ begin
104
+ obj= obj.clone
105
+ rescue TypeError
106
+ end
107
+ end
108
+
109
+
110
+ @classes_instance.class_leveler(@level)
67
111
 
68
112
  case ODM::Helper.class?( obj.class ).to_s.downcase.to_sym
69
113
 
70
114
  when :array
71
- var= @class.__send__(ODM::Helper.class?( obj.class ).to_s.downcase)[ *obj.map!{ |e| self.parse!(e) } ]
72
- begin; var.__send__( :initialize, obj ) ;rescue ::NoMethodError,::ArgumentError ;end
115
+ klass= @classes_instance.__send__(ODM::Helper.class?( obj.class ).to_s.downcase)
116
+ return obj if klass.nil?
117
+
118
+ @level += 1
119
+ var= klass[ *obj.map!{ |e| self.parse(e) } ]
120
+ @level -= 1
121
+
73
122
  return var
74
123
 
75
124
  when :hash
76
- var= @class.__send__(ODM::Helper.class?( obj.class ).to_s.downcase)[ *obj.map{ |k,v| { self.parse!(k) => self.parse!(v) } } ]
77
- begin; var.__send__( :initialize, obj ) ;rescue ::NoMethodError,::ArgumentError ;end
125
+ klass= @classes_instance.__send__(ODM::Helper.class?( obj.class ).to_s.downcase)
126
+ return obj if klass.nil?
127
+
128
+ @level += 1
129
+ var= klass[ *obj.map{ |k,v| { self.parse(k) => self.parse(v) } } ]
130
+ @level -= 1
131
+
78
132
  return var
79
133
 
80
134
  else
81
135
 
82
136
  begin
83
137
 
84
- klass= @class.__send__(ODM::Helper.class?( obj.class ).to_s.downcase)
138
+ klass= @classes_instance.__send__(ODM::Helper.class?( obj.class ).to_s.downcase)
85
139
  return obj if klass.nil?
86
- return klass.new(obj)
140
+ return klass.new(obj) #> ,*args,&block
87
141
 
88
142
  rescue ::NoMethodError, ::ArgumentError
89
143
  return obj
@@ -93,36 +147,40 @@ module ODM
93
147
 
94
148
  end
95
149
 
96
- def parse(obj)
97
- self.parse!(obj.dup)
98
- end
99
150
 
100
- def parse_json(obj)
151
+ def parse_json(obj,*args)
101
152
  require 'json'
102
- self.parse(JSON.parse(obj))
153
+ self.parse(JSON.parse(obj),*args,&block)
103
154
  end
104
155
 
105
- def parse_yaml(obj)
156
+ def parse_yaml(obj,*args)
106
157
  require 'yaml'
107
- self.parse(YAML.load(obj))
158
+ self.parse(YAML.load(obj),*args,&block)
108
159
  end
109
160
 
110
161
  alias parse_yml parse_yaml
111
162
 
112
- def parse_safe_yaml(obj)
163
+ def parse_safe_yaml(obj,*args)
113
164
  require 'yaml'
114
- self.parse(YAML.safe_load(obj))
165
+ self.parse(YAML.safe_load(obj),*args,&block)
115
166
  end
116
167
 
117
168
  alias parse_safe_yml parse_safe_yaml
118
169
 
170
+ def test obj
171
+
172
+ require 'mpatch'
173
+ @classes_instance.conv2hash
174
+
175
+ end
176
+
119
177
  end
120
178
 
121
179
  # SyntaxSugar
122
180
  class << self
123
181
 
124
- def new(&block)
125
- Parser.new(&block)
182
+ def new(*args,&block)
183
+ Parser.new(*args,&block)
126
184
  end
127
185
 
128
186
  alias gen new
data/odm.gemspec CHANGED
@@ -9,6 +9,8 @@ Gem::Specification.new do |spec|
9
9
  spec.description = %q{ Simple Object Data Mapper. Parse Array of Hash into random Class of classes to make it more object oriented }
10
10
  spec.summary = %q{ Simple Object Data Mapper }
11
11
 
12
+ spec.homepage = "https://github.com/adamluzsi/odm"
13
+
12
14
  spec.files = `git ls-files`.split($/)
13
15
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
14
16
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: odm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Luzsi
@@ -52,10 +52,11 @@ files:
52
52
  - README.md
53
53
  - Rakefile
54
54
  - VERSION
55
- - examples/test.rb
55
+ - examples/level.rb
56
+ - examples/parse.rb
56
57
  - lib/odm.rb
57
58
  - odm.gemspec
58
- homepage:
59
+ homepage: https://github.com/adamluzsi/odm
59
60
  licenses: []
60
61
  metadata: {}
61
62
  post_install_message: