tbpgr_utils 0.0.89 → 0.0.90

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -85,7 +85,8 @@ Or install it yourself as:
85
85
  |[TestToolbox Kernel#dp_line](#kerneldp_line) |debug print line for print-debugging |
86
86
  |[TbpgrUtils Kernel#aa_ancestors](#kernelaa_ancestors) |Ascii Art Ancestors |
87
87
  |[TbpgrUtils Kernel#bulk_puts_eval](#kernelbulk_puts_eval) |Puts each-line-code + eval result |
88
- |[TbpgrUtils Kernel#evalb](#kernelevalb) |eval block version |
88
+ |[TbpgrUtils Kernel#evalb](#kernelevalb) |set attributes from hash |
89
+ |[TbpgrUtils Kernel#hash_to_attributes](#kernelhash_to_attributes) |eval block version |
89
90
  |[TbpgrUtils Kernel#null](#kernelnull) |null is alias of nil |
90
91
  |[TbpgrUtils Kernel#print_eval](#kernelprint_eval) |Print code + eval result |
91
92
  |[TbpgrUtils Kernel#puts_eval](#kernelputs_eval) |Puts code + eval result |
@@ -1469,6 +1470,25 @@ print actual # => 4
1469
1470
 
1470
1471
  [back to list](#list)
1471
1472
 
1473
+ ### Kernel#hash_to_attributes
1474
+ ~~~ruby
1475
+ require 'tbpgr_utils'
1476
+
1477
+ class Person
1478
+ attr_accessor :name, :age
1479
+ end
1480
+
1481
+ person = Person.new
1482
+ person.hash_to_attributes({name: 'hoge', age: 33})
1483
+ ~~~
1484
+
1485
+ result
1486
+ ~~~ruby
1487
+ #<PersonForHashToAttributes:0x3957858 @age=33, @not_exists="hoge">
1488
+ ~~~
1489
+
1490
+ [back to list](#list)
1491
+
1472
1492
  ### Kernel#null
1473
1493
  ~~~ruby
1474
1494
  require 'tbpgr_utils'
@@ -2954,6 +2974,7 @@ if you are Sublime Text2 user, you can use snippet for TbpgrUtils.
2954
2974
  https://github.com/tbpgr/tbpgr_utils_snippets
2955
2975
 
2956
2976
  ## History
2977
+ * version 0.0.90 : add Kernel#hash_to_attributes
2957
2978
  * version 0.0.89 : add String#escape_quote
2958
2979
  * version 0.0.88 : add String#escape_double_quote
2959
2980
  * version 0.0.87 : add String#spacing
@@ -0,0 +1,23 @@
1
+ # encoding: utf-8
2
+
3
+ # Kernel
4
+ module Kernel
5
+ # set attributes from hash
6
+ #
7
+ # === Example
8
+ #
9
+ # class Person
10
+ # attr_accessor :name, :age
11
+ # end
12
+ #
13
+ # person = Person.new
14
+ # person.hash_to_attributes({name: 'hoge', age: 33})
15
+ #
16
+ # result
17
+ #
18
+ # #<PersonForHashToAttributes:0x3957858 @age=33, @not_exists="hoge">
19
+ #
20
+ def hash_to_attributes(hash)
21
+ hash.each { |key, value|instance_variable_set "@#{key}", value }
22
+ end
23
+ end
@@ -4,5 +4,6 @@ require 'open_classes/kernel/aa_ancestors'
4
4
  require 'open_classes/kernel/booleans'
5
5
  require 'open_classes/kernel/bulk_define_methods'
6
6
  require 'open_classes/kernel/evalb'
7
+ require 'open_classes/kernel/hash_to_attributes'
7
8
  require 'open_classes/kernel/null'
8
9
  require 'open_classes/kernel/p_evals'
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Tbpgr Utilities
4
4
  module TbpgrUtils
5
- VERSION = '0.0.89'
5
+ VERSION = '0.0.90'
6
6
  end
@@ -11,7 +11,7 @@ describe Familyable::Family do
11
11
  persons: [
12
12
  a = Familyable::Person.new(id: 1, parent_ids: [2, 3]),
13
13
  b = Familyable::Person.new(id: 2, parent_ids: []),
14
- c = Familyable::Person.new(id: 3, parent_ids: [4], ),
14
+ c = Familyable::Person.new(id: 3, parent_ids: [4],),
15
15
  d = Familyable::Person.new(id: 4, parent_ids: []),
16
16
  e = Familyable::Person.new(id: 5, parent_ids: [2]),
17
17
  ],
@@ -59,7 +59,7 @@ describe Familyable::Family do
59
59
  persons: [
60
60
  a = Familyable::Person.new(id: 1, parent_ids: [2, 3]),
61
61
  b = Familyable::Person.new(id: 2, parent_ids: []),
62
- c = Familyable::Person.new(id: 3, parent_ids: [4], ),
62
+ c = Familyable::Person.new(id: 3, parent_ids: [4],),
63
63
  d = Familyable::Person.new(id: 4, parent_ids: []),
64
64
  e = Familyable::Person.new(id: 5, parent_ids: [2]),
65
65
  ],
@@ -107,7 +107,7 @@ describe Familyable::Family do
107
107
  persons: [
108
108
  a = Familyable::Person.new(id: 1, parent_ids: [2, 3]),
109
109
  b = Familyable::Person.new(id: 2, parent_ids: []),
110
- c = Familyable::Person.new(id: 3, parent_ids: [4], ),
110
+ c = Familyable::Person.new(id: 3, parent_ids: [4],),
111
111
  d = Familyable::Person.new(id: 4, parent_ids: [3]),
112
112
  e = Familyable::Person.new(id: 5, parent_ids: [2]),
113
113
  ],
@@ -0,0 +1,52 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+ require 'open_classes/kernel'
4
+
5
+ describe Kernel do
6
+ context :hash_to_attributes do
7
+ class PersonForHashToAttributes
8
+ attr_accessor :name, :age
9
+ end
10
+
11
+ cases = [
12
+ {
13
+ case_no: 1,
14
+ case_title: 'valid hash case',
15
+ hash: {
16
+ name: 'hoge',
17
+ age: 33,
18
+ },
19
+ expected_name: 'hoge',
20
+ expected_age: 33,
21
+ },
22
+ ]
23
+
24
+ cases.each do |c|
25
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
26
+ begin
27
+ case_before c
28
+
29
+ # -- given --
30
+ person = PersonForHashToAttributes.new
31
+
32
+ # -- when --
33
+ person.hash_to_attributes c[:hash]
34
+
35
+ # -- then --
36
+ expect(person.name).to eq(c[:expected_name])
37
+ expect(person.age).to eq(c[:expected_age])
38
+ ensure
39
+ case_after c
40
+ end
41
+ end
42
+
43
+ def case_before(c)
44
+ # implement each case before
45
+ end
46
+
47
+ def case_after(c)
48
+ # implement each case after
49
+ end
50
+ end
51
+ end
52
+ end
@@ -3,17 +3,17 @@ require 'spec_helper'
3
3
  require 'open_classes/string/escape_double_quote'
4
4
 
5
5
  describe String do
6
- context :escape_double_quote do
6
+ context :escape_quote do
7
7
  cases = [
8
8
  {
9
9
  case_no: 1,
10
- case_title: 'exist double quote case',
11
- input: 'hoge"h"ige',
12
- expected: 'hoge""h""ige',
10
+ case_title: 'exist quote case',
11
+ input: "hoge'h'ige",
12
+ expected: "hoge''h''ige",
13
13
  },
14
14
  {
15
15
  case_no: 2,
16
- case_title: 'not exist double quote case',
16
+ case_title: 'not exist quote case',
17
17
  input: 'hogehige',
18
18
  expected: 'hogehige',
19
19
  },
@@ -28,7 +28,7 @@ describe String do
28
28
  # nothing
29
29
 
30
30
  # -- when --
31
- actual = c[:input].escape_double_quote
31
+ actual = c[:input].escape_quote
32
32
 
33
33
  # -- then --
34
34
  expect(actual).to eq(c[:expected])
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tbpgr_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.89
4
+ version: 0.0.90
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-04-06 00:00:00.000000000 Z
12
+ date: 2014-04-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
16
- requirement: &29555100 !ruby/object:Gem::Requirement
16
+ requirement: &27072132 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 4.0.1
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *29555100
24
+ version_requirements: *27072132
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: bundler
27
- requirement: &29554812 !ruby/object:Gem::Requirement
27
+ requirement: &27071844 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '1.3'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *29554812
35
+ version_requirements: *27071844
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rake
38
- requirement: &29554584 !ruby/object:Gem::Requirement
38
+ requirement: &27071616 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *29554584
46
+ version_requirements: *27071616
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rspec
49
- requirement: &29554260 !ruby/object:Gem::Requirement
49
+ requirement: &27071292 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 2.14.1
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *29554260
57
+ version_requirements: *27071292
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: simplecov
60
- requirement: &29553960 !ruby/object:Gem::Requirement
60
+ requirement: &27070992 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ~>
@@ -65,7 +65,7 @@ dependencies:
65
65
  version: 0.8.2
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *29553960
68
+ version_requirements: *27070992
69
69
  description: Utilities
70
70
  email:
71
71
  - tbpgr@tbpgr.jp
@@ -147,6 +147,7 @@ files:
147
147
  - lib/open_classes/kernel/booleans.rb
148
148
  - lib/open_classes/kernel/bulk_define_methods.rb
149
149
  - lib/open_classes/kernel/evalb.rb
150
+ - lib/open_classes/kernel/hash_to_attributes.rb
150
151
  - lib/open_classes/kernel/null.rb
151
152
  - lib/open_classes/kernel/p_evals.rb
152
153
  - lib/open_classes/module.rb
@@ -250,6 +251,7 @@ files:
250
251
  - spec/open_classes/kernel/bulk_define_methods_spec.rb
251
252
  - spec/open_classes/kernel/bulk_puts_eval_spec.rb
252
253
  - spec/open_classes/kernel/evalb_spec.rb
254
+ - spec/open_classes/kernel/hash_to_attributes_spec.rb
253
255
  - spec/open_classes/kernel/null_spec.rb
254
256
  - spec/open_classes/kernel/print_eval_spec.rb
255
257
  - spec/open_classes/kernel/puts_eval_spec.rb
@@ -374,6 +376,7 @@ test_files:
374
376
  - spec/open_classes/kernel/bulk_define_methods_spec.rb
375
377
  - spec/open_classes/kernel/bulk_puts_eval_spec.rb
376
378
  - spec/open_classes/kernel/evalb_spec.rb
379
+ - spec/open_classes/kernel/hash_to_attributes_spec.rb
377
380
  - spec/open_classes/kernel/null_spec.rb
378
381
  - spec/open_classes/kernel/print_eval_spec.rb
379
382
  - spec/open_classes/kernel/puts_eval_spec.rb