Permalink
Browse files

importing files

  • Loading branch information...
0 parents commit 736c9db72ba78b88eea84765502cef2bf1585a69 @twoism committed Mar 2, 2011
Showing with 171 additions and 0 deletions.
  1. +3 −0 .gitignore
  2. +2 −0 .rspec
  3. +11 −0 Gemfile
  4. +28 −0 Gemfile.lock
  5. +3 −0 Rakefile
  6. +1 −0 autotest/discover.rb
  7. +22 −0 lib/pathy.rb
  8. +3 −0 lib/pathy/version.rb
  9. +21 −0 pathy.gemspec
  10. +73 −0 spec/lib/pathy_spec.rb
  11. +4 −0 spec/spec_helper.rb
@@ -0,0 +1,3 @@
+pkg/*
+*.gem
+.bundle
@@ -0,0 +1,2 @@
+--colour
+--format=nested
@@ -0,0 +1,11 @@
+source "http://rubygems.org"
+
+# Specify your gem's dependencies in pathy.gemspec
+gemspec
+
+gem 'json'
+
+group :development do
+ gem 'rspec'
+ gem 'ZenTest'
+end
@@ -0,0 +1,28 @@
+PATH
+ remote: .
+ specs:
+ pathy (0.0.1)
+
+GEM
+ remote: http://rubygems.org/
+ specs:
+ ZenTest (4.5.0)
+ diff-lcs (1.1.2)
+ json (1.5.1)
+ rspec (2.5.0)
+ rspec-core (~> 2.5.0)
+ rspec-expectations (~> 2.5.0)
+ rspec-mocks (~> 2.5.0)
+ rspec-core (2.5.1)
+ rspec-expectations (2.5.0)
+ diff-lcs (~> 1.1.2)
+ rspec-mocks (2.5.0)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ ZenTest
+ json
+ pathy!
+ rspec
@@ -0,0 +1,3 @@
+require 'bundler'
+Bundler::GemHelper.install_tasks
+
@@ -0,0 +1 @@
+Autotest.add_discovery { "rspec2" }
@@ -0,0 +1,22 @@
+require 'json'
+
+module Pathy
+ module InstanceMethods
+ def at_json_path path
+ method_chain = path.split('.')
+ method_chain.inject(self.reparsed_as_json) {|obj,m| obj.send('[]', (obj.respond_to?(:push) ? m.to_i : m) ) }
+ end
+
+ def reparsed_as_json
+ self.is_a?(String) ? JSON.parse(self) : JSON.parse(self.to_json)
+ end
+ end
+ module ClassMethods
+ def pathy!
+ self.send :include, InstanceMethods
+ end
+ end
+end
+
+Object.extend Pathy::ClassMethods
+
@@ -0,0 +1,3 @@
+module Pathy
+ VERSION = "0.0.1"
+end
@@ -0,0 +1,21 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "pathy/version"
+
+Gem::Specification.new do |s|
+ s.name = "pathy"
+ s.version = Pathy::VERSION
+ s.platform = Gem::Platform::RUBY
+ s.authors = ["TODO: Write your name"]
+ s.email = ["TODO: Write your email address"]
+ s.homepage = "http://rubygems.org/gems/pathy"
+ s.summary = %q{TODO: Write a gem summary}
+ s.description = %q{TODO: Write a gem description}
+
+ s.rubyforge_project = "pathy"
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
+end
@@ -0,0 +1,73 @@
+require 'spec_helper'
+
+describe Pathy do
+ before :all do
+ Object.pathy!
+ @json = %[
+ {
+ "string" : "barr",
+ "number" : 1,
+ "array" : [1,2,3],
+ "hash" : {"one":{"two" : 2}}
+ }
+ ]
+
+ @json_array = %[
+ [{
+ "string" : "barr",
+ "number" : 1,
+ "array" : [1,2,3],
+ "hash" : {"one":{"two" : 2}}
+ }]
+ ]
+
+ end
+
+
+ describe "for hashes" do
+ before :all do
+ @obj = JSON.parse(@json)
+ @array = JSON.parse(@json_array)
+ end
+
+ it "should parse 'number' as 1" do
+ @obj.at_json_path("number").should == 1
+ end
+
+ it "should parse 'array' as [1,2,3]" do
+ @obj.at_json_path('array').should == [1,2,3]
+ end
+
+ it "should parse 'hash.one' as {'two': 2}" do
+ @obj.at_json_path('hash.one.two').should == 2
+ end
+
+ end
+
+ describe "for arrays" do
+
+ before :all do
+ @array = JSON.parse(@json_array)
+ end
+
+ it "should find the index" do
+ @array.at_json_path('0.hash.one.two').should == 2
+ end
+ end
+
+ describe "for json strings" do
+ it "should parse 'number' as 1" do
+ @json.at_json_path("number").should == 1
+ end
+
+ it "should parse 'array' as [1,2,3]" do
+ @json.at_json_path('array').should == [1,2,3]
+ end
+
+ it "should parse 'hash.one.two' as 2" do
+ @json.at_json_path('hash.one.two').should == 2
+ end
+
+ end
+end
+
@@ -0,0 +1,4 @@
+$LOAD_PATH.unshift(File.dirname(__FILE__))
+$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
+require 'pathy'
+require 'json'

0 comments on commit 736c9db

Please sign in to comment.