hash_to_object 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Gemfile ADDED
@@ -0,0 +1 @@
1
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,15 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ hash_to_object (0.1.0)
5
+
6
+ GEM
7
+ specs:
8
+ rspec (1.2.9)
9
+
10
+ PLATFORMS
11
+ ruby
12
+
13
+ DEPENDENCIES
14
+ hash_to_object!
15
+ rspec
data/README.md ADDED
@@ -0,0 +1,30 @@
1
+ HashToObject
2
+ ============
3
+
4
+ HashToObject is a mixin for converting hashes into ruby objects. For instance, if you wanted an Order class that could be instantiated from a hash, you could define it as such:
5
+
6
+ class Order
7
+ include HashToObject
8
+
9
+ def initialize(options = {})
10
+ objectify(options)
11
+ end
12
+ end
13
+
14
+ And you have a hash like such:
15
+
16
+ hash_to_object = {:amount => 25, :type => "credit", :admin => false}
17
+
18
+ Then you can call `Order.new(hash_to_object)` and get an `Order` object with instance variables `@amount`, `@type`, `@admin`.
19
+
20
+ Nesting
21
+ =======
22
+ HashToObject also supports nesting of object creation. It defines the nested objects' class based on the name of the parent class and the singularized key name of the nested value.
23
+
24
+ Example:
25
+
26
+ Order.new({:item => {:name => "foo"}, :transactions => [{:id => "bar"},{:id => "baz"}])
27
+
28
+ This would create an object of type `Order`, with instance variables `@item` and `@transactions` linking to objects of type `Order::Item` (with instance variable `@name`), and `Order::Transaction` (with instance variable `@id`).
29
+
30
+ The only caveat is that you need to have `Order::Item` and `Order::Transaction` defined, and mix-in HashToObject similarly to `Order` above.
data/Rakefile ADDED
@@ -0,0 +1,150 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'date'
4
+
5
+ #############################################################################
6
+ #
7
+ # Helper functions
8
+ #
9
+ #############################################################################
10
+
11
+ def name
12
+ @name ||= Dir['*.gemspec'].first.split('.').first
13
+ end
14
+
15
+ def version
16
+ line = File.read("lib/#{name}.rb")[/^\s*VERSION\s*=\s*.*/]
17
+ line.match(/.*VERSION\s*=\s*['"](.*)['"]/)[1]
18
+ end
19
+
20
+ def date
21
+ Date.today.to_s
22
+ end
23
+
24
+ def rubyforge_project
25
+ name
26
+ end
27
+
28
+ def gemspec_file
29
+ "#{name}.gemspec"
30
+ end
31
+
32
+ def gem_file
33
+ "#{name}-#{version}.gem"
34
+ end
35
+
36
+ def replace_header(head, header_name)
37
+ head.sub!(/(\.#{header_name}\s*= ').*'/) { "#{$1}#{send(header_name)}'"}
38
+ end
39
+
40
+ #############################################################################
41
+ #
42
+ # Standard tasks
43
+ #
44
+ #############################################################################
45
+
46
+ task :default => :test
47
+
48
+ require 'rake/testtask'
49
+ Rake::TestTask.new(:test) do |test|
50
+ test.libs << 'lib' << 'test'
51
+ test.pattern = 'test/**/test_*.rb'
52
+ test.verbose = true
53
+ end
54
+
55
+ desc "Generate RCov test coverage and open in your browser"
56
+ task :coverage do
57
+ require 'rcov'
58
+ sh "rm -fr coverage"
59
+ sh "rcov test/test_*.rb"
60
+ sh "open coverage/index.html"
61
+ end
62
+
63
+ require 'rake/rdoctask'
64
+ Rake::RDocTask.new do |rdoc|
65
+ rdoc.rdoc_dir = 'rdoc'
66
+ rdoc.title = "#{name} #{version}"
67
+ rdoc.rdoc_files.include('README*')
68
+ rdoc.rdoc_files.include('lib/**/*.rb')
69
+ end
70
+
71
+ desc "Open an irb session preloaded with this library"
72
+ task :console do
73
+ sh "irb -rubygems -r ./lib/#{name}.rb"
74
+ end
75
+
76
+ #############################################################################
77
+ #
78
+ # Custom tasks (add your own tasks here)
79
+ #
80
+ #############################################################################
81
+
82
+
83
+
84
+ #############################################################################
85
+ #
86
+ # Packaging tasks
87
+ #
88
+ #############################################################################
89
+
90
+ desc "Create tag v#{version} and build and push #{gem_file} to Rubygems"
91
+ task :release => :build do
92
+ unless `git branch` =~ /^\* master$/
93
+ puts "You must be on the master branch to release!"
94
+ exit!
95
+ end
96
+ sh "git commit --allow-empty -a -m 'Release #{version}'"
97
+ sh "git tag v#{version}"
98
+ sh "git push origin master"
99
+ sh "git push origin v#{version}"
100
+ sh "gem push pkg/#{name}-#{version}.gem"
101
+ end
102
+
103
+ desc "Build #{gem_file} into the pkg directory"
104
+ task :build => :gemspec do
105
+ sh "mkdir -p pkg"
106
+ sh "gem build #{gemspec_file}"
107
+ sh "mv #{gem_file} pkg"
108
+ end
109
+
110
+ desc "Generate #{gemspec_file}"
111
+ task :gemspec => :validate do
112
+ # read spec file and split out manifest section
113
+ spec = File.read(gemspec_file)
114
+ head, manifest, tail = spec.split(" # = MANIFEST =\n")
115
+
116
+ # replace name version and date
117
+ replace_header(head, :name)
118
+ replace_header(head, :version)
119
+ replace_header(head, :date)
120
+ #comment this out if your rubyforge_project has a different name
121
+ replace_header(head, :rubyforge_project)
122
+
123
+ # determine file list from git ls-files
124
+ files = `git ls-files`.
125
+ split("\n").
126
+ sort.
127
+ reject { |file| file =~ /^\./ }.
128
+ reject { |file| file =~ /^(rdoc|pkg)/ }.
129
+ map { |file| " #{file}" }.
130
+ join("\n")
131
+
132
+ # piece file back together and write
133
+ manifest = " s.files = %w[\n#{files}\n ]\n"
134
+ spec = [head, manifest, tail].join(" # = MANIFEST =\n")
135
+ File.open(gemspec_file, 'w') { |io| io.write(spec) }
136
+ puts "Updated #{gemspec_file}"
137
+ end
138
+
139
+ desc "Validate #{gemspec_file}"
140
+ task :validate do
141
+ libfiles = Dir['lib/*'] - ["lib/#{name}.rb", "lib/#{name}"]
142
+ unless libfiles.empty?
143
+ puts "Directory `lib` should only contain a `#{name}.rb` file and `#{name}` dir."
144
+ exit!
145
+ end
146
+ unless Dir['VERSION*'].empty?
147
+ puts "A `VERSION` file at root level violates Gem best practices."
148
+ exit!
149
+ end
150
+ end
@@ -0,0 +1,57 @@
1
+ ## This is the rakegem gemspec template. Make sure you read and understand
2
+ ## all of the comments. Some sections require modification, and others can
3
+ ## be deleted if you don't need them. Once you understand the contents of
4
+ ## this file, feel free to delete any comments that begin with two hash marks.
5
+ ## You can find comprehensive Gem::Specification documentation, at
6
+ ## http://docs.rubygems.org/read/chapter/20
7
+ Gem::Specification.new do |s|
8
+ s.specification_version = 2 if s.respond_to? :specification_version=
9
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
10
+ s.rubygems_version = '1.3.5'
11
+
12
+ ## Leave these as is they will be modified for you by the rake gemspec task.
13
+ ## If your rubyforge_project name is different, then edit it and comment out
14
+ ## the sub! line in the Rakefile
15
+ s.name = 'hash_to_object'
16
+ s.version = '0.1.0'
17
+ s.date = '2011-09-26'
18
+
19
+ ## Make sure your summary is short. The description may be as long
20
+ ## as you like.
21
+ s.summary = "Converts json-like hashes into ruby objects."
22
+ s.description = ""
23
+
24
+ ## List the primary authors. If there are a bunch of authors, it's probably
25
+ ## better to set the email to an email list or something. If you don't have
26
+ ## a custom homepage, consider using your GitHub URL or the like.
27
+ s.authors = ["Phillip Birtcher"]
28
+ s.email = 'philbirt@gmail.com'
29
+ s.homepage = 'https://github.com/philbirt/hash_to_object'
30
+
31
+ ## This gets added to the $LOAD_PATH so that 'lib/NAME.rb' can be required as
32
+ ## require 'NAME.rb' or'/lib/NAME/file.rb' can be as require 'NAME/file.rb'
33
+ s.require_paths = %w[lib]
34
+
35
+ ## List your development dependencies here. Development dependencies are
36
+ ## those that are only needed during development
37
+ s.add_development_dependency('rspec')
38
+
39
+ ## Leave this section as-is. It will be automatically generated from the
40
+ ## contents of your Git repository via the gemspec task. DO NOT REMOVE
41
+ ## THE MANIFEST COMMENTS, they are used as delimiters by the task.
42
+ # = MANIFEST =
43
+ s.files = %w[
44
+ Gemfile
45
+ Gemfile.lock
46
+ README.md
47
+ Rakefile
48
+ hash_to_object.gemspec
49
+ lib/hash_to_object.rb
50
+ spec/hash_to_object_spec.rb
51
+ ]
52
+ # = MANIFEST =
53
+
54
+ ## Test files will be grabbed from the file list. Make sure the path glob
55
+ ## matches what you actually use.
56
+ s.test_files = s.files.select { |path| path =~ /^spec\/*._spec\.rb/ }
57
+ end
@@ -0,0 +1,53 @@
1
+ require 'active_support/inflector'
2
+
3
+ module HashToObject
4
+ VERSION = '0.1.0'
5
+
6
+ class << self
7
+ alias_method :build_new_object, :build_new_object
8
+ end
9
+
10
+ def objectify(hash_parameters)
11
+ hash_parameters.each do |key, value|
12
+ if value.is_a?(Array)
13
+ objects = value.map{|item| build_new_object(build_class_string(key), item)}
14
+ define_attribute(key, objects)
15
+ elsif value.is_a?(Hash)
16
+ define_attribute(key, build_new_object(build_class_string(key), value))
17
+ else
18
+ define_attribute(key.to_s, value)
19
+ end
20
+ end
21
+ end
22
+
23
+ def build_class_string(class_string)
24
+ @settings ||= load_settings
25
+ class_string = class_string.singularize.underscore
26
+
27
+ if @settings && @settings.send(class_string.to_sym)
28
+ @settings.send(class_string.to_sym)
29
+ else
30
+ "#{self.class}::#{class_string.singularize.camelcase}"
31
+ end
32
+ end
33
+
34
+ def build_new_object(class_string, hash_parameters)
35
+ class_string.constantize.new(hash_parameters)
36
+ end
37
+
38
+ def define_attribute(key, value)
39
+ metaclass.send :attr_accessor, key.underscore
40
+ send "#{key}=".underscore.to_sym, value
41
+ end
42
+
43
+ def metaclass
44
+ class << self
45
+ self
46
+ end
47
+ end
48
+
49
+ def load_settings
50
+ #implement this
51
+ nil
52
+ end
53
+ end
@@ -0,0 +1,88 @@
1
+ require 'lib/hash_to_object.rb'
2
+
3
+ describe HashToObject do
4
+ class Dummy
5
+ include HashToObject
6
+ def initialize(options = {})
7
+ objectify(options)
8
+ end
9
+ end
10
+
11
+ class Dummy::Item
12
+ include HashToObject
13
+ def initialize(options = {})
14
+ objectify(options)
15
+ end
16
+ end
17
+
18
+ class TopLevel
19
+ include HashToObject
20
+ def initialize(options = {})
21
+ objectify(options)
22
+ end
23
+ end
24
+
25
+ before do
26
+ Settings = stub unless Object.const_defined?(:Settings)
27
+ Settings.stub_chain(:hash_to_object, :send).and_return(nil)
28
+ end
29
+
30
+ context "creates a new object based on a class and a hash" do
31
+ before :each do
32
+ @dummy = Dummy.new({"amount" => 1, "body" => "text", "admin" => true})
33
+ end
34
+ it "has a class that matches the parameters" do
35
+ @dummy.class.should == Dummy
36
+ end
37
+
38
+ it "creates instance variables based on the hash" do
39
+ @dummy.amount.should == 1
40
+ @dummy.body.should == "text"
41
+ @dummy.admin.should == true
42
+ end
43
+
44
+ context "creates associations" do
45
+ context "creates a new object if the association is 1:1" do
46
+ before :each do
47
+ @dummy = Dummy.new({"amount" => 1, "body" => "text", "admin" => true, "item" => {"foo" => "bar"}})
48
+ end
49
+ it "has a class that matches the parameters" do
50
+ @dummy.item.class.should == Dummy::Item
51
+ end
52
+
53
+ it "creates instance variables based on the hash" do
54
+ @dummy.item.foo.should == "bar"
55
+ end
56
+ end
57
+
58
+ context "creates an array of new objects if the association is 1:many" do
59
+ before :each do
60
+ @dummy = Dummy.new({"amount" => 1, "body" => "text", "admin" => true, "items" => [{"foo" => "bar"}, {"bar" => "baz"}]})
61
+ end
62
+ it "creates an instance variable based on the array" do
63
+ @dummy.items.class.should == Array
64
+ end
65
+ it "creates objects of the desired class in the array" do
66
+ @dummy.items.first.class.should == Dummy::Item
67
+ end
68
+ end
69
+ end
70
+ end
71
+
72
+ context "throws exceptions if classes are undefined" do
73
+ it "throws a NameError if the parent class provided is undefined" do
74
+ lambda{Foo.new({"foo" => "bar"})}.should raise_error(NameError)
75
+ end
76
+
77
+ it "throws a NameError if a subclass is undefined" do
78
+ lambda{Dummy.new({"foo" => "bar", "bar" => {"admin" => true}})}.should raise_error(NameError)
79
+ end
80
+ end
81
+
82
+ it "creates objects of pre-defined classes if they are in hash_to_object.yml config" do
83
+ pending
84
+ Settings.stub_chain(:hash_to_object, :send).and_return('TopLevel')
85
+ @dummy = Dummy.new({"amount" => 1, "body" => "text", "admin" => true, "top_level" => {"foo" => "bar"}})
86
+ @dummy.top_level.class.should == TopLevel
87
+ end
88
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hash_to_object
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Phillip Birtcher
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-09-26 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ description: ""
36
+ email: philbirt@gmail.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files: []
42
+
43
+ files:
44
+ - Gemfile
45
+ - Gemfile.lock
46
+ - README.md
47
+ - Rakefile
48
+ - hash_to_object.gemspec
49
+ - lib/hash_to_object.rb
50
+ - spec/hash_to_object_spec.rb
51
+ has_rdoc: true
52
+ homepage: https://github.com/philbirt/hash_to_object
53
+ licenses: []
54
+
55
+ post_install_message:
56
+ rdoc_options: []
57
+
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ hash: 3
66
+ segments:
67
+ - 0
68
+ version: "0"
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ hash: 3
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ requirements: []
79
+
80
+ rubyforge_project:
81
+ rubygems_version: 1.5.2
82
+ signing_key:
83
+ specification_version: 2
84
+ summary: Converts json-like hashes into ruby objects.
85
+ test_files: []
86
+