Skip to content

Commit

Permalink
Define alias methods
Browse files Browse the repository at this point in the history
 * Add new alias for iteminfo
  • Loading branch information
meganemura committed Nov 29, 2013
1 parent 045a09c commit b68d9c4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 34 deletions.
41 changes: 14 additions & 27 deletions lib/ruby-dmm/response/item.rb
Expand Up @@ -4,41 +4,25 @@
module DMM
class Response
class Item
KEYS = [
:affiliate_url,
:affiliate_url_sp,
:bandaiinfo,
:category_name,
:cdinfo,
:content_id,
:floor_name,
:image_url,
:isbn,
:jancode,
:maker_product,
:product_id,
:service_name,
:stock,
:title,
:url,
:url_sp,
]
attr_reader *KEYS
alias :bandai_info :bandaiinfo
alias :cd_info :cdinfo
alias :images :image_url

attr_reader *[
PREDEFINED_KEYS = [
:date,
:iteminfo,
:large_images,
:list_price,
:price,
:price_all,
:prices,
:small_images,
]
alias :item_info :iteminfo
attr_reader *PREDEFINED_KEYS
alias_method :item_info, :iteminfo
alias_method :info, :iteminfo

ALIAS_METHOD_MAP = {
:bandaiinfo => :bandai_info,
:cdinfo => :cd_info,
:image_url => :images,
}

def initialize(item)
item.each do |key, value|
Expand All @@ -53,14 +37,17 @@ def initialize(item)
when :prices
setup_prices(value) if value
else
instance_variable_set("@#{key}", item[key])
self.class.class_eval do
unless method_defined?(key)
define_method "#{key}" do
instance_variable_get("@#{key}")
end
if name = ALIAS_METHOD_MAP[key.to_sym]
alias_method name, key.to_sym
end
end
end
instance_variable_set("@#{key}", item[key])
end
end
end
Expand Down
17 changes: 10 additions & 7 deletions spec/response/item_spec.rb
Expand Up @@ -7,13 +7,6 @@
@item = DMM::new.item_list.result.items.first
end

describe 'methods which returns integer' do
subject { @item }
DMM::Response::Item::KEYS.each do |key|
it { should respond_to(key) }
end
end

describe '#images' do
subject { @item.images }
it { should be }
Expand All @@ -22,6 +15,16 @@
its([:large]) { should == 'http://pics.dmm.com/mono/movie/n_616dlr22659/n_616dlr22659pl.jpg' }
end

describe 'define alias methods' do
subject do
item = DMM::Response::Item::ALIAS_METHOD_MAP.keys.inject({}) {|h, key| h.merge(key => 1) }
DMM::Response::Item.new(item)
end
DMM::Response::Item::ALIAS_METHOD_MAP.values.each do |name|
it { should respond_to(name) }
end
end

describe '#large_images' do
subject do
item = {
Expand Down

0 comments on commit b68d9c4

Please sign in to comment.