odm 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +54 -7
  3. data/VERSION +1 -1
  4. data/examples/level.rb +6 -4
  5. data/lib/odm.rb +50 -24
  6. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9518ac3935b8d592921d9860e3910b06c1c4c2e7
4
- data.tar.gz: dc28868ab840a32090382e233b70ca3c46a312af
3
+ metadata.gz: 442aa0b448f0dee8bf70643a1e325d46dbe2a72c
4
+ data.tar.gz: c1ce6e3f2412782cf388c08f200a1dedf72c0f3a
5
5
  SHA512:
6
- metadata.gz: dd37c2a91f436337dedb2576ba0014b1b0aa60554d31608b887f9450b54ec118fba1f864cd87df409313d93d758b38907dee10761260cf5438beff9403b1a2df
7
- data.tar.gz: 4768bc4d5dbe749a7fe49926860855cb553475926c69324432e5393aacae613004157a036c066fa96d68c1718ea8e4ade655240faf4382b2ee1f0efd2f217b08
6
+ metadata.gz: f4c5d51f2ce44cbfcb1c4c6b2380e983af6d39940ed6fb32a00ab308dbdd77fbda2c6f88a23f488bd77591c9be94d759fc23931bb51a86d8d3c0a00df5c41937
7
+ data.tar.gz: 3045e9e10c500c495c608363cd76baff55a5ecf2b9d6e326dd6148657f9b5716713258391221e41f35aeddd774db2c2818947a33a21099ccfe94e42fb72c5966
data/README.md CHANGED
@@ -11,7 +11,11 @@ at this point the following classes (and they child's) are implemented:
11
11
  initialize method will be called directly in array and hash classes,
12
12
  but will be exception handled if Argument or NoMethod Error occurs
13
13
 
14
- ### example
14
+ ### Example
15
+
16
+ #### Simple
17
+
18
+ we can simply pass to opts the defaults we want to be used when the parser find an object
15
19
 
16
20
  ```ruby
17
21
 
@@ -26,11 +30,6 @@ but will be exception handled if Argument or NoMethod Error occurs
26
30
  end
27
31
 
28
32
  class HASH < Hash
29
-
30
- def initialize original_obj
31
- # some stuff on init for fun
32
- end
33
-
34
33
  end
35
34
 
36
35
  odm= ODM.new do |opts|
@@ -45,7 +44,55 @@ but will be exception handled if Argument or NoMethod Error occurs
45
44
 
46
45
  ```
47
46
 
48
- yaml and json can be parsed too
47
+ #### Complex
48
+
49
+ ```ruby
50
+
51
+
52
+ class ARRAYDEF < Array
53
+ end
54
+
55
+ class ARRAY1 < Array
56
+ end
57
+
58
+ class ARRAY2 < Array
59
+ end
60
+
61
+ #> for deepness level 0 | for deepness level 1 set instance variable "var" with value: "hello"
62
+ odm= ODM.new( { array: ARRAY1 }, { array: ARRAY2 }, [:var,"hello"] ) do |opts|
63
+
64
+ # defaults for else places (if block not given, no parse will be made)
65
+ opts.array= ARRAYDEF
66
+
67
+ end
68
+
69
+ #> test obj
70
+ obj= [[[]]]
71
+
72
+ var= odm.parse obj
73
+
74
+ #> make an ARRAY1[ ARRAY2[ ARRAYDEF[] ] ]
75
+ #> each object got a var instance variable with a "hello" string
76
+
77
+ ```
78
+
79
+ ### Better to be known
80
+
81
+ when parsing a Hash or an Array object, the default initialize method is [] and not :new
82
+ when parsing else objects the default behavior is trying to call the class :new method with the original object
83
+
84
+ ### YAML and JSON
85
+
86
+ yaml and json can be parsed too:
87
+
88
+ * parse_json
89
+ * parse_yaml
90
+ * or parse_yml
91
+ * parse_safe_yaml
92
+ * or parse_safe_yml
93
+
94
+
95
+ #### Example for yaml parse
49
96
 
50
97
  ```ruby
51
98
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.4.0
data/examples/level.rb CHANGED
@@ -9,23 +9,25 @@ end
9
9
  class ARRAY2 < Array
10
10
  end
11
11
 
12
- #> for deepness level 0 | for deepness level 1
13
- odm= ODM.new( { array: ARRAY1 }, { array: ARRAY2 } ) do |opts|
12
+ #> for deepness level 0 | for deepness level 1 set instance variable "var" with value: "hello"
13
+ odm= ODM.new( { array: ARRAY1 }, { array: ARRAY2 }, [:var,"hello"] ) do |opts|
14
14
 
15
15
  # defaults for else places (if block not given, no parse will be made)
16
16
  opts.array= ARRAYDEF
17
17
 
18
18
  end
19
- #> else -> use core classes
20
19
 
21
20
 
22
21
  obj= [[[]]]
23
22
  var= odm.parse obj
23
+ puts odm.state
24
24
 
25
25
 
26
26
  puts obj.inspect
27
27
  puts [obj.class,obj[0].class,obj[0][0].class].join(', '),"\n"
28
28
 
29
29
  puts var.inspect
30
- puts [var.class,var[0].class,var[0][0].class].join(', ')
30
+ puts [ var.class, var[0].class, var[0][0].class ].join(', ')
31
+
32
+ print "instance variable 'var': ",var[0][0].instance_variable_get("@#{:var}"),"\n\n"
31
33
 
data/lib/odm.rb CHANGED
@@ -87,15 +87,28 @@ module ODM
87
87
  def initialize *args, &block
88
88
 
89
89
  @classes_instance= Classes.new(*args,&block)
90
+ @instance_variables= args.select{|e|(e.class <= ::Array)}.select{|ary|(ary.size == 2)}
90
91
 
91
92
  #> set deepness level to 1
92
93
  @level= 0
93
94
 
94
95
  end
95
96
 
97
+ def level &block
98
+ @level += 1
99
+ var= block.call unless block.nil?
100
+ @level -= 1
101
+ return var
102
+ end
96
103
 
97
104
  def parse(obj,*args,&block)
98
105
 
106
+ get_class= Proc.new do |object|
107
+ klass= @classes_instance.__send__(ODM::Helper.class?( object.class ).to_s.downcase)
108
+ return object if klass.nil?
109
+ klass #> pass klass value
110
+ end
111
+
99
112
  # duplicate obj
100
113
  begin
101
114
  obj= obj.dup
@@ -106,38 +119,48 @@ module ODM
106
119
  end
107
120
  end
108
121
 
109
-
110
122
  @classes_instance.class_leveler(@level)
111
-
112
123
  case ODM::Helper.class?( obj.class ).to_s.downcase.to_sym
113
124
 
114
125
  when :array
115
- klass= @classes_instance.__send__(ODM::Helper.class?( obj.class ).to_s.downcase)
116
- return obj if klass.nil?
117
126
 
118
- @level += 1
119
- var= klass[ *obj.map!{ |e| self.parse(e) } ]
120
- @level -= 1
127
+ klass= get_class.call(obj)
128
+ var= nil
129
+
130
+ return level do
131
+ var= klass[ *obj.map!{ |e| self.parse(e) } ]
132
+ @instance_variables.each do |ary|
133
+ var.instance_variable_set( "@#{ary[0]}",ary[1] )
134
+ end
121
135
 
122
- return var
136
+ var #> return
137
+ end
123
138
 
124
139
  when :hash
125
- klass= @classes_instance.__send__(ODM::Helper.class?( obj.class ).to_s.downcase)
126
- return obj if klass.nil?
127
140
 
128
- @level += 1
129
- var= klass[ *obj.map{ |k,v| { self.parse(k) => self.parse(v) } } ]
130
- @level -= 1
141
+ klass= get_class.call(obj)
142
+ return level do
143
+ var= klass[ *obj.map{ |k,v| { self.parse(k) => self.parse(v) } } ]
131
144
 
132
- return var
145
+ @instance_variables.each do |ary|
146
+ var.instance_variable_set( "@#{ary[0]}",ary[1] )
147
+ end
148
+
149
+ var #> return
150
+ end
133
151
 
134
152
  else
135
153
 
136
154
  begin
137
155
 
138
- klass= @classes_instance.__send__(ODM::Helper.class?( obj.class ).to_s.downcase)
139
- return obj if klass.nil?
140
- return klass.new(obj) #> ,*args,&block
156
+ klass= get_class.call(obj)
157
+ var= klass.new(obj) #> ,*args,&block
158
+
159
+ @instance_variables.each do |ary|
160
+ var.instance_variable_set( "@#{ary[0]}",ary[1] )
161
+ end
162
+
163
+ return var
141
164
 
142
165
  rescue ::NoMethodError, ::ArgumentError
143
166
  return obj
@@ -147,30 +170,33 @@ module ODM
147
170
 
148
171
  end
149
172
 
150
-
151
173
  def parse_json(obj,*args)
152
174
  require 'json'
153
- self.parse(JSON.parse(obj),*args,&block)
175
+ self.parse(JSON.parse(obj),*args)
154
176
  end
155
177
 
156
178
  def parse_yaml(obj,*args)
157
179
  require 'yaml'
158
- self.parse(YAML.load(obj),*args,&block)
180
+ self.parse(YAML.load(obj),*args)
159
181
  end
160
182
 
161
183
  alias parse_yml parse_yaml
162
184
 
163
185
  def parse_safe_yaml(obj,*args)
164
186
  require 'yaml'
165
- self.parse(YAML.safe_load(obj),*args,&block)
187
+ self.parse(YAML.safe_load(obj),*args)
166
188
  end
167
189
 
168
190
  alias parse_safe_yml parse_safe_yaml
169
191
 
170
- def test obj
192
+ def state
193
+
194
+ tmp_hash= {}
195
+ @classes_instance.instance_variables.each { |ivar|
196
+ tmp_hash.merge!({ ivar.to_s.sub('@','').to_sym => @classes_instance.instance_variable_get(ivar) })
197
+ }
171
198
 
172
- require 'mpatch'
173
- @classes_instance.conv2hash
199
+ return tmp_hash
174
200
 
175
201
  end
176
202
 
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.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Luzsi