objectify 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ .bundle
2
+ .idea
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,24 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ objectify (0.0.1)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.1.2)
10
+ rspec (2.5.0)
11
+ rspec-core (~> 2.5.0)
12
+ rspec-expectations (~> 2.5.0)
13
+ rspec-mocks (~> 2.5.0)
14
+ rspec-core (2.5.1)
15
+ rspec-expectations (2.5.0)
16
+ diff-lcs (~> 1.1.2)
17
+ rspec-mocks (2.5.0)
18
+
19
+ PLATFORMS
20
+ ruby
21
+
22
+ DEPENDENCIES
23
+ objectify!
24
+ rspec
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/gempackagetask'
4
+ require 'rspec/core/rake_task'
5
+ require 'bundler'
6
+
7
+ Bundler::GemHelper.install_tasks
8
+
9
+ RSpec::Core::RakeTask.new :spec do |t|
10
+ t.rspec_opts= '--color'
11
+ end
12
+
13
+ task :default => :spec
data/lib/objectify.rb ADDED
@@ -0,0 +1,27 @@
1
+ module Objectify
2
+ class Proxy
3
+ def == other
4
+ @target == other
5
+ end
6
+
7
+ def initialize target
8
+ @target = target
9
+ end
10
+
11
+ def method_missing name, *args, &block
12
+ if @target.is_a?(Hash) && @target.has_key?(name)
13
+ Proxy.new @target[name]
14
+ else
15
+ Proxy.new @target.send(name, *args, &block)
16
+ end
17
+ end
18
+ end
19
+
20
+ def objectify
21
+ Proxy.new self
22
+ end
23
+ end
24
+
25
+ def Objectify object
26
+ Objectify::Proxy.new object
27
+ end
@@ -0,0 +1,3 @@
1
+ module Objectify
2
+ VERSION = "0.0.1"
3
+ end
data/objectify.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "objectify/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "objectify"
7
+ s.version = Objectify::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Andrew Kiellor"]
10
+ s.email = ["akiellor@gmail.com"]
11
+ s.homepage = ""
12
+ s.summary = %q{A simple gem to make hashes and arrays of hashes behave like objects.}
13
+ s.description = %q{A simple gem to make hashes and arrays of hashes behave like objects using proxies.}
14
+
15
+ s.rubyforge_project = "objectify"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_development_dependency('rspec')
23
+ end
@@ -0,0 +1,59 @@
1
+ require 'spec_helper'
2
+ require 'objectify'
3
+
4
+ Object.send :include, Objectify
5
+
6
+ describe "an objectified hash" do
7
+ subject { {:key => "value", :another_key => "another value"}.objectify }
8
+
9
+ its(:key) { should == "value" }
10
+ its(:another_key) { should == "another value" }
11
+ end
12
+
13
+ describe "an objectified hash with nested hashes" do
14
+ let(:objectified) { {:key => {:nested_key => "value"}}.objectify }
15
+
16
+ it "should allow nested hashes to be accessed via accessors" do
17
+ objectified.key.nested_key.should == "value"
18
+ end
19
+ end
20
+
21
+ describe "an array with nested hashes" do
22
+ let(:objectified) { [{:key => {:nested_key => [{:first => "first_value"}]}}].objectify }
23
+
24
+ it "should allow arrays nested hashes to be accessed via accessors" do
25
+ objectified[0].key == {:nested_key => [{:first => "first_value"}]}
26
+ end
27
+
28
+ it "should allow arrays nested nested hashes to be accessed via accessors" do
29
+ objectified[0].key.nested_key.should == [{:first => "first_value"}]
30
+ end
31
+
32
+ it "should allow arrays nested nested arrays hashes to be accessed via accessors" do
33
+ objectified[0].key.nested_key[0].first.should == "first_value"
34
+ end
35
+
36
+ it "should allow arrays nested hashes to be mapped" do
37
+ objectified[0].key.map {|item| item[0].class }.should == [Symbol]
38
+ end
39
+ end
40
+
41
+ describe "an array with nested hashes constructed with Objectify()" do
42
+ let(:objectified) { Objectify([{:key => {:nested_key => [{:first => "first_value"}]}}]) }
43
+
44
+ it "should allow arrays nested hashes to be accessed via accessors" do
45
+ objectified[0].key == {:nested_key => [{:first => "first_value"}]}
46
+ end
47
+
48
+ it "should allow arrays nested nested hashes to be accessed via accessors" do
49
+ objectified[0].key.nested_key.should == [{:first => "first_value"}]
50
+ end
51
+
52
+ it "should allow arrays nested nested arrays hashes to be accessed via accessors" do
53
+ objectified[0].key.nested_key[0].first.should == "first_value"
54
+ end
55
+
56
+ it "should allow arrays nested hashes to be mapped" do
57
+ objectified[0].key.map {|item| item[0].class }.should == [Symbol]
58
+ end
59
+ end
@@ -0,0 +1,7 @@
1
+ Dir["support/**/*.rb"].each {|f| require f }
2
+
3
+ if defined? RSpec
4
+ RSpec.configure do |config|
5
+ config.mock_with :rspec
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: objectify
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Andrew Kiellor
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-03-07 00:00:00 +11:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rspec
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ type: :development
26
+ version_requirements: *id001
27
+ description: A simple gem to make hashes and arrays of hashes behave like objects using proxies.
28
+ email:
29
+ - akiellor@gmail.com
30
+ executables: []
31
+
32
+ extensions: []
33
+
34
+ extra_rdoc_files: []
35
+
36
+ files:
37
+ - .gitignore
38
+ - Gemfile
39
+ - Gemfile.lock
40
+ - Rakefile
41
+ - lib/objectify.rb
42
+ - lib/objectify/version.rb
43
+ - objectify.gemspec
44
+ - spec/objectify_spec.rb
45
+ - spec/spec_helper.rb
46
+ has_rdoc: true
47
+ homepage: ""
48
+ licenses: []
49
+
50
+ post_install_message:
51
+ rdoc_options: []
52
+
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ requirements: []
68
+
69
+ rubyforge_project: objectify
70
+ rubygems_version: 1.5.0
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: A simple gem to make hashes and arrays of hashes behave like objects.
74
+ test_files:
75
+ - spec/objectify_spec.rb
76
+ - spec/spec_helper.rb