class2 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a3ba3ccb4eb642b385d3878ba4861cfcfdd66aaa
4
+ data.tar.gz: 7f34029b8614857d44725173638ef3263b68cb7c
5
+ SHA512:
6
+ metadata.gz: fd9f09822c3eb65d5b3e2584ea5bf3011d5421a232457657b658b3a54b3acf34873d7d3d36e93644c38e338d89915159ba085989c70aad99deb52a92d6235950
7
+ data.tar.gz: 05720dd6b75a9ed646b833f0ecdc41163ff68faa25fa238366bc439f0fa627e1339ddd1836a3e42b26f50b86abed1c3c698d86f2dbeec5c1e681121cc1b166a3
@@ -0,0 +1,103 @@
1
+
2
+ # Created by https://www.gitignore.io/api/emacs,ruby
3
+
4
+ ### Emacs ###
5
+ # -*- mode: gitignore; -*-
6
+ *~
7
+ \#*\#
8
+ /.emacs.desktop
9
+ /.emacs.desktop.lock
10
+ *.elc
11
+ auto-save-list
12
+ tramp
13
+ .\#*
14
+
15
+ # Org-mode
16
+ .org-id-locations
17
+ *_archive
18
+
19
+ # flymake-mode
20
+ *_flymake.*
21
+
22
+ # eshell files
23
+ /eshell/history
24
+ /eshell/lastdir
25
+
26
+ # elpa packages
27
+ /elpa/
28
+
29
+ # reftex files
30
+ *.rel
31
+
32
+ # AUCTeX auto folder
33
+ /auto/
34
+
35
+ # cask packages
36
+ .cask/
37
+ dist/
38
+
39
+ # Flycheck
40
+ flycheck_*.el
41
+
42
+ # server auth directory
43
+ /server/
44
+
45
+ # projectiles files
46
+ .projectile
47
+
48
+ # directory configuration
49
+ .dir-locals.el
50
+
51
+ ### Ruby ###
52
+ *.gem
53
+ *.rbc
54
+ /.config
55
+ /coverage/
56
+ /InstalledFiles
57
+ /pkg/
58
+ /spec/reports/
59
+ /spec/examples.txt
60
+ /test/tmp/
61
+ /test/version_tmp/
62
+ /tmp/
63
+
64
+ # Used by dotenv library to load environment variables.
65
+ # .env
66
+
67
+ ## Specific to RubyMotion:
68
+ .dat*
69
+ .repl_history
70
+ build/
71
+ *.bridgesupport
72
+ build-iPhoneOS/
73
+ build-iPhoneSimulator/
74
+
75
+ ## Specific to RubyMotion (use of CocoaPods):
76
+ #
77
+ # We recommend against adding the Pods directory to your .gitignore. However
78
+ # you should judge for yourself, the pros and cons are mentioned at:
79
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
80
+ #
81
+ # vendor/Pods/
82
+
83
+ ## Documentation cache and generated files:
84
+ /.yardoc/
85
+ /_yardoc/
86
+ /doc/
87
+ /rdoc/
88
+
89
+ ## Environment normalization:
90
+ /.bundle/
91
+ /vendor/bundle
92
+ /lib/bundler/man/
93
+
94
+ # for a library or gem, you might want to ignore these files since the code is
95
+ # intended to run in multiple environments; otherwise, check them in:
96
+ # Gemfile.lock
97
+ # .ruby-version
98
+ # .ruby-gemset
99
+
100
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
101
+ .rvmrc
102
+
103
+ # End of https://www.gitignore.io/api/emacs,ruby
@@ -0,0 +1,9 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.0
5
+ - 2.4.0
6
+
7
+ before_install: gem install bundler
8
+ notifications:
9
+ email: false
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in class2.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Skye Shaw
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,128 @@
1
+ # Class2
2
+
3
+ Easily create hierarchies of classes that support nested attributes, equality, and more.
4
+
5
+ [![Build Status](https://travis-ci.org/sshaw/class2.svg?branch=master)](https://travis-ci.org/sshaw/class2)
6
+
7
+ ## Usage
8
+
9
+ ```rb
10
+ Class2(
11
+ :user => [
12
+ :name, :age,
13
+ :addresses => [
14
+ :city, :state, :zip,
15
+ :country => [ :name, :code ]
16
+ ]
17
+ ]
18
+ )
19
+ ```
20
+
21
+ This creates 3 classes: `User`, `Address`, and `Country` with the following attribute accessors:
22
+
23
+ * `User`: name, age, addresses
24
+ * `Address`: city, state, zip, country
25
+ * `Country`: name, code
26
+
27
+ Each of these classes also contain [several additional methods](#methods).
28
+
29
+ Example:
30
+
31
+ ```rb
32
+ user = User.new(
33
+ :name => "sshaw",
34
+ :age => 99,
35
+ :addresses => [
36
+ { :city => "LA",
37
+ :country => { :code => "US" } },
38
+ { :city => "NY Sizzle",
39
+ :country => { :code => "US" } },
40
+ { :city => "São José dos Campos",
41
+ :country => { :code => "BR" } }
42
+ ]
43
+ )
44
+
45
+ p user.name # "sshaw"
46
+ p user.addresses.size # 3
47
+ p user.addresses.first.city # "LA"
48
+
49
+ # Keys can be strings too
50
+ country = Country.new("name" => "America", "code" => "US")
51
+ address = Address.new(:city => "Da Bay", :state => "CA", :country => country)
52
+ user.addresses << address
53
+
54
+ p User.new(:name => "sshaw") == User.new(:name => "sshaw") # true
55
+
56
+ Class2(:foo, :bar => :baz)
57
+ Foo.new
58
+ Bar.new(:baz => 123)
59
+ ```
60
+
61
+ ### Namespaces
62
+
63
+ `Class2` can use an exiting namespace or create a new one:
64
+
65
+ ```rb
66
+ Class2(
67
+ My::Namespace,
68
+ :user => %i[name age]
69
+ )
70
+
71
+ My::Namespace::User.new(:name => "sshaw")
72
+
73
+ Class2(
74
+ "New::Namespace",
75
+ :user => %i[name age]
76
+ )
77
+
78
+ New::Namespace::User.new(:name => "sshaw")
79
+ ```
80
+
81
+ ### Naming
82
+
83
+ `Class2` uses
84
+ [`String#classify`](http://api.rubyonrails.org/classes/String.html#method-i-classify)
85
+ to turn keys into class names. `:foo` will be `Foo`, `:foo_bars` will
86
+ be `FooBar`. It also uses it to turn plural attribute names into
87
+ singular classes. An `:addresses` attribute will result in a class named
88
+ `Address` being created.
89
+
90
+ Plurality is determined by [`String#pluralize`](http://api.rubyonrails.org/classes/String.html#method-i-pluralize).
91
+
92
+ ### Methods
93
+
94
+ Classes created by `Class2` will have:
95
+
96
+ * A constructor that accepts a nested attribute hash
97
+ * Attribute readers and writers
98
+ * `#to_h`
99
+ * `#eql?` and `#==`
100
+ * `#hash`
101
+
102
+ #### Custom Methods
103
+
104
+ Just open up the class and write them:
105
+
106
+ ```rb
107
+ Class2(:user => :name)
108
+
109
+ class User
110
+ def first_initial
111
+ name[0] if name
112
+ end
113
+ end
114
+
115
+ User.new(:name => "sshaw").first_initial
116
+ ```
117
+
118
+ ## See Also
119
+
120
+ The Perl module that served as the inspiration: [`MooseX::NestedAttributesConstructor`](https://github.com/sshaw/MooseX-NestedAttributesConstructor).
121
+
122
+ ## Author
123
+
124
+ Skye Shaw [sshaw AT gmail.com]
125
+
126
+ ## License
127
+
128
+ Released under the MIT License: www.opensource.org/licenses/MIT
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ task :default => :test
5
+ Rake::TestTask.new do |t|
6
+ t.pattern = "spec/*_spec.rb"
7
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'class2/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "class2"
8
+ spec.version = Class2::VERSION
9
+ spec.authors = ["Skye Shaw"]
10
+ spec.email = ["skye.shaw@gmail.com"]
11
+
12
+ spec.summary = %q{Easily create hierarchies of classes that support nested attributes, equality, and more.}
13
+ spec.homepage = "https://github.com/sshaw/class2"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "activesupport", ">= 3.0"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.10"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "minitest"
26
+ end
@@ -0,0 +1,110 @@
1
+ # coding: utf-8
2
+
3
+ require "class2/version"
4
+ require "active_support/core_ext/module"
5
+ require "active_support/core_ext/string"
6
+
7
+ def Class2(*args)
8
+ Class2.new(*args)
9
+ end
10
+
11
+ class Class2
12
+ class << self
13
+ def new(*argz)
14
+ specs = argz
15
+ namespace = Object
16
+
17
+ if specs[0].is_a?(String) || specs[0].is_a?(Module)
18
+ namespace = specs[0].is_a?(String) ? create_namespace(specs.shift) : specs.shift
19
+ end
20
+
21
+ specs.each do |spec|
22
+ spec = [spec] unless spec.respond_to?(:each)
23
+ spec.each { |klass, attributes| make_class(namespace, klass, attributes) }
24
+ end
25
+
26
+ nil
27
+ end
28
+
29
+ private
30
+
31
+ def create_namespace(str)
32
+ str.split("::").inject(Object) do |parent, child|
33
+ # empty? to handle "::Namespace"
34
+ child.empty? ? parent : parent.const_set(child, Module.new)
35
+ end
36
+ end
37
+
38
+ def make_class(namespace, name, attributes)
39
+ attributes = [attributes] unless attributes.is_a?(Array)
40
+
41
+ nested, simple = attributes.compact.partition { |e| e.is_a?(Hash) }
42
+ nested.each do |object|
43
+ object.each { |klass, attrs| make_class(namespace, klass, attrs) }
44
+ end
45
+
46
+ klass = Class.new do
47
+ def initialize(attributes = nil)
48
+ assign_attributes(attributes || {})
49
+ end
50
+
51
+ class_eval <<-CODE
52
+ def hash
53
+ to_h.hash
54
+ end
55
+
56
+ def ==(other)
57
+ return false unless other.instance_of?(self.class)
58
+ to_h == other.to_h
59
+ end
60
+
61
+ alias :eql? :==
62
+
63
+ def to_h
64
+ hash = {}
65
+ (#{simple + nested.map { |n| n.keys.first }}).each do |name|
66
+ hash[name] = public_send(name)
67
+ hash[name] = hash[name].to_h if hash[name].respond_to?(:to_h)
68
+ end
69
+
70
+ hash
71
+ end
72
+ CODE
73
+
74
+ simple.each { |method| attr_accessor method }
75
+
76
+ nested.map { |n| n.keys.first }.each do |method, _|
77
+ attr_writer method
78
+
79
+ method = method.to_s
80
+ retval = method == method.pluralize ? "[]" : "#{method.classify}.new"
81
+ class_eval <<-CODE
82
+ def #{method}
83
+ @#{method} ||= #{retval}
84
+ end
85
+ CODE
86
+ end
87
+
88
+ private
89
+
90
+ def assign_attributes(attributes)
91
+ attributes.each do |key, value|
92
+ if value.is_a?(Hash) || value.is_a?(Array)
93
+ name = key.to_s.classify
94
+
95
+ # Only look in our namespace to prevent unwanted lookup
96
+ next unless self.class.parent.const_defined?(name)
97
+ klass = self.class.parent.const_get(name)
98
+
99
+ value = value.is_a?(Hash) ? klass.new(value) : value.map { |v| klass.new(v) }
100
+ end
101
+
102
+ public_send("#{key}=", value)
103
+ end
104
+ end
105
+ end
106
+
107
+ namespace.const_set(name.to_s.classify, klass)
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,3 @@
1
+ class Class2
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: class2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Skye Shaw
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-08-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description:
70
+ email:
71
+ - skye.shaw@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - class2.gemspec
83
+ - lib/class2.rb
84
+ - lib/class2/version.rb
85
+ homepage: https://github.com/sshaw/class2
86
+ licenses:
87
+ - MIT
88
+ metadata: {}
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 2.6.11
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: Easily create hierarchies of classes that support nested attributes, equality,
109
+ and more.
110
+ test_files: []