fattr 2.2.2 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -212,7 +212,8 @@ SAMPLES
212
212
 
213
213
  ~ > ruby samples/e.rb
214
214
 
215
- #<Config:0x2cd1c @port=80, @host="codeforpeople.org">
215
+ samples/e.rb:7: Use RbConfig instead of obsolete and deprecated Config.
216
+ samples/e.rb:7:in `<main>': Config is not a class (TypeError)
216
217
 
217
218
 
218
219
  <========< samples/f.rb >========>
@@ -317,8 +318,42 @@ SAMPLES
317
318
  "forty-two"
318
319
 
319
320
 
321
+ <========< samples/i.rb >========>
322
+
323
+ ~ > cat samples/i.rb
324
+
325
+ #
326
+ # you can retrieve all fattrs as a list, or a hash with values included
327
+ #
328
+ require 'fattr'
329
+
330
+ class C
331
+ fattr(:a)
332
+ fattr(:b){ a.to_f }
333
+ end
334
+
335
+ o = C.new
336
+
337
+ o.fattr(:c)
338
+ o.fattr(:d){ self.c.upcase }
339
+
340
+ o.a = 42
341
+ o.c = 'forty-two'
342
+
343
+ p o.fattrs.to_hash #=> {"a"=>42, "b"=>42.0, "c"=>"forty-two", "d"=>"FORTY-TWO"}
344
+ p o.fattrs #=> ["c", "d"]
345
+
346
+ ~ > ruby samples/i.rb
347
+
348
+ {"a"=>42, "b"=>42.0, "c"=>"forty-two", "d"=>"FORTY-TWO"}
349
+ ["c", "d"]
350
+
351
+
320
352
 
321
353
  HISTORY
354
+ 2.3.0
355
+ support for "object.fattrs.to_hash"
356
+
322
357
  2.0.0:
323
358
  support class/module inheritable attributes
324
359
 
data/README.erb CHANGED
@@ -54,6 +54,9 @@ SAMPLES
54
54
  <%= samples %>
55
55
 
56
56
  HISTORY
57
+ 2.3.0
58
+ support for "object.fattrs.to_hash"
59
+
57
60
  2.0.0:
58
61
  support class/module inheritable attributes
59
62
 
@@ -0,0 +1,48 @@
1
+ ## fattr.gemspec
2
+ #
3
+
4
+ Gem::Specification::new do |spec|
5
+ spec.name = "fattr"
6
+ spec.version = "2.3.0"
7
+ spec.platform = Gem::Platform::RUBY
8
+ spec.summary = "fattr"
9
+ spec.description = "a \"fatter attr\" for ruby"
10
+ spec.license = "same as ruby's"
11
+
12
+ spec.files =
13
+ ["LICENSE",
14
+ "README",
15
+ "README.erb",
16
+ "Rakefile",
17
+ "fattr.gemspec",
18
+ "lib",
19
+ "lib/fattr.rb",
20
+ "samples",
21
+ "samples/a.rb",
22
+ "samples/b.rb",
23
+ "samples/c.rb",
24
+ "samples/d.rb",
25
+ "samples/e.rb",
26
+ "samples/f.rb",
27
+ "samples/g.rb",
28
+ "samples/h.rb",
29
+ "samples/i.rb",
30
+ "test",
31
+ "test/fattr_test.rb"]
32
+
33
+ spec.executables = []
34
+
35
+ spec.require_path = "lib"
36
+
37
+ spec.test_files = nil
38
+
39
+ ### spec.add_dependency 'lib', '>= version'
40
+ #### spec.add_dependency 'map'
41
+
42
+ spec.extensions.push(*[])
43
+
44
+ spec.rubyforge_project = "codeforpeople"
45
+ spec.author = "Ara T. Howard"
46
+ spec.email = "ara.t.howard@gmail.com"
47
+ spec.homepage = "https://github.com/ahoward/fattr"
48
+ end
@@ -1,5 +1,5 @@
1
1
  module Fattr
2
- Fattr::Version = '2.2.2' unless Fattr.const_defined?(:Version)
2
+ Fattr::Version = '2.3.0' unless Fattr.const_defined?(:Version)
3
3
  def Fattr.version() Fattr::Version end
4
4
 
5
5
  def Fattr.description
@@ -7,6 +7,12 @@ module Fattr
7
7
  end
8
8
 
9
9
  class List < ::Array
10
+ attr_accessor :object
11
+
12
+ def initialize(*args, &block)
13
+ super(*args, &block)
14
+ end
15
+
10
16
  def << element
11
17
  super
12
18
  self
@@ -28,11 +34,36 @@ module Fattr
28
34
  def initializers
29
35
  @initializers ||= Hash.new
30
36
  end
37
+
38
+ def to_hash
39
+ if @object
40
+ list = @object.class.fattrs + @object.fattrs
41
+ list.inject(Hash.new){|hash, fattr| hash.update(fattr => @object.send(fattr))}
42
+ end
43
+ end
44
+
45
+ def to_h
46
+ to_hash
47
+ end
48
+
49
+ def for(object)
50
+ @object = object
51
+ self
52
+ end
53
+ end
54
+
55
+ class Result < ::Hash
56
+ attr_accessor :object
57
+
58
+ def for(object)
59
+ @object = object
60
+ self
61
+ end
31
62
  end
32
63
 
33
64
  def fattrs(*args, &block)
34
65
  unless args.empty?
35
- returned = Hash.new
66
+ returned = Fattr::Result.new
36
67
 
37
68
  args.flatten!
38
69
  args.compact!
@@ -186,13 +217,13 @@ class Module
186
217
  def Fattrs(*args, &block)
187
218
  class << self
188
219
  self
189
- end.module_eval{ __fattrs__(*args, &block) }
220
+ end.module_eval{ __fattrs__(*args, &block) }.for(self)
190
221
  end
191
222
 
192
223
  def Fattr(*args, &block)
193
224
  class << self
194
225
  self
195
- end.module_eval{ __fattr__(*args, &block) }
226
+ end.module_eval{ __fattr__(*args, &block) }.for(self)
196
227
  end
197
228
  end
198
229
 
@@ -200,7 +231,7 @@ class Object
200
231
  def fattrs(*args, &block)
201
232
  class << self
202
233
  self
203
- end.__fattrs__(*args, &block)
234
+ end.__fattrs__(*args, &block).for(self)
204
235
  end
205
236
  %w( __fattrs__ __fattr__ fattr ).each{|dst| alias_method(dst, 'fattrs')}
206
237
  end
@@ -0,0 +1,20 @@
1
+ #
2
+ # you can retrieve all fattrs as a list, or a hash with values included
3
+ #
4
+ require 'fattr'
5
+
6
+ class C
7
+ fattr(:a)
8
+ fattr(:b){ a.to_f }
9
+ end
10
+
11
+ o = C.new
12
+
13
+ o.fattr(:c)
14
+ o.fattr(:d){ self.c.upcase }
15
+
16
+ o.a = 42
17
+ o.c = 'forty-two'
18
+
19
+ p o.fattrs.to_hash #=> {"a"=>42, "b"=>42.0, "c"=>"forty-two", "d"=>"FORTY-TWO"}
20
+ p o.fattrs #=> ["c", "d"]
@@ -99,6 +99,17 @@ Testing Fattr do
99
99
  assert{ c.fattrs.include?(attr.to_sym) }
100
100
  end
101
101
  end
102
+
103
+ testing 'that all fattrs can be got via object.fattrs.to_hash' do
104
+ c = Class.new{ fattr :a; fattr :b; fattr(:c){ 'forty-two' } }
105
+ o = c.new
106
+ o.a = 42
107
+ o.b = 42.0
108
+ hash = assert{ o.fattrs.to_hash }
109
+ assert{ hash['a'] == 42 }
110
+ assert{ hash['b'] == 42.0 }
111
+ assert{ hash['c'] == 'forty-two' }
112
+ end
102
113
  end
103
114
 
104
115
 
metadata CHANGED
@@ -1,14 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fattr
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.2
4
+ version: 2.3.0
5
+ prerelease:
5
6
  platform: ruby
6
7
  authors:
7
8
  - Ara T. Howard
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2014-02-15 00:00:00.000000000 Z
12
+ date: 2016-03-29 00:00:00.000000000 Z
12
13
  dependencies: []
13
14
  description: a "fatter attr" for ruby
14
15
  email: ara.t.howard@gmail.com
@@ -20,6 +21,7 @@ files:
20
21
  - README
21
22
  - README.erb
22
23
  - Rakefile
24
+ - fattr.gemspec
23
25
  - lib/fattr.rb
24
26
  - samples/a.rb
25
27
  - samples/b.rb
@@ -29,29 +31,32 @@ files:
29
31
  - samples/f.rb
30
32
  - samples/g.rb
31
33
  - samples/h.rb
34
+ - samples/i.rb
32
35
  - test/fattr_test.rb
33
36
  homepage: https://github.com/ahoward/fattr
34
37
  licenses:
35
38
  - same as ruby's
36
- metadata: {}
37
39
  post_install_message:
38
40
  rdoc_options: []
39
41
  require_paths:
40
42
  - lib
41
43
  required_ruby_version: !ruby/object:Gem::Requirement
44
+ none: false
42
45
  requirements:
43
46
  - - ! '>='
44
47
  - !ruby/object:Gem::Version
45
48
  version: '0'
46
49
  required_rubygems_version: !ruby/object:Gem::Requirement
50
+ none: false
47
51
  requirements:
48
52
  - - ! '>='
49
53
  - !ruby/object:Gem::Version
50
54
  version: '0'
51
55
  requirements: []
52
56
  rubyforge_project: codeforpeople
53
- rubygems_version: 2.0.3
57
+ rubygems_version: 1.8.23.2
54
58
  signing_key:
55
- specification_version: 4
59
+ specification_version: 3
56
60
  summary: fattr
57
61
  test_files: []
62
+ has_rdoc:
checksums.yaml DELETED
@@ -1,15 +0,0 @@
1
- ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- YWU4MGI5MmUxYWE3OWIzNTgwYmE0YWQ0MjY0MzRkNjY1MDk5MmI3NQ==
5
- data.tar.gz: !binary |-
6
- ODcwMDUwNDYxYjAwYTBhYWFjM2Q2MzRmODlmMzNiYWM3ZTBmZWIzYQ==
7
- !binary "U0hBNTEy":
8
- metadata.gz: !binary |-
9
- NmE5NWY1NGEzYzNmZGRjNDFkMmUwMThhNWUzYTFlZTI3YTBjZTkxMDhjMGU3
10
- NDNlMDY5YzY3NDIyZjAxZWYyMGNiNzFiOTYxNGE1ZDFiMWE4MTRmMTk3OTRm
11
- ODNmNDU5NDhmM2M3ZWQ3NTRiZWYzYzM4ZmJhYmNkMzYwZGZiNTc=
12
- data.tar.gz: !binary |-
13
- MDJlODcxNDZmMDY4YTBlMWI3NGUxNWJjMzZkOWY5MzQ1MDhiN2MxYzY3Yzdk
14
- YTg2NWQxYTQxMTExNjgxNGRlNTVmZTU0Yzg2YTY2MTVhNWUyNGJjZjhhZTZj
15
- OWQxNTExODQ5YmMzMjk4MDQwNmU2ODU3MDE4ODI4Zjk5Njc2YzQ=