attr_permit 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f9c5282583796947f5fa8dbfe8d52d403face943
4
+ data.tar.gz: 6a017843c7a79715a9e7f2d39db51c4021e824f9
5
+ SHA512:
6
+ metadata.gz: f54cff42ef598ccc27d720e86477643f678d097ec3ddd8a404e8d96bded5ee3013b1a8eff1e28d8f9aa1b9d0d8c68dcf9d4d246d76d8ea4d3f8ef650ab648ce6
7
+ data.tar.gz: 08047aee3e18f211bc6461b3778e0ecbeb4128a73b8a413dfb08dbfb4c73f3f54043635d6056a6d759a0375c293ddb8df00fe96e8f7b7992950ea59c39b25590
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in attr_permit.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Dustin Zeisler
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,29 @@
1
+ # AttrPermit
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'attr_permit'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install attr_permit
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( https://github.com/[my-github-username]/attr_permit/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'attr_permit/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "attr_permit"
8
+ spec.version = AttrPermit::VERSION
9
+ spec.authors = ["Dustin Zeisler"]
10
+ spec.email = ["dustin@zive.me"]
11
+ spec.summary = %q{Simple parametable object creator with lazy loading options.}
12
+ spec.description = %q{Simple parametable object creator with lazy loading options. Objects can be hashable and optionally convert all values to strings.}
13
+ spec.homepage = "https://github.com/zeisler/attr_permit"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency "virtus", '~> 1.0'
22
+ spec.add_runtime_dependency "activesupport", "~>4.1"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.6"
25
+ spec.add_development_dependency "rake", "~> 10.3"
26
+ spec.add_development_dependency "rspec", "~>3.0"
27
+ end
@@ -0,0 +1,141 @@
1
+ require 'attr_permit/version'
2
+ require 'active_support/core_ext/big_decimal'
3
+
4
+ class AttrPermit
5
+
6
+ class << self
7
+
8
+ def permissible_methods
9
+ @permissible_methods ||= []
10
+ end
11
+
12
+ def attr_permit(*permissible_methods)
13
+ self.permissible_methods.concat [*permissible_methods, *get_super_attr]
14
+ self.permissible_methods.each do |meth|
15
+
16
+ send(:define_method, meth) do
17
+ call_method(meth)
18
+ end
19
+
20
+ attr_writer meth unless public_instance_methods.include?("#{meth}=")
21
+ end
22
+ end
23
+
24
+ protected
25
+
26
+ def get_super_attr
27
+ superclass.permissible_methods unless superclass == AttrPermit
28
+ end
29
+
30
+ end
31
+ protected
32
+ attr_reader :source
33
+ public
34
+
35
+ def initialize(source=nil)
36
+ @source = source
37
+ update(source)
38
+ end
39
+
40
+ def update(source)
41
+ return if source.nil?
42
+ source = OpenStruct.new(source) if source.class <= Hash
43
+ self.class.permissible_methods.each do |meth|
44
+ send("#{meth}=", source.send(meth)) if source.respond_to? meth
45
+ end
46
+ end
47
+
48
+ def call_method(meth)
49
+ callable = instance_variable_get("@#{meth}")
50
+ instance_variable_set("@#{meth}", callable.call) if callable.respond_to?(:call)
51
+ instance_variable_get("@#{meth}")
52
+ end
53
+
54
+ protected :call_method
55
+
56
+ def to_hash(big_decimal_as_string: false, all_values_as_string: false)
57
+ @big_decimal_as_string = big_decimal_as_string
58
+ @all_values_as_string = all_values_as_string
59
+ hash = {}
60
+ self.class.permissible_methods.each do |var|
61
+ value = send(var)
62
+ value = to_hash_object(value)
63
+ value = big_decimal_as_string_convert(value)
64
+ value = all_values_as_string_convert(value)
65
+ value = array_to_hash(value)
66
+ hash[var] = value
67
+ end
68
+ hash
69
+ end
70
+
71
+ alias_method :to_h, :to_hash
72
+
73
+ def non_nil_values
74
+ hash = {}
75
+ to_hash.each { |k, v| hash[k] = v unless v.nil? }
76
+ hash
77
+ end
78
+
79
+ def ==(obj)
80
+ self.hash == obj.hash
81
+ end
82
+
83
+ def hash
84
+ self.to_hash.hash
85
+ end
86
+
87
+ def to_enum
88
+ copy = self.dup
89
+ copy.singleton_class.send(:include, Enumerable)
90
+
91
+ def copy.each(&block)
92
+ self.class.permissible_methods.each do |item|
93
+ block.call(public_send(item))
94
+ end
95
+ end
96
+
97
+ copy
98
+ end
99
+
100
+ private
101
+
102
+ attr_reader :big_decimal_as_string, :all_values_as_string
103
+
104
+ def big_decimal_as_string_convert(object)
105
+ val = if big_decimal_as_string
106
+ object.to_s if object.class <= BigDecimal
107
+ end
108
+ return object if val.nil?
109
+ val
110
+ end
111
+
112
+ def all_values_as_string_convert(object)
113
+ val = if all_values_as_string
114
+ object.to_s if object.respond_to?(:to_s)
115
+ end
116
+ return object if val.nil?
117
+ val
118
+ end
119
+
120
+ def to_hash_object(object)
121
+ if object.respond_to?(:to_hash) && object.class <= AttrPermit
122
+ value = object.to_hash(big_decimal_as_string: big_decimal_as_string, all_values_as_string: all_values_as_string)
123
+ end
124
+ if object.respond_to?(:to_hash) && !(object.class <= AttrPermit)
125
+ value = object.to_hash
126
+ end
127
+ return object if value.nil?
128
+ value
129
+ end
130
+
131
+ def array_to_hash(object)
132
+ if object.class <= Array
133
+ value = object.map do |v|
134
+ to_hash_object(v)
135
+ end
136
+ end
137
+ return object if value.nil?
138
+ value
139
+ end
140
+
141
+ end
@@ -0,0 +1,3 @@
1
+ class AttrPermit
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: attr_permit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Dustin Zeisler
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: virtus
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '4.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '4.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.6'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description: Simple parametable object creator with lazy loading options. Objects
84
+ can be hashable and optionally convert all values to strings.
85
+ email:
86
+ - dustin@zive.me
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - attr_permit.gemspec
97
+ - lib/attr_permit.rb
98
+ - lib/attr_permit/version.rb
99
+ homepage: https://github.com/zeisler/attr_permit
100
+ licenses:
101
+ - MIT
102
+ metadata: {}
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ requirements: []
118
+ rubyforge_project:
119
+ rubygems_version: 2.2.2
120
+ signing_key:
121
+ specification_version: 4
122
+ summary: Simple parametable object creator with lazy loading options.
123
+ test_files: []