ruby-dmm 0.1.2 → 0.1.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 05ff67107c04c13451281c2ddce1037f703bc594
4
- data.tar.gz: c169bc92ef677da3de5b75d02d93b3bd09cf19da
3
+ metadata.gz: 79cc74c7709505fc6927020e46facf6b3e220ac7
4
+ data.tar.gz: 0869a4633cb3da9608f81290a514ba958e57a0a6
5
5
  SHA512:
6
- metadata.gz: d8c039f51ec9c66217dfba522a124fb1550269ba32178d4c9c7efd7bf9e3a1b02b52e935c76819162266c37d4408fbbe3cb9d539648fa37b3f79a61bf322fc89
7
- data.tar.gz: 032538ccbf823e0698bf0e280b37b47f1cd29a0ce7421ebdf4523bfa333c0bb86ce3d361a6527f2dbf8fb07283000b9ee67aa61fab7ca9d457b56da8127134b6
6
+ metadata.gz: 84d6e48892644274e3728a5a1ab2dd23064f75cf6f3aacd429cbd7d20113a3d82614be035a769fe94acf3e88d424650a8215353a30f1b9cc6b9624c5065334fb
7
+ data.tar.gz: 5c5e95763c5017e44177a19aade42d7b94cf65b7944a82b420edc0f56c2d29b6cb61f04daf68f6c5f214cd5cf8fdf368c098bf465224bd26226ca531d8bdb2d4
@@ -30,6 +30,7 @@ module DMM
30
30
 
31
31
  define_method(key) do |value|
32
32
  @params.update(key => value)
33
+ self
33
34
  end
34
35
  end
35
36
 
@@ -39,9 +39,7 @@ module DMM
39
39
  else
40
40
  self.class.class_eval do
41
41
  unless method_defined?(key)
42
- define_method "#{key}" do
43
- instance_variable_get("@#{key}")
44
- end
42
+ attr_reader key
45
43
  if name = ALIAS_METHOD_MAP[key.to_sym]
46
44
  alias_method name, key.to_sym
47
45
  end
@@ -2,36 +2,36 @@
2
2
  module DMM
3
3
  class Response
4
4
  class ItemInfo
5
- SINGLE_VALUE_ATTRIBUTES = [
6
- :author,
7
- :color,
8
- :director,
9
- :genre,
10
- :label,
11
- :maker,
12
- :series,
13
- :size,
14
- :type,
15
- ]
16
- MULTIPLE_VALUES_ATTRIBUTES = {
17
- :actors => :actor,
18
- :actresses => :actress,
19
- :artists => :artist,
20
- :fighters => :fighter,
21
- :keywords => :keyword,
5
+
6
+ # for defining
7
+ # alias_method :actors, :actor
8
+ PLURAL_MAP = {
9
+ :actor => :actors,
10
+ :actress => :actresses,
11
+ :artist => :artists,
12
+ :author => :authors,
13
+ :color => :colors,
14
+ :director => :directors,
15
+ :fighter => :fighters,
16
+ :genre => :genres,
17
+ :keyword => :keywords,
18
+ :label => :labels,
19
+ :maker => :makers,
22
20
  }
23
- attr_reader *SINGLE_VALUE_ATTRIBUTES
24
- attr_reader *MULTIPLE_VALUES_ATTRIBUTES.keys
25
21
 
26
22
  def initialize(item_info)
27
- SINGLE_VALUE_ATTRIBUTES.inject({}) {|h,k| h.merge(k => k)}.merge({}).each do |attribute, key|
28
- value = self.class.integrate(item_info[key])
29
- value = value.first if value
30
- instance_variable_set("@#{attribute}", value)
31
- end
32
- MULTIPLE_VALUES_ATTRIBUTES.each do |attribute, key|
33
- value = self.class.integrate(item_info[key])
34
- instance_variable_set("@#{attribute}", value)
23
+ item_info.each do |key, value|
24
+ key = key.to_s
25
+ value = self.class.integrate(value)
26
+ self.class.class_eval do
27
+ unless method_defined?(key)
28
+ attr_reader key
29
+ if plural = PLURAL_MAP[key.to_sym]
30
+ alias_method plural, key.to_sym
31
+ end
32
+ end
33
+ end
34
+ instance_variable_set("@#{key}", value)
35
35
  end
36
36
  end
37
37
 
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module DMM
3
- VERSION = "0.1.2"
3
+ VERSION = "0.1.3"
4
4
  end
@@ -0,0 +1,86 @@
1
+ # vim: ts=2 sts=2 et sw=2 ft=ruby fileencoding=utf-8
2
+ require "spec_helper"
3
+
4
+ describe DMM::Response::ItemInfo do
5
+
6
+ def complex_hash
7
+ {
8
+ "actor"=> [
9
+ {"name" => "ダニエル・ラドクリフ", "id" => "60257"},
10
+ {"name" => "だにえるらどくりふ", "id" => "60257_ruby"},
11
+ {"name" => "メガネ君", "id" => "60257_classify"},
12
+ {"name" => "ルパート・グリント", "id" => "60458"},
13
+ {"name" => "るぱーとぐりんと", "id" => "60458_ruby"},
14
+ {"name" => "リチャード・ハリス", "id" => "61833"},
15
+ {"name" => "りちゃーどはりす", "id" => "61833_ruby"},
16
+ {"name" => "エマ・ワトソン", "id" => "60074"},
17
+ {"name" => "えまわとそん", "id" => "60074_ruby"},
18
+ ],
19
+ }
20
+ end
21
+
22
+ def simple_hash
23
+ {
24
+ "label" => {"name" => "ワーナー・ホーム・ビデオ", "id" => "60016"},
25
+ "genre" => {"name" => "ファンタジー", "id" => "71009"},
26
+ "series" => {"name" => "ハリー・ポッター", "id" => "60029"},
27
+ "maker" => {"name" => "ワーナー・ホーム・ビデオ", "id" => "45578"},
28
+ }
29
+ end
30
+
31
+ describe '.integrate' do
32
+ context 'simple_hash' do
33
+ subject { DMM::Response::ItemInfo.integrate(complex_hash["actor"]) }
34
+ it 'integrates name and ruby and more (classify etc...) by id' do
35
+ actor = subject.find {|actor| actor["id"] == "60257" }
36
+ actor["name"].should == "ダニエル・ラドクリフ"
37
+ actor["ruby"].should == "だにえるらどくりふ"
38
+ actor["classify"].should == "メガネ君"
39
+ end
40
+ end
41
+
42
+ context 'simple_hash' do
43
+ subject { DMM::Response::ItemInfo.integrate(simple_hash["label"]) }
44
+ it 'runs collectry' do
45
+ label = subject.first
46
+ label["id"].should == "60016"
47
+ label["name"].should == "ワーナー・ホーム・ビデオ"
48
+ end
49
+ end
50
+ end
51
+
52
+
53
+ describe 'define method for any keys' do
54
+ context 'complex_hash' do
55
+ subject { DMM::Response::ItemInfo.new(complex_hash) }
56
+ its(:actors) { should_not be_empty }
57
+ describe 'actors' do
58
+ it 'integrates name and ruby by id' do
59
+ actor = subject.actors.find {|actor| actor["id"] == "60257" }
60
+ actor["name"].should == "ダニエル・ラドクリフ"
61
+ actor["ruby"].should == "だにえるらどくりふ"
62
+ actor["classify"].should == "メガネ君"
63
+
64
+ actor = subject.actors.find {|actor| actor["id"] == "60074" }
65
+ actor["name"].should == "エマ・ワトソン"
66
+ actor["ruby"].should == "えまわとそん"
67
+ end
68
+ end
69
+ end
70
+
71
+ context 'simple_hash' do
72
+ subject do
73
+ DMM::Response::ItemInfo.new(simple_hash)
74
+ end
75
+ it { should be }
76
+
77
+ describe 'director' do
78
+ it 'respond to given keys' do
79
+ simple_hash.keys.each do |key|
80
+ subject.should respond_to(key)
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-dmm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - meganemura
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-29 00:00:00.000000000 Z
11
+ date: 2013-11-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -115,6 +115,7 @@ files:
115
115
  - spec/fixtures/r18_rental.xml
116
116
  - spec/fixtures/request_error.xml
117
117
  - spec/fixtures/zero_items.xml
118
+ - spec/response/item_info_spec.rb
118
119
  - spec/response/item_spec.rb
119
120
  - spec/response_spec.rb
120
121
  - spec/spec_helper.rb
@@ -164,6 +165,7 @@ test_files:
164
165
  - spec/fixtures/r18_rental.xml
165
166
  - spec/fixtures/request_error.xml
166
167
  - spec/fixtures/zero_items.xml
168
+ - spec/response/item_info_spec.rb
167
169
  - spec/response/item_spec.rb
168
170
  - spec/response_spec.rb
169
171
  - spec/spec_helper.rb