etter 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b9402fe25c21a2e68bfd57650804558602c41453
4
+ data.tar.gz: ad5987ebc8d8c7a98e248db2d24ff07c580f5514
5
+ SHA512:
6
+ metadata.gz: 49274ba1d414859c9e1ac1309d5c59e5be837dc778123f93eebb0e9959028db8b75d4cb2044399478d3962653b76adadb96b06e3d78f3f29b9f2a2772ed1bfe2
7
+ data.tar.gz: e80a1095b811b7be7e896c445e6c328bd768f68264016ca81c008e821c18ed9255e60809dac76e0b7b89dca44753cfffded5e4d5604a304d986db09f025bf111
data/lib/etter.rb ADDED
@@ -0,0 +1 @@
1
+ require_relative "etter/etter"
@@ -0,0 +1,65 @@
1
+ #
2
+ # Top level namespace for Etter. Behaves somewhat like a class, in that you can
3
+ # send +::new+ to it, but will not return anything other than a +String+ or
4
+ # +Sumbol+
5
+ #
6
+ module Etter
7
+ ERROR = "is not allowed as an instance variable name"
8
+
9
+ class << self
10
+ #call-seq:
11
+ # new(string) => string
12
+ # new(symbol) => symbol
13
+ def new(obj)
14
+ attribute = _attrify(obj)
15
+ if obj.is_a? Symbol
16
+ return attribute.intern
17
+ else
18
+ return attribute
19
+ end
20
+ end
21
+
22
+ def getter(obj)
23
+ _validate_internal obj
24
+ obj.intern
25
+ end
26
+
27
+ def setter(obj)
28
+ _validate_internal obj
29
+ (obj.to_s + "=").intern
30
+ end
31
+
32
+ private
33
+ def _attrify(obj)
34
+ _validate_internal(obj)
35
+ obj = literal2snk(obj)
36
+ if obj[0] == '@'
37
+ return obj
38
+ else
39
+ return obj.prepend '@'
40
+ end
41
+ end
42
+
43
+ def literal2snk(obj)
44
+ rep = obj.to_s.gsub('-', '_')
45
+ rep
46
+ end
47
+
48
+ def _validate_internal(obj)
49
+ raise NameError, "`#{obj}' #{ERROR}" unless
50
+ obj.is_a?(String) || obj.is_a?(Symbol)
51
+ raise NameError, "`#{obj}' #{ERROR}" unless
52
+ obj[0] =~ /[a-zA-Z@]/
53
+ raise NameError, "`#{obj}' #{ERROR}" unless
54
+ obj.to_s[1..-1].scan(/[^a-zA-Z0-9-]/).empty?
55
+ end
56
+
57
+ def self.extended(mod)
58
+ raise "cannot extend this module. "
59
+ end
60
+
61
+ end
62
+ end
63
+
64
+ require_relative "string"
65
+ require_relative "symbol"
@@ -0,0 +1,13 @@
1
+ class String
2
+ def to_attr
3
+ Etter.new self
4
+ end
5
+
6
+ def to_setter
7
+ Etter.setter self
8
+ end
9
+
10
+ def to_getter
11
+ Etter.getter self
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ class Symbol
2
+ def to_attr
3
+ Etter.new self
4
+ end
5
+
6
+ def to_setter
7
+ Etter.setter self
8
+ end
9
+
10
+ def to_getter
11
+ Etter.getter self
12
+ end
13
+ end
@@ -0,0 +1,137 @@
1
+ require_relative "etter"
2
+
3
+ #
4
+ # Provides quick and easy ways to set attributes with type restrictions.
5
+ # Also, optionally enforces scoping, in case you have some attributes that you
6
+ # want to remain private or protected but need the dynamic handling allowed
7
+ # by +setter+ and +getter+
8
+ #
9
+ # Of course, in Ruby, type is only a suggestion, and not a requirement,
10
+ # so there are ways of getting around these settings.
11
+ #
12
+ # To use this, +extend+ your class with +Etter::Typed+
13
+ # (+include+ will work... but it doesn't really do you any good)
14
+ module Etter::Typed
15
+
16
+ # This method is not very different from +attr_reader+, but is
17
+ # provided because in the future, it may do something such as
18
+ # taint the returned object if it does not match the specified
19
+ # type, or maybe even attempt conversion, e.g.
20
+ # class Example
21
+ # getter :one,
22
+ # type: Integer
23
+ # def initialize(one)
24
+ # @one = one
25
+ # p @one
26
+ # end
27
+ # end
28
+ # ex = Example.new "1"
29
+ # # => "1"
30
+ # p ex.one
31
+ # # => 1
32
+ # Also provides a +scope+ parameter which can be set to
33
+ # :public
34
+ # :protected
35
+ # :private
36
+ def getter(name, type: Object, scope: :public)
37
+ define_method(name.to_getter) do
38
+ get_attr name
39
+ end
40
+
41
+ case scope
42
+ when :public, "public"
43
+ public name.to_getter
44
+ when :protected, "protected"
45
+ protected name.to_getter
46
+ when :private, "private"
47
+ private name.to_getter
48
+ else
49
+ raise NameError, <<~EOF
50
+ #{scope.inspect} is not a valid scope
51
+ Did you mean? public
52
+ protected
53
+ private
54
+ EOF
55
+ end
56
+ end
57
+
58
+ #
59
+ # Like +attr_writer+ except that when the instance methods
60
+ # it creates are called, the incoming object is checked
61
+ # against the class provided with the +type+ parameter
62
+ #
63
+ # Also provides a +scope+ parameter which can be set to
64
+ # :public
65
+ # :protected
66
+ # :private
67
+ #
68
+ def setter(name, type: Object, scope: :public)
69
+ define_method(name.to_setter) do |value|
70
+ raise TypeError, "#{name.to_attr} must be a #{type}" unless
71
+ value.is_a? type
72
+ set_attr name, value
73
+ end
74
+
75
+ case scope
76
+ when :public, "public"
77
+ public name.to_setter
78
+ when :protected, "protected"
79
+ protected name.to_setter
80
+ when :private, "private"
81
+ private name.to_setter
82
+ else
83
+ raise NameError, <<~EOF
84
+ #{scope.inspect} is not a valid scope
85
+ Did you mean? public
86
+ protected
87
+ private
88
+ EOF
89
+ end
90
+ end
91
+
92
+ #
93
+ # Like +attr_accessor+ , but encompasses the properties mentioned
94
+ # in +setter+ and +getter+
95
+ #
96
+ def property(name, type: Object, scope: :public)
97
+ getter name,
98
+ type: type,
99
+ scope: scope
100
+
101
+ setter name,
102
+ type: type,
103
+ scope: scope
104
+ end
105
+
106
+ private
107
+ def self.extended(mod)
108
+ mod.include(InstanceMethods)
109
+ end
110
+
111
+ #
112
+ # Automatically +include+ d when +Etter::Typed+ extends
113
+ # a class
114
+ #
115
+ module InstanceMethods
116
+ private
117
+
118
+ #
119
+ # Gets an instance variable, accepting either of the following:
120
+ # get_attr :name
121
+ # get_attr :@name
122
+ #
123
+ def get_attr(name) # :doc:
124
+ instance_variable_get Etter.new(name)
125
+ end
126
+
127
+ #
128
+ # Sets an instance variable, accepting either of the following:
129
+ # set_attr :name, "value"
130
+ # set_attr :@name, "value"
131
+ #
132
+ def set_attr(name, value) # :doc:
133
+ instance_variable_set Etter.new(name), value
134
+ end
135
+ end
136
+
137
+ end
@@ -0,0 +1,3 @@
1
+ module Etter
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: etter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Carl Frederick
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-06-14 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: "...for Classes and Instances!"
14
+ email:
15
+ - galvertez@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/etter.rb
21
+ - lib/etter/etter.rb
22
+ - lib/etter/string.rb
23
+ - lib/etter/symbol.rb
24
+ - lib/etter/typed.rb
25
+ - lib/etter/version.rb
26
+ homepage: https://github.com/galvertez/etter
27
+ licenses:
28
+ - MIT
29
+ metadata: {}
30
+ post_install_message:
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubyforge_project:
46
+ rubygems_version: 2.6.7
47
+ signing_key:
48
+ specification_version: 4
49
+ summary: Setters and Getters and Properties...
50
+ test_files: []