lazyhash 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
4
+
5
+ Gemfile.lock
6
+ .DS_Store
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in lazyhash.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core'
5
+ require 'rspec/core/rake_task'
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task :default => :spec
data/lazyhash.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "lazyhash/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "lazyhash"
7
+ s.version = Lazyhash::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Roger Campos"]
10
+ s.email = ["roger@itnig.net"]
11
+ s.homepage = "https://github.com/rogercampos/lazyhash"
12
+ s.summary = %q{add values to a hash with an arbitrary deep of keys}
13
+ s.description = %q{add values to a hash with an arbitrary deep of keys}
14
+
15
+ s.rubyforge_project = "lazyhash"
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", "~> 2.5"
23
+ end
@@ -0,0 +1,3 @@
1
+ module Lazyhash
2
+ VERSION = "0.1.0"
3
+ end
data/lib/lazyhash.rb ADDED
@@ -0,0 +1,23 @@
1
+ module LazyHash
2
+ class << self
3
+ def add(hash, key, value, pre = nil)
4
+ skeys = key.split(".")
5
+ f = skeys.shift
6
+ if skeys.empty?
7
+ if pre.nil?
8
+ hash.send("[]=", f, value) unless hash.has_key?(f)
9
+ else
10
+ pre.send("[]=", f, value) unless pre.has_key?(f)
11
+ end
12
+ else
13
+ pre = pre.nil? ? hash.send("[]", f) : pre.send("[]", f)
14
+ add(hash, skeys.join("."), value, pre)
15
+ end
16
+ end
17
+
18
+ def build_hash
19
+ lazy = lambda { |h,k| h[k] = Hash.new(&lazy) }
20
+ Hash.new(&lazy)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe LazyHash do
4
+ before do
5
+ @hash = LazyHash.build_hash
6
+ end
7
+
8
+ it "should assign consecutive values with different deep's" do
9
+ LazyHash.add(@hash, "es.projects.title", "Main title")
10
+ LazyHash.add(@hash, "es.ph1", "one paragraph")
11
+
12
+ @hash.should == {"es" => {"projects" => {"title" => "Main title"}, "ph1" => "one paragraph"}}
13
+ end
14
+
15
+ it "should not overwrite existing values with keys" do
16
+ LazyHash.add(@hash, "es.projects.title", "Main title")
17
+ LazyHash.add(@hash, "es.projects", "title")
18
+
19
+ @hash.should == {"es" => {"projects" => {"title" => "Main title"}}}
20
+ end
21
+ end
@@ -0,0 +1 @@
1
+ require 'lazyhash'
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lazyhash
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Roger Campos
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-02-17 00:00:00 +01: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: 9
30
+ segments:
31
+ - 2
32
+ - 5
33
+ version: "2.5"
34
+ type: :development
35
+ version_requirements: *id001
36
+ description: add values to a hash with an arbitrary deep of keys
37
+ email:
38
+ - roger@itnig.net
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files: []
44
+
45
+ files:
46
+ - .gitignore
47
+ - Gemfile
48
+ - Rakefile
49
+ - lazyhash.gemspec
50
+ - lib/lazyhash.rb
51
+ - lib/lazyhash/version.rb
52
+ - spec/lazyhash_spec.rb
53
+ - spec/spec_helper.rb
54
+ has_rdoc: true
55
+ homepage: https://github.com/rogercampos/lazyhash
56
+ licenses: []
57
+
58
+ post_install_message:
59
+ rdoc_options: []
60
+
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ hash: 3
69
+ segments:
70
+ - 0
71
+ version: "0"
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ hash: 3
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ requirements: []
82
+
83
+ rubyforge_project: lazyhash
84
+ rubygems_version: 1.3.7
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: add values to a hash with an arbitrary deep of keys
88
+ test_files: []
89
+