From: yasushiito Date: Mon, 29 Jul 2013 23:23:05 +0000 (+0900) Subject: t#31779:element lib X-Git-Url: http://git.osdn.net/view?p=pettanr%2Fpettanr.git;a=commitdiff_plain;h=8ef0d6b0e6908ea6ffd2169556fa52b34a02b214 t#31779:element lib --- diff --git a/.gitignore b/.gitignore index ddb3c5a5..574645ec 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,7 @@ config/aws.yaml config/picture_io.yml config/license.yml config/speech_balloon.yml +config/element.yml config/profile.json config/test_layout lib/test/temp/ diff --git a/app/models/ground_color.rb b/app/models/ground_color.rb index 810ffb55..057d1d04 100644 --- a/app/models/ground_color.rb +++ b/app/models/ground_color.rb @@ -1,4 +1,5 @@ class GroundColor < ActiveRecord::Base + include Element include ElementInspire belongs_to :panel belongs_to :color @@ -17,6 +18,29 @@ class GroundColor < ActiveRecord::Base end end + def self.list_opt_for_panel + { + :ground_colors => { + } + } + end + + def self.show_opt_for_panel + { + :ground_colors => { + } + } + end + + def self.json_opt_for_panel + { + } + end + + def self.has_picture? + false + end + def supply_default self.code ||= 0 if self.panel diff --git a/app/models/ground_picture.rb b/app/models/ground_picture.rb index e48b5e90..50428685 100644 --- a/app/models/ground_picture.rb +++ b/app/models/ground_picture.rb @@ -1,4 +1,5 @@ class GroundPicture < ActiveRecord::Base + include Element include ElementInspire belongs_to :panel belongs_to :picture @@ -22,6 +23,32 @@ class GroundPicture < ActiveRecord::Base end end + def self.list_opt_for_panel + { + :ground_pictures => { + :picture => {:artist => {}, :license => {}} + } + } + end + + def self.show_opt_for_panel + { + :ground_pictures => { + :picture => {:artist => {}, :license => {}} + } + } + end + + def self.json_opt_for_panel + { + :picture => {:artist => {}, :license => {}} + } + end + + def self.has_picture? + true + end + def supply_default self.x = 0 self.y = 0 diff --git a/app/models/panel.rb b/app/models/panel.rb index dd919d04..67514286 100644 --- a/app/models/panel.rb +++ b/app/models/panel.rb @@ -30,6 +30,27 @@ class Panel < ActiveRecord::Base end end + def self.each_element_class_names + Pettanr::Application.elements.each do |k, n| + yield k + end + end + + def self.class_name_to_class k + Object.const_get k + end + + def self.each_element_classes + self.each_element_class_names do |k| + e = self.class_name_to_class k + yield e + end + end + + def elements_by_class_name class_name + self.__send__ class_name.tableize + end + def supply_default self.border = 2 self.publish = 0 @@ -138,18 +159,13 @@ class Panel < ActiveRecord::Base end def self.list_opt - { - :panel_pictures => { - :picture => {:artist => {}, :license => {}} - }, - :speech_balloons => {:balloon => {}, :speech => {}}, - :ground_pictures => { - :picture => {:artist => {}, :license => {}} - }, - :ground_colors => { - }, + r = { :author => {} } + self.each_element_classes do |e| + r.merge!(e.list_opt_for_panel) + end + r end def self.show rid, roles @@ -169,25 +185,21 @@ class Panel < ActiveRecord::Base end def self.show_opt - {:include => { - :panel_pictures => { - :picture => {:artist => {}, :license => {}} - }, - :speech_balloons => {:balloon => {}, :speech => {}}, - :ground_pictures => { - :picture => {:artist => {}, :license => {}} - }, - :ground_colors => { - }, + r = { :author => {} - }} + } + self.each_element_classes do |e| + r.merge!(e.show_opt_for_panel) + end + {:include => r} end def parts_element - ((self.panel_pictures || []) + (self.speech_balloons || []) + (self.ground_colors || []) + (self.ground_pictures || [])).compact - end - - def parts + r = [] + self.class.each_element_class_names do |k| + r += (self.elements_by_class_name(k) || []) + end + r end def zorderd_elements @@ -206,26 +218,10 @@ class Panel < ActiveRecord::Base res end - @@elm_json_opt = { - 'PanelPicture' => { - :picture => {:artist => {}, :license => {}} - }, - 'SpeechBalloon' => {:balloon => {}, :speech => {}}, - 'GroundPicture' => { - :picture => {:artist => {}, :license => {}} - }, - 'GroundColor' => { - }, - } - - def self.elm_json_opt e - @@elm_json_opt[e.class.to_s] - end - def elements self.panel_elements.map {|e| #(-_-;)<... kore wa hidoi - JSON.parse e.to_json({:include => Panel.elm_json_opt(e)}) + JSON.parse e.to_json({:include => e.class.json_opt_for_panel}) } end @@ -259,7 +255,8 @@ class Panel < ActiveRecord::Base def licensed_pictures r = {} - ((self.panel_pictures || []) + (self.ground_pictures || [])).compact.each do |elm| + self.panel_elements.each do |elm| + next unless elm.class.has_picture? r[elm.picture_id] = elm.picture unless r[elm.picture_id] end r @@ -301,9 +298,13 @@ class Panel < ActiveRecord::Base end def validate_serial_list + l = [] + self.class.each_element_class_names do |k| + l << self.elements_by_class_name(k) + end [ - {:elements => [self.panel_pictures, self.speech_balloons, self.ground_colors, self.ground_pictures], :name => :t, :offset => 0}, - {:elements => [self.panel_pictures, self.speech_balloons, self.ground_colors, self.ground_pictures], :name => :z, :offset => 1} + {:elements => l, :name => :t, :offset => 0}, + {:elements => l, :name => :z, :offset => 1} ] end def validate_child diff --git a/app/models/panel_picture.rb b/app/models/panel_picture.rb index 9d116829..64f990ca 100644 --- a/app/models/panel_picture.rb +++ b/app/models/panel_picture.rb @@ -1,4 +1,5 @@ class PanelPicture < ActiveRecord::Base + include Element include ElementInspire belongs_to :panel belongs_to :picture @@ -22,6 +23,32 @@ class PanelPicture < ActiveRecord::Base end end + def self.list_opt_for_panel + { + :panel_pictures => { + :picture => {:artist => {}, :license => {}} + } + } + end + + def self.show_opt_for_panel + { + :panel_pictures => { + :picture => {:artist => {}, :license => {}} + } + } + end + + def self.json_opt_for_panel + { + :picture => {:artist => {}, :license => {}} + } + end + + def self.has_picture? + true + end + def visible? roles if MagicNumber['run_mode'] == 0 return false unless guest_role_check(roles) diff --git a/app/models/speech_balloon.rb b/app/models/speech_balloon.rb index 08f50b86..421127b3 100644 --- a/app/models/speech_balloon.rb +++ b/app/models/speech_balloon.rb @@ -1,4 +1,5 @@ class SpeechBalloon < ActiveRecord::Base + include Element include ElementInspire has_one :balloon, :dependent => :destroy has_one :speech, :dependent => :destroy @@ -24,6 +25,28 @@ class SpeechBalloon < ActiveRecord::Base end end + def self.list_opt_for_panel + { + :speech_balloons => {:balloon => {}, :speech => {}} + } + end + + def self.show_opt_for_panel + { + :speech_balloons => {:balloon => {}, :speech => {}} + } + end + + def self.json_opt_for_panel + { + :balloon => {}, :speech => {} + } + end + + def self.has_picture? + false + end + def supply_default if self.panel self.t = self.panel.new_t diff --git a/config/application.rb b/config/application.rb index 78e162fb..22807c11 100644 --- a/config/application.rb +++ b/config/application.rb @@ -63,7 +63,7 @@ config.assets.initialize_on_precompile = false def self.licenses @@licenses || {} end - + def self.speech_balloons=(ary) @@speech_balloons = ary end @@ -71,11 +71,20 @@ config.assets.initialize_on_precompile = false def self.speech_balloons @@speech_balloons || {} end - end + + def self.elements=(ary) + @@elements = ary + end + + def self.elements + @@elements || {} + end + end end Pettanr::Application.licenses = YAML.load(open(Rails.root + 'config/license.yml').read) Pettanr::Application.speech_balloons = YAML.load(open(Rails.root + 'config/speech_balloon.yml').read) +Pettanr::Application.elements = YAML.load(open(Rails.root + 'config/element.yml').read) MagicNumber = YAML.load(open(Rails.root + 'config/magic_number.yml').read) MagicNumber['test_layout'] = if File.exist? Rails.root + 'config/test_layout' 'test' diff --git a/config/element.yml.org b/config/element.yml.org new file mode 100644 index 00000000..46f91739 --- /dev/null +++ b/config/element.yml.org @@ -0,0 +1,4 @@ + PanelPicture: panel_pictures + SpeechBalloon: speech_balloons + GroundPicture: ground_pictures + GroundColor: ground_colors diff --git a/config/environment.rb b/config/environment.rb index 7bb8189d..83559c3d 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -11,4 +11,5 @@ require 'pettan_imager' require 'ar_helper' require 'import_result' require 'common' +require 'element' diff --git a/config/environments/test.rb b/config/environments/test.rb index 3ceac7c9..84441a45 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -1,3 +1,4 @@ +require 'element' Pettanr::Application.configure do # Settings specified here will take precedence over those in config/application.rb diff --git a/lib/element.rb b/lib/element.rb new file mode 100644 index 00000000..aef0d2b2 --- /dev/null +++ b/lib/element.rb @@ -0,0 +1,32 @@ +module Element + def self.included(base) + base.extend(ClassMethods) + base.__send__ :include, InstanceMethods + end + + module ClassMethods + def list_opt_for_panel + {} + end + + def show_opt_for_panel + {} + end + + def json_opt_for_panel + {} + end + end + + module InstanceMethods + private + + public + + def has_picture? + false + end + + end +end + diff --git a/spec/models/panel_spec.rb b/spec/models/panel_spec.rb index ec82c05b..2c7ba5bd 100644 --- a/spec/models/panel_spec.rb +++ b/spec/models/panel_spec.rb @@ -784,18 +784,6 @@ describe Panel do r.include?(@gp).should be_true end end - describe 'コマ部品に於いて' do - before do - end - context 'つつがなく終わるとき' do - it 'コマ部品集合を利用している' do - r = @panel.parts - end - end - it 'リストを返している' do - r = @panel.parts - end - end describe 'z順コマ部品に於いて' do before do @@ -867,8 +855,8 @@ describe Panel do end context 'つつがなく終わるとき' do it 'コマ部品を利用している' do - Panel.any_instance.stub(:parts).with(any_args).and_return([]) - Panel.any_instance.should_receive(:parts).with(any_args).exactly(1) + Panel.any_instance.stub(:parts_element).with(any_args).and_return([]) + Panel.any_instance.should_receive(:parts_element).with(any_args).exactly(1) r = @panel.panel_elements end end @@ -918,25 +906,25 @@ describe Panel do end context 'つつがなく終わるとき' do before do - Panel.stub(:elm_json_opt).with(@pp).and_return({}) - Panel.stub(:elm_json_opt).with(@sb).and_return({}) - Panel.stub(:elm_json_opt).with(@gp).and_return({}) - Panel.stub(:elm_json_opt).with(@gc).and_return({}) + PanelPicture.stub(:json_opt_for_panel).and_return({}) + SpeechBalloon.stub(:json_opt_for_panel).and_return({}) + GroundPicture.stub(:json_opt_for_panel).and_return({}) + GroundColor.stub(:json_opt_for_panel).and_return({}) end - it 'コマ要素のjson出力オプションにコマ絵json出力オプションを依頼している' do - Panel.should_receive(:elm_json_opt).with(@pp).exactly(1) + it 'コマ絵にjson出力オプションを依頼している' do + PanelPicture.should_receive(:json_opt_for_panel).exactly(1) r = @panel.elements end - it 'コマ要素のjson出力オプションにフキダシjson出力オプションを依頼している' do - Panel.should_receive(:elm_json_opt).with(@sb).exactly(1) + it 'フキダシにjson出力オプションを依頼している' do + SpeechBalloon.should_receive(:json_opt_for_panel).exactly(1) r = @panel.elements end - it 'コマ要素のjson出力オプションに絵地json出力オプションを依頼している' do - Panel.should_receive(:elm_json_opt).with(@gp).exactly(1) + it '絵地にjson出力オプションを依頼している' do + GroundPicture.should_receive(:json_opt_for_panel).exactly(1) r = @panel.elements end - it 'コマ要素のjson出力オプションに色地json出力オプションを依頼している' do - Panel.should_receive(:elm_json_opt).with(@gc).exactly(1) + it '色地にjson出力オプションを依頼している' do + GroundColor.should_receive(:json_opt_for_panel).exactly(1) r = @panel.elements end end