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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1777cc023b2925b282b8258d8bd775fe22346d81
4
- data.tar.gz: 48fafb1d2ca288764ac7795c755ece47a773644d
3
+ metadata.gz: d205de950ed17bd916da606c25e3671e5fe7c481
4
+ data.tar.gz: 52bbdd77947520bf8e56a0e636cb4bf94b092a8d
5
5
  SHA512:
6
- metadata.gz: b1e72fdca127c2ea42adb825653137c8555366411b98d25dedfc33e63e60e58f029e5cc079ac3f27f71c23a24a29993192e8cbd038480ac1305f792924ee74dc
7
- data.tar.gz: bea277b9776ca5ed4e9f6455f84a8887654706dd5dad624ded59aed33c60f870b6f7eb36b9abe88e191180ceddb355c0af376ccb1c249e90300d7151e07ed1f7
6
+ metadata.gz: cd7ac421ce051af891cebae298131e65a8a5efe7bc8d4d72d351c370b2646fa2f36a738598bd366d17918d2ecf0aed4a22595abb8291f312788e21208af767a2
7
+ data.tar.gz: 8bb06f2211370f03f65673ba4e81d8cea726379af2b260140ad884cf91b621d53421e6e95bc12ec8534ca27e93b01c59870fc8753a9ab936321e9e9cacf292e0
data/README.md CHANGED
@@ -1,64 +1,54 @@
1
- ARGV
1
+ ODM
2
2
  ========
3
3
 
4
- Extension for ARGV obj.
5
- With this extension module, you can parse in an easy way the script input tags
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 'argv'
12
+ require 'odm'
12
13
 
13
- #$ ruby sample/test.rb --test test this ok
14
+ class ARRAY < Array
14
15
 
15
- puts ARGV.to_hash
16
- # {"--test"=>"test"}
16
+ end
17
17
 
18
- puts ARGV.to_hash( multi_value: true )
19
- # {"--test"=>["test", "this", "ok"]}
18
+ class HASH < Hash
20
19
 
21
- puts ARGV.to_hash( sym_key: true )
22
- # {:test=>"test"}
20
+ def initialize original_obj
21
+ # some stuff on init
22
+ end
23
23
 
24
- puts ARGV.to_hash( s: true, m: true )
25
- # {:test=>["test", "this", "ok"]}
24
+ end
26
25
 
27
- puts ARGV.values.inspect
28
- # ["test", "this", "ok"]
26
+ class STRING < String
29
27
 
30
- puts ARGV.keys.inspect
31
- # ["--test"]
28
+ end
32
29
 
33
- puts ARGV.flag_syms.inspect
34
- # [:test, :test, :this, :ok]
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
- #### For Modules
42
+ ```
39
43
 
40
- if you write module and you want to have some custom tags and help msg for that,
41
- use the following example
44
+ yaml and json can be parsed too
42
45
 
43
46
  ```ruby
44
47
 
45
- require 'argv'
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
- ### Test
51
+ puts odm.parse_yml(yaml_data).inspect
52
+ #> [{:hello=>{"world"=>"hey!"}}, 123]
63
53
 
64
- * you can find a test file in the sample named as "test"
54
+ ```
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0.alpha
1
+ 0.1.0
data/examples/test.rb CHANGED
@@ -1,30 +1,32 @@
1
- require "odm"
1
+ require_relative "../lib/odm"
2
2
 
3
- #$ ruby sample/test.rb --test test this ok
3
+ class ARRAY < Array
4
4
 
5
- # new help
6
- ARGV.add_help "heeeelp meeeeee~~~",:no
5
+ end
7
6
 
8
- ARGV.show_help
7
+ class HASH < Hash
9
8
 
10
- puts "","original ARGV:",ARGV.inspect,""
9
+ def initialize obj
10
+ # puts obj
11
+ end
11
12
 
12
- puts "hash from argv:",ARGV.to_hash,""
13
- # {"--test"=>"test"}
13
+ end
14
14
 
15
- puts "multi valued hash:",ARGV.to_hash( multi_value: true ),""
16
- # {"--test"=>["test", "this", "ok"]}
15
+ class STRING < String
17
16
 
18
- puts "sym keyed hash:",ARGV.to_hash( sym_key: true ),""
19
- # {:test=>"test"}
17
+ end
20
18
 
21
- puts "sym keyed multi valued hash:",ARGV.to_hash( s: true, m: true ),""
22
- # {:test=>["test", "this", "ok"]}
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
- puts "argv values without the tags:",ARGV.values.inspect,""
25
- # ["test", "this", "ok"]
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
- puts "argv tags, \"keys\":",ARGV.keys.inspect,""
28
- # ["--test"]
29
+ require 'yaml'
30
+ yaml_data= [ {hello: {"world" => "hey!"} }, 123 ].to_yaml
29
31
 
30
- puts "symbolized flags:",ARGV.sym_flags.inspect,""
32
+ puts odm.parse_yml(yaml_data).inspect
data/lib/odm.rb CHANGED
@@ -1 +1,134 @@
1
- require 'odm/ext'
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. Pars Array of Hash into random Class of classes to make it more object oriented }
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.alpha
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. Pars Array of Hash into random Class of
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: 1.3.1
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