odm 0.1.0.alpha → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +31 -41
- data/VERSION +1 -1
- data/examples/test.rb +21 -19
- data/lib/odm.rb +134 -1
- data/odm.gemspec +1 -1
- metadata +4 -5
- data/lib/odm/ext.rb +0 -26
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d205de950ed17bd916da606c25e3671e5fe7c481
|
4
|
+
data.tar.gz: 52bbdd77947520bf8e56a0e636cb4bf94b092a8d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cd7ac421ce051af891cebae298131e65a8a5efe7bc8d4d72d351c370b2646fa2f36a738598bd366d17918d2ecf0aed4a22595abb8291f312788e21208af767a2
|
7
|
+
data.tar.gz: 8bb06f2211370f03f65673ba4e81d8cea726379af2b260140ad884cf91b621d53421e6e95bc12ec8534ca27e93b01c59870fc8753a9ab936321e9e9cacf292e0
|
data/README.md
CHANGED
@@ -1,64 +1,54 @@
|
|
1
|
-
|
1
|
+
ODM
|
2
2
|
========
|
3
3
|
|
4
|
-
|
5
|
-
|
4
|
+
Simple Object Data Mapper
|
5
|
+
|
6
|
+
Parse Array of Hash into random Class of classes to make it more object oriented on the fly :)
|
6
7
|
|
7
8
|
### example
|
8
9
|
|
9
10
|
```ruby
|
10
11
|
|
11
|
-
require '
|
12
|
+
require 'odm'
|
12
13
|
|
13
|
-
|
14
|
+
class ARRAY < Array
|
14
15
|
|
15
|
-
|
16
|
-
# {"--test"=>"test"}
|
16
|
+
end
|
17
17
|
|
18
|
-
|
19
|
-
# {"--test"=>["test", "this", "ok"]}
|
18
|
+
class HASH < Hash
|
20
19
|
|
21
|
-
|
22
|
-
|
20
|
+
def initialize original_obj
|
21
|
+
# some stuff on init
|
22
|
+
end
|
23
23
|
|
24
|
-
|
25
|
-
# {:test=>["test", "this", "ok"]}
|
24
|
+
end
|
26
25
|
|
27
|
-
|
28
|
-
# ["test", "this", "ok"]
|
26
|
+
class STRING < String
|
29
27
|
|
30
|
-
|
31
|
-
# ["--test"]
|
28
|
+
end
|
32
29
|
|
33
|
-
|
34
|
-
|
30
|
+
odm= ODM.new do |opts|
|
31
|
+
opts.array = ARRAY
|
32
|
+
opts.hash = HASH
|
33
|
+
opts.string = STRING
|
34
|
+
end
|
35
35
|
|
36
|
-
|
36
|
+
var= odm.parse [ {hello: {"world" => "hey!"} }, 123 ]
|
37
|
+
puts var.inspect,var.class,var[0].class,var[0][:hello].keys[0].class
|
38
|
+
#> ARRAY
|
39
|
+
#> HASH
|
40
|
+
#> STRING
|
37
41
|
|
38
|
-
|
42
|
+
```
|
39
43
|
|
40
|
-
|
41
|
-
use the following example
|
44
|
+
yaml and json can be parsed too
|
42
45
|
|
43
46
|
```ruby
|
44
47
|
|
45
|
-
require '
|
46
|
-
|
47
|
-
ARGVEXT.add_help_msg "This will show you the help msg (this)",:helper,:help,:h
|
48
|
-
|
49
|
-
#
|
50
|
-
# ARGVEXT.help_msg or show_help will read on your values and will produce the following with this example
|
51
|
-
#
|
52
|
-
# This will show you the help msg (this)
|
53
|
-
# --helper
|
54
|
-
# --help
|
55
|
-
# -h
|
56
|
-
|
57
|
-
# this will run on the terminal and break Process,
|
58
|
-
# if the user give one of the helper tags as argv
|
59
|
-
|
60
|
-
```
|
48
|
+
require 'yaml'
|
49
|
+
yaml_data= [ {hello: {"world" => "hey!"} }, 123 ].to_yaml
|
61
50
|
|
62
|
-
|
51
|
+
puts odm.parse_yml(yaml_data).inspect
|
52
|
+
#> [{:hello=>{"world"=>"hey!"}}, 123]
|
63
53
|
|
64
|
-
|
54
|
+
```
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.0
|
1
|
+
0.1.0
|
data/examples/test.rb
CHANGED
@@ -1,30 +1,32 @@
|
|
1
|
-
|
1
|
+
require_relative "../lib/odm"
|
2
2
|
|
3
|
-
|
3
|
+
class ARRAY < Array
|
4
4
|
|
5
|
-
|
6
|
-
ARGV.add_help "heeeelp meeeeee~~~",:no
|
5
|
+
end
|
7
6
|
|
8
|
-
|
7
|
+
class HASH < Hash
|
9
8
|
|
10
|
-
|
9
|
+
def initialize obj
|
10
|
+
# puts obj
|
11
|
+
end
|
11
12
|
|
12
|
-
|
13
|
-
# {"--test"=>"test"}
|
13
|
+
end
|
14
14
|
|
15
|
-
|
16
|
-
# {"--test"=>["test", "this", "ok"]}
|
15
|
+
class STRING < String
|
17
16
|
|
18
|
-
|
19
|
-
# {:test=>"test"}
|
17
|
+
end
|
20
18
|
|
21
|
-
|
22
|
-
|
19
|
+
odm= ODM.new do |opts|
|
20
|
+
opts.array = ARRAY
|
21
|
+
opts.hash = HASH
|
22
|
+
opts.string = STRING
|
23
|
+
opts.symbol = SYMBOL
|
24
|
+
end
|
23
25
|
|
24
|
-
|
25
|
-
|
26
|
+
var= odm.parse [ {hello: {"world" => "hey!"} }, 123 ]
|
27
|
+
puts var.inspect,var.class,var[0].class,var[0][:hello].keys[0].class,var[0].keys[0].class
|
26
28
|
|
27
|
-
|
28
|
-
|
29
|
+
require 'yaml'
|
30
|
+
yaml_data= [ {hello: {"world" => "hey!"} }, 123 ].to_yaml
|
29
31
|
|
30
|
-
puts
|
32
|
+
puts odm.parse_yml(yaml_data).inspect
|
data/lib/odm.rb
CHANGED
@@ -1 +1,134 @@
|
|
1
|
-
require '
|
1
|
+
require 'mpatch'
|
2
|
+
|
3
|
+
module ODM
|
4
|
+
|
5
|
+
CoreClasses= [ ::Module, ::Array, ::Hash, ::Regexp, ::String, ::File, ::IO ] #> Fixnum, Float, Integer dont have .new
|
6
|
+
|
7
|
+
module Helper
|
8
|
+
class << self
|
9
|
+
|
10
|
+
# constantize object
|
11
|
+
def constantize self_obj
|
12
|
+
|
13
|
+
camel_cased_word= self_obj.to_s
|
14
|
+
names = camel_cased_word.split('::')
|
15
|
+
names.shift if names.empty? || names.first.empty?
|
16
|
+
|
17
|
+
constant = ::Object
|
18
|
+
names.each do |name|
|
19
|
+
constant = constant.const_defined?(name, false) ? constant.const_get(name) : constant.const_missing(name)
|
20
|
+
end
|
21
|
+
constant
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
def class? klass
|
26
|
+
raise(ArgumentError,"Invalid object for class: #{klass}") unless klass.class == Class
|
27
|
+
ODM::CoreClasses.each do |core_class|
|
28
|
+
return core_class if klass <= core_class
|
29
|
+
end
|
30
|
+
return nil
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
class Classes
|
37
|
+
|
38
|
+
ODM::CoreClasses.each do |core_class|
|
39
|
+
|
40
|
+
define_method(core_class.to_s.downcase+"=") do |klass|
|
41
|
+
raise(ArgumentError,"Invalid object for class: #{klass}") unless klass.class == ::Class
|
42
|
+
raise(ArgumentError,"Invalid parse class: #{klass}") unless klass <= core_class
|
43
|
+
self.instance_variable_set( "@#{core_class.to_s.downcase}", klass )
|
44
|
+
end
|
45
|
+
|
46
|
+
define_method(core_class.to_s.downcase) do
|
47
|
+
self.instance_variable_get("@#{core_class.to_s.downcase}") #> || core_class
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
def method_missing *args
|
53
|
+
return nil
|
54
|
+
end
|
55
|
+
|
56
|
+
def initialize &block
|
57
|
+
block.call(self)
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
class Parser
|
63
|
+
|
64
|
+
def initialize &block
|
65
|
+
@class= Classes.new(&block)
|
66
|
+
end
|
67
|
+
|
68
|
+
def parse!(obj)
|
69
|
+
|
70
|
+
case ODM::Helper.class?( obj.class ).to_s.downcase.to_sym
|
71
|
+
|
72
|
+
when :array
|
73
|
+
return @class.__send__(ODM::Helper.class?( obj.class ).to_s.downcase).new(obj.map{ |e| self.parse!(e) })
|
74
|
+
|
75
|
+
when :hash
|
76
|
+
var= @class.__send__(ODM::Helper.class?( obj.class ).to_s.downcase)[ obj.map_hash{ |k,v| { self.parse!(k) => self.parse!(v) } } ]
|
77
|
+
begin; var.__send__( :initialize, obj ) ;rescue ::NoMethodError,::ArgumentError ;end
|
78
|
+
return var
|
79
|
+
|
80
|
+
else
|
81
|
+
|
82
|
+
begin
|
83
|
+
|
84
|
+
klass= @class.__send__(ODM::Helper.class?( obj.class ).to_s.downcase)
|
85
|
+
return obj if klass.nil?
|
86
|
+
return klass.new(obj)
|
87
|
+
|
88
|
+
rescue ::NoMethodError,::ArgumentError
|
89
|
+
return obj
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
95
|
+
|
96
|
+
def parse(obj)
|
97
|
+
self.parse!(obj.dup)
|
98
|
+
end
|
99
|
+
|
100
|
+
def parse_json(obj)
|
101
|
+
require 'json'
|
102
|
+
self.parse(JSON.parse(obj))
|
103
|
+
end
|
104
|
+
|
105
|
+
def parse_yaml(obj)
|
106
|
+
require 'yaml'
|
107
|
+
self.parse(YAML.load(obj))
|
108
|
+
end
|
109
|
+
|
110
|
+
alias parse_yml parse_yaml
|
111
|
+
|
112
|
+
def parse_safe_yaml(obj)
|
113
|
+
require 'yaml'
|
114
|
+
self.parse(YAML.safe_load(obj))
|
115
|
+
end
|
116
|
+
|
117
|
+
alias parse_safe_yml parse_safe_yaml
|
118
|
+
|
119
|
+
end
|
120
|
+
|
121
|
+
# SyntaxSugar
|
122
|
+
class << self
|
123
|
+
|
124
|
+
def new(&block)
|
125
|
+
Parser.new(&block)
|
126
|
+
end
|
127
|
+
|
128
|
+
alias gen new
|
129
|
+
alias init new
|
130
|
+
alias create new
|
131
|
+
|
132
|
+
end
|
133
|
+
|
134
|
+
end
|
data/odm.gemspec
CHANGED
@@ -6,7 +6,7 @@ Gem::Specification.new do |spec|
|
|
6
6
|
spec.version = File.open(File.join(File.dirname(__FILE__),"VERSION")).read.split("\n")[0].chomp.gsub(' ','')
|
7
7
|
spec.authors = ["Adam Luzsi"]
|
8
8
|
spec.email = ["adamluzsi@gmail.com"]
|
9
|
-
spec.description = %q{ Simple Object Data Mapper.
|
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
12
|
spec.files = `git ls-files`.split($/)
|
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.1.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Luzsi
|
@@ -52,7 +52,7 @@ dependencies:
|
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: 2.12.1
|
55
|
-
description: " Simple Object Data Mapper.
|
55
|
+
description: " Simple Object Data Mapper. Parse Array of Hash into random Class of
|
56
56
|
classes to make it more object oriented "
|
57
57
|
email:
|
58
58
|
- adamluzsi@gmail.com
|
@@ -68,7 +68,6 @@ files:
|
|
68
68
|
- VERSION
|
69
69
|
- examples/test.rb
|
70
70
|
- lib/odm.rb
|
71
|
-
- lib/odm/ext.rb
|
72
71
|
- odm.gemspec
|
73
72
|
homepage:
|
74
73
|
licenses: []
|
@@ -84,9 +83,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
84
83
|
version: '0'
|
85
84
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
85
|
requirements:
|
87
|
-
- - "
|
86
|
+
- - ">="
|
88
87
|
- !ruby/object:Gem::Version
|
89
|
-
version:
|
88
|
+
version: '0'
|
90
89
|
requirements: []
|
91
90
|
rubyforge_project:
|
92
91
|
rubygems_version: 2.2.2
|
data/lib/odm/ext.rb
DELETED
@@ -1,26 +0,0 @@
|
|
1
|
-
module ODM
|
2
|
-
|
3
|
-
module EXT
|
4
|
-
|
5
|
-
def odm_convert obj
|
6
|
-
raise unless self.class == Class
|
7
|
-
|
8
|
-
case true
|
9
|
-
|
10
|
-
when self <= ::Hash
|
11
|
-
|
12
|
-
when self <= ::Array
|
13
|
-
|
14
|
-
when self <= ::Array
|
15
|
-
|
16
|
-
when self <= ::Array
|
17
|
-
when self <= ::Array
|
18
|
-
when self <= ::Array
|
19
|
-
|
20
|
-
end
|
21
|
-
|
22
|
-
end
|
23
|
-
|
24
|
-
end
|
25
|
-
|
26
|
-
end
|