such 0.0.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: 034ecc706d9a4c43a76d423f2c923a6a0d83f1e6
4
+ data.tar.gz: 046e83830b08e094b1c3bf5e6105eebf1e15337e
5
+ SHA512:
6
+ metadata.gz: cb4627c8552d8b29a0f688428daac053bfcfab9aec5a35c463ea10600344696cce34e2f9860c7d7dd0a0a4b01a9aa1d71ee43ab2f5ec5db262b8f355bb14af58
7
+ data.tar.gz: e6456431c3c453541111db1afd0311ddfd1331c61db3557594f8a4f3ae7bc97d351794138f98a4d51780b53bec5ca35bf5824fc0b4a6e17bfebb0cdb0ece5d8e
data/README.rdoc ADDED
@@ -0,0 +1,101 @@
1
+ = Such
2
+
3
+ == DESCRIPTION:
4
+
5
+ Wraps Ruby's Gtk widgets with an alternate constructor
6
+ which factors out the configuration and assembly procedures into metadata.
7
+
8
+ And the _Such_ module can be used to wrap any class with the alternate constructor,
9
+ although currently only _Gtk3_ widgets is supported.
10
+
11
+ == SYNOPSIS:
12
+
13
+ require 'gtk3'
14
+ require 'such'
15
+ include Such; Things.gtk_widget
16
+
17
+ Thing.configure window: {
18
+ set_title: 'Synopsis Example',
19
+ set_window_position: :center },
20
+ BUTTON: [label: 'Button!'],
21
+ button: {set_size_request: [100,50], into:[:add]}
22
+
23
+ window = Window.new(:window, 'destroy'){Gtk.main_quit}
24
+ Button.new(window, :button!){puts 'Button pressed!'}
25
+ window.show_all
26
+
27
+ Gtk.main
28
+
29
+ == MORE:
30
+
31
+ Arrays are passed to constructer's super,
32
+ Hashes are method=>arguments pairs, and Strings are signals.
33
+ Other objects are assumed to be containers:
34
+
35
+ Such::Button.new(window, [label: 'Hello!'], {set_size_request:[100,50]}, 'clicked' ){puts 'OK'}
36
+
37
+ # Is equivalent to
38
+
39
+ button = Gtk::Button.new(label:'Hello!')
40
+ window.add button
41
+ button.set_size_request 100, 50
42
+ button.signal_connect('clicked'){puts 'OK'}
43
+
44
+ To set the packing method to say, :pack_start, set the :into method as follows:
45
+
46
+ {into: [:pack_start, expand:false, fill:false, padding:0]}
47
+ # The effect in the contructor will be as if the following was run:
48
+ # container.pack_start(self, expand:false, fill:false, padding:0)
49
+
50
+ One can configure Symbol keys to represent metadata about a widget:
51
+
52
+ Thing.configure(
53
+ KEY: [ arg1, arg2, arg3 ], # an array for super(arg1, arg2, arg3)
54
+ key: { # a hash for like self.method(args)
55
+ meth1:args1,
56
+ meth2:[args2a, args2b],
57
+ meth3:[a,b,c,d]
58
+ },
59
+ key!: [[arg1, arg2], {meth1:args1, meth2:args2}, 'signal1', 'signal2'] # the splatter bang!
60
+ )
61
+
62
+ The examples in this repository are reworks of the examples given in
63
+ ZetCode.com[http://zetcode.com/gui/rubygtk/].
64
+
65
+ === Features:
66
+
67
+ * :key! content *splat
68
+ * Undefined :key! expanded to :KEY, :key
69
+ * Missing signal assumed to be 'clicked'
70
+ * Heuristics on when one wants to iterate a method over the arguments given
71
+ * Packing method defaults (ultimately) to :add
72
+ * Way to change default packing behaviour
73
+
74
+ == INSTALL:
75
+
76
+ $ sudo gem install such
77
+
78
+ == LICENSE:
79
+
80
+ (The MIT License)
81
+
82
+ Copyright (c) 2014
83
+
84
+ Permission is hereby granted, free of charge, to any person obtaining
85
+ a copy of this software and associated documentation files (the
86
+ 'Software'), to deal in the Software without restriction, including
87
+ without limitation the rights to use, copy, modify, merge, publish,
88
+ distribute, sublicense, and/or sell copies of the Software, and to
89
+ permit persons to whom the Software is furnished to do so, subject to
90
+ the following conditions:
91
+
92
+ The above copyright notice and this permission notice shall be
93
+ included in all copies or substantial portions of the Software.
94
+
95
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
96
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
97
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
98
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
99
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
100
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
101
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/lib/such/such.rb ADDED
@@ -0,0 +1,9 @@
1
+ module Such
2
+ def self.subclass(clss, sprclss="Gtk::#{clss}", body='include Such::Thing')
3
+ eval <<-CODE
4
+ class #{clss} < #{sprclss}
5
+ #{body}
6
+ end
7
+ CODE
8
+ end
9
+ end
data/lib/such/thing.rb ADDED
@@ -0,0 +1,128 @@
1
+ module Such
2
+ module Thing
3
+ INTOS = [:set_submenu, :pack_start, :append, :add] # TODO:GTK!?
4
+
5
+ PARAMETERS = {}
6
+ def self.configure(conf)
7
+ conf.each{|k,v| PARAMETERS[k]=v}
8
+ end
9
+
10
+ def self.trace_method(obj, mthd, args)
11
+ $stderr.puts "#{obj.class}##{mthd}(#{[*args].join(',')})"
12
+ end
13
+
14
+ def self.trace_signal(obj, signal)
15
+ $stderr.puts "#{obj.class} links #{signal}"
16
+ end
17
+
18
+ def self.do_symbol(parameter, parameters)
19
+ if PARAMETERS.has_key?(parameter)
20
+ p = PARAMETERS[parameter]
21
+ (parameter[-1]=='!')? parameters.unshift(*p) : parameters.unshift(p)
22
+ else
23
+ if parameter[-1]=='!'
24
+ p = parameter[0..-2]
25
+ parameters.unshift(p.downcase.to_sym)
26
+ parameters.unshift(p.upcase.to_sym)
27
+ else
28
+ warn "Warning: Such::PARAMETERS[#{parameter}] not defined"
29
+ end
30
+ end
31
+ end
32
+
33
+ def self.do_parameters(parameters)
34
+ container, arguments, methods, signals = nil, [], {}, []
35
+ while parameter = parameters.shift
36
+ case parameter
37
+ when Symbol
38
+ # Symbols are expected to translate to something else.
39
+ Thing.do_symbol(parameter, parameters)
40
+ when Array
41
+ # Arrays are added to the Thing's arguments list.
42
+ arguments += parameter
43
+ when Hash
44
+ # Hashes are expected to be a symbol list of methods on Thing with respective arguments.
45
+ # It's possible to override a previously defined method with new arguments.
46
+ parameter.each{|k,v| methods[k]=v}
47
+ when String
48
+ # Typically a signal: Thing#signal_connect(signal){|*emits| block.call(*emits)}
49
+ signals.push(parameter)
50
+ else
51
+ # Assume it's a container
52
+ container = parameter
53
+ end
54
+ end
55
+ signals.uniq!
56
+ return container, arguments, methods, signals
57
+ end
58
+
59
+ def self.which_method(container, methods=INTOS)
60
+ methods.each{|mthd| return mthd if container.respond_to?(mthd)}
61
+ raise "Don't know how to put into #{container.class}."
62
+ end
63
+
64
+ def self.into(obj, container=nil, mthd=nil, *args)
65
+ if container
66
+ mthd=Thing.which_method(container) unless mthd
67
+ unless mthd.class==Symbol and container.respond_to?(mthd)
68
+ raise "Need container & method. Got #{container.class}##{mthd}(#{obj.class}...)"
69
+ end
70
+ Thing.trace_method(container, mthd, [obj.class,*args]) if $VERBOSE
71
+ container.method(mthd).call(obj, *args)
72
+ else
73
+ warn "Warning: Container for #{self.class} not given."
74
+ end
75
+ end
76
+
77
+ def self.do_method(obj, mthd, *args)
78
+ Thing.trace_method(obj, mthd, args) if $VERBOSE
79
+ m = obj.method(mthd)
80
+ begin
81
+ m.call(*args)
82
+ rescue ArgumentError, TypeError
83
+ # Assume user meant to iterate. Note that the heuristic is not perfect.
84
+ $stderr.puts "# Iterated Method #{mthd} ^^^" if $VERBOSE
85
+ [*args].each{|arg| m.call(*arg)}
86
+ if $!.class == ArgumentError and not m.arity == 1
87
+ warn "Warning: Iterated method's arity not one."
88
+ end
89
+ end
90
+ end
91
+
92
+ def self.do_methods(obj, methods, container=nil)
93
+ # If user does not specify how to add to container, assume default way.
94
+ methods[:into]=Thing.which_method(container) if container and !methods.has_key?(:into)
95
+ methods.each do |mthd, args|
96
+ (mthd==:into)? Thing.into(obj, container, *args) :
97
+ Thing.do_method(obj, mthd, *args)
98
+ end
99
+ end
100
+
101
+ def self.do_links(obj, signals, block)
102
+ none = (signals.length==0)
103
+ if block
104
+ signals.push('clicked') if block and none # TODO: GTK!?
105
+ signals.each do |signal|
106
+ Thing.trace_signal(obj, signal) if $VERBOSE
107
+ obj.signal_connect(signal){|*emits| block.call(*emits)} # TODO: GTK!?
108
+ end
109
+ elsif not none
110
+ warn "Warning: No block given for #{signals.join(',')} on #{obj.class}."
111
+ end
112
+ end
113
+
114
+ def self.do_config(obj, *parameters, &block)
115
+ container, arguments, methods, signals = Thing.do_parameters(parameters)
116
+ Thing.do_methods(obj, methods, container)
117
+ Thing.do_links(obj, signals, block)
118
+ warn "Warning: arguments not used in do_config(#{obj.class}...)." if arguments.length > 0
119
+ end
120
+
121
+ def initialize(*parameters, &block)
122
+ container, arguments, methods, signals = Thing.do_parameters(parameters)
123
+ super(*arguments)
124
+ Thing.do_methods(self, methods, container)
125
+ Thing.do_links(self, signals, block)
126
+ end
127
+ end
128
+ end
@@ -0,0 +1,11 @@
1
+ module Such
2
+ module Things
3
+ def self.list(clss=Gtk::Widget)
4
+ ObjectSpace.each_object(Class).select{|k| k < clss}
5
+ end
6
+
7
+ def self.gtk_widget
8
+ Things.list.each{|clss| Such.subclass(clss.name.sub('Gtk::',''))}
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module Such
2
+ VERSION = '0.0.0'
3
+ end
data/lib/such.rb ADDED
@@ -0,0 +1,6 @@
1
+ require 'such/version'
2
+ require 'such/thing'
3
+ require 'such/such'
4
+ require 'such/things'
5
+ # Requires:
6
+ #`ruby`
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: such
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - carlosjhr64
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-01 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: |
14
+ Wraps Ruby's Gtk widgets with an alternate constructor
15
+ which factors out the configuration and assembly procedures into metadata.
16
+
17
+ And the _Such_ module can be used to wrap any class with the alternate constructor,
18
+ although currently only _Gtk3_ widgets is supported.
19
+ email: carlosjhr64@gmail.com
20
+ executables: []
21
+ extensions: []
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ files:
25
+ - README.rdoc
26
+ - lib/such.rb
27
+ - lib/such/such.rb
28
+ - lib/such/thing.rb
29
+ - lib/such/things.rb
30
+ - lib/such/version.rb
31
+ homepage: https://github.com/carlosjhr64/such
32
+ licenses:
33
+ - MIT
34
+ metadata: {}
35
+ post_install_message:
36
+ rdoc_options:
37
+ - "--main"
38
+ - README.rdoc
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ requirements:
52
+ - 'ruby: ruby 2.1.3p242 (2014-09-19 revision 47630) [x86_64-linux]'
53
+ rubyforge_project:
54
+ rubygems_version: 2.4.1
55
+ signing_key:
56
+ specification_version: 4
57
+ summary: Wraps Ruby's Gtk widgets with an alternate constructor which factors out
58
+ the configuration and assembly procedures into metadata.
59
+ test_files: []
60
+ has_rdoc: