nashie 0.0.2
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/.gitignore +17 -0
- data/.rspec +2 -0
- data/Gemfile +9 -0
- data/LICENSE +22 -0
- data/README.md +38 -0
- data/Rakefile +2 -0
- data/lib/nash/version.rb +3 -0
- data/lib/nashie.rb +109 -0
- data/nashie.gemspec +17 -0
- data/spec/nash_spec.rb +49 -0
- data/spec/spec_helper.rb +16 -0
- metadata +59 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 noverloop
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# Nashie
|
2
|
+
Nashie provides a Hashie that can nest other Hashies.
|
3
|
+
This allows you to rapidly create complex nested hashes.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'nashie'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install nashie
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
class Car < Hashie::Nash
|
22
|
+
class Engine < Hashie::Nash
|
23
|
+
property :crankshaft, :required => true
|
24
|
+
property :nitro, :default => false
|
25
|
+
end
|
26
|
+
|
27
|
+
property :engine, :required => true, :class => Engine
|
28
|
+
end
|
29
|
+
|
30
|
+
Car.new "engine" => {"crankshaft" => true, "nitro" => false}
|
31
|
+
|
32
|
+
## Contributing
|
33
|
+
|
34
|
+
1. Fork it
|
35
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
36
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
37
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
38
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/nash/version.rb
ADDED
data/lib/nashie.rb
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
require "nash/version"
|
2
|
+
require 'hashie/hash'
|
3
|
+
require 'set'
|
4
|
+
|
5
|
+
module Hashie
|
6
|
+
# A Nash is a Nested Defined Hash and is an extension of a Dash
|
7
|
+
# A Nash allows you to create complex assignments composing of
|
8
|
+
# several nested Dashes
|
9
|
+
#
|
10
|
+
#
|
11
|
+
# Nashes are useful when you need to create a lightweigth nested
|
12
|
+
# data object, ideal for validating JSON
|
13
|
+
class Nash < Hashie::Dash
|
14
|
+
include Hashie::PrettyInspect
|
15
|
+
alias_method :to_s, :inspect
|
16
|
+
|
17
|
+
class << self
|
18
|
+
attr_reader :class_properties
|
19
|
+
end
|
20
|
+
instance_variable_set('@class_properties', Hash.new)
|
21
|
+
|
22
|
+
# Defines a property on the Nash. Options are
|
23
|
+
# as follows:
|
24
|
+
#
|
25
|
+
# * <tt>:default</tt> - Specify a default value for this property,
|
26
|
+
# to be returned before a value is set on the property in a new
|
27
|
+
# Nash.
|
28
|
+
#
|
29
|
+
# * <tt>:required</tt> - Specify the value as required for this
|
30
|
+
# property, to raise an error if a value is unset in a new or
|
31
|
+
# existing Nash.
|
32
|
+
#
|
33
|
+
# * <tt>:class</tt> - Specify a class for this property that
|
34
|
+
# should be instantiated when an assignment is made to this
|
35
|
+
# property.
|
36
|
+
#
|
37
|
+
def self.property(property_name, options = {})
|
38
|
+
property_name = property_name.to_sym
|
39
|
+
|
40
|
+
self.properties << property_name
|
41
|
+
|
42
|
+
if options.has_key?(:class)
|
43
|
+
class_name = options[:class].to_s
|
44
|
+
class_properties[property_name] = class_name if options.delete(:class)
|
45
|
+
class_eval <<-ACCESSORS
|
46
|
+
def #{property_name}(&block)
|
47
|
+
self.[](#{property_name.to_s.inspect}, &block)
|
48
|
+
end
|
49
|
+
|
50
|
+
def #{property_name}=(value)
|
51
|
+
self.[]=(#{property_name.to_s.inspect}, #{class_properties[property_name]}.new(value))
|
52
|
+
end
|
53
|
+
|
54
|
+
ACCESSORS
|
55
|
+
|
56
|
+
|
57
|
+
elsif
|
58
|
+
class_properties[property_name] = nil
|
59
|
+
end
|
60
|
+
|
61
|
+
if options.has_key?(:default)
|
62
|
+
self.defaults[property_name] = options[:default]
|
63
|
+
elsif self.defaults.has_key?(property_name)
|
64
|
+
self.defaults.delete property_name
|
65
|
+
end
|
66
|
+
|
67
|
+
unless instance_methods.map { |m| m.to_s }.include?("#{property_name}=")
|
68
|
+
class_eval <<-ACCESSORS
|
69
|
+
def #{property_name}(&block)
|
70
|
+
self.[](#{property_name.to_s.inspect}, &block)
|
71
|
+
end
|
72
|
+
|
73
|
+
def #{property_name}=(value)
|
74
|
+
self.[]=(#{property_name.to_s.inspect}, value)
|
75
|
+
end
|
76
|
+
ACCESSORS
|
77
|
+
end
|
78
|
+
|
79
|
+
if defined? @subclasses
|
80
|
+
@subclasses.each { |klass| klass.property(property_name, options) }
|
81
|
+
end
|
82
|
+
required_properties << property_name if options.delete(:required)
|
83
|
+
end
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
def self.inherited(klass)
|
88
|
+
super
|
89
|
+
(@subclasses ||= Set.new) << klass
|
90
|
+
klass.instance_variable_set('@properties', self.properties.dup)
|
91
|
+
klass.instance_variable_set('@defaults', self.defaults.dup)
|
92
|
+
klass.instance_variable_set('@required_properties', self.required_properties.dup)
|
93
|
+
klass.instance_variable_set('@class_properties', self.class_properties.dup)
|
94
|
+
end
|
95
|
+
|
96
|
+
|
97
|
+
# You may initialize a Dash with an attributes hash
|
98
|
+
# just like you would many other kinds of data objects.
|
99
|
+
def initialize(attributes = {}, &block)
|
100
|
+
super(attributes, &block)
|
101
|
+
|
102
|
+
# override whatever Dash has set
|
103
|
+
attributes.each_pair do |att, value|
|
104
|
+
self.send((att.to_s + '=').to_sym,value)
|
105
|
+
end if attributes
|
106
|
+
assert_required_properties_set!
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
data/nashie.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/nash/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["noverloop"]
|
6
|
+
gem.email = ["nicolas@geckoboard.com"]
|
7
|
+
gem.description = %q{Nashie is an extension of Hashie and stands for Nested Hashie. Nashie allows you to create hashes that include other objects}
|
8
|
+
gem.summary = %q{Nashie allows you to create nested Hashie structures}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "nashie"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Nash::VERSION
|
17
|
+
end
|
data/spec/nash_spec.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Hashie::Nash do
|
4
|
+
|
5
|
+
class Test1 < Hashie::Nash
|
6
|
+
property :meh,:required => true
|
7
|
+
end
|
8
|
+
|
9
|
+
class NestedTest < Hashie::Nash
|
10
|
+
property :nest, :class => Test1
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should create a new Test1" do
|
14
|
+
t = Test1.new "meh"=> 5
|
15
|
+
t.nil?.should == false
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should create a new NestedTest" do
|
19
|
+
t = NestedTest.new "nest" => {"meh"=> 5}
|
20
|
+
t.nil?.should == false
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should make nested hashes accessible" do
|
24
|
+
t = NestedTest.new "nest" => {"meh"=> 5}
|
25
|
+
t.nest.meh.should == 5
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should make validate required properties in nested dash" do
|
29
|
+
lambda do
|
30
|
+
t = NestedTest.new "nest" => {"non_existent"=> 5}
|
31
|
+
t.nest.meh.should == 5
|
32
|
+
end.should raise_error NoMethodError
|
33
|
+
end
|
34
|
+
|
35
|
+
class Car < Hashie::Nash
|
36
|
+
class Engine < Hashie::Nash
|
37
|
+
property :crankshaft, :required => true
|
38
|
+
property :nitro, :default => false
|
39
|
+
end
|
40
|
+
property :engine, :required => true, :class => Engine
|
41
|
+
end
|
42
|
+
|
43
|
+
|
44
|
+
it "should make validate required properties in nested dash" do
|
45
|
+
c = Car.new "engine" => {"crankshaft" => true, "nitro" => false}
|
46
|
+
c.engine.crankshaft.should == true
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper.rb"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
|
8
|
+
require 'rubygems'
|
9
|
+
require 'bundler'
|
10
|
+
Bundler.require(:default,:test)
|
11
|
+
|
12
|
+
RSpec.configure do |config|
|
13
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
14
|
+
config.run_all_when_everything_filtered = true
|
15
|
+
config.filter_run :focus
|
16
|
+
end
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nashie
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- noverloop
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-06-08 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Nashie is an extension of Hashie and stands for Nested Hashie. Nashie
|
15
|
+
allows you to create hashes that include other objects
|
16
|
+
email:
|
17
|
+
- nicolas@geckoboard.com
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- .gitignore
|
23
|
+
- .rspec
|
24
|
+
- Gemfile
|
25
|
+
- LICENSE
|
26
|
+
- README.md
|
27
|
+
- Rakefile
|
28
|
+
- lib/nash/version.rb
|
29
|
+
- lib/nashie.rb
|
30
|
+
- nashie.gemspec
|
31
|
+
- spec/nash_spec.rb
|
32
|
+
- spec/spec_helper.rb
|
33
|
+
homepage: ''
|
34
|
+
licenses: []
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options: []
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
|
+
none: false
|
47
|
+
requirements:
|
48
|
+
- - ! '>='
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
requirements: []
|
52
|
+
rubyforge_project:
|
53
|
+
rubygems_version: 1.8.24
|
54
|
+
signing_key:
|
55
|
+
specification_version: 3
|
56
|
+
summary: Nashie allows you to create nested Hashie structures
|
57
|
+
test_files:
|
58
|
+
- spec/nash_spec.rb
|
59
|
+
- spec/spec_helper.rb
|