OSDN Git Service

t#31779:element lib
authoryasushiito <yas@pen-chan.jp>
Mon, 29 Jul 2013 23:23:05 +0000 (08:23 +0900)
committeryasushiito <yas@pen-chan.jp>
Mon, 29 Jul 2013 23:23:05 +0000 (08:23 +0900)
12 files changed:
.gitignore
app/models/ground_color.rb
app/models/ground_picture.rb
app/models/panel.rb
app/models/panel_picture.rb
app/models/speech_balloon.rb
config/application.rb
config/element.yml.org [new file with mode: 0644]
config/environment.rb
config/environments/test.rb
lib/element.rb [new file with mode: 0644]
spec/models/panel_spec.rb

index ddb3c5a..574645e 100644 (file)
@@ -11,6 +11,7 @@ config/aws.yaml
 config/picture_io.yml
 config/license.yml
 config/speech_balloon.yml
 config/picture_io.yml
 config/license.yml
 config/speech_balloon.yml
+config/element.yml
 config/profile.json
 config/test_layout
 lib/test/temp/
 config/profile.json
 config/test_layout
 lib/test/temp/
index 810ffb5..057d1d0 100644 (file)
@@ -1,4 +1,5 @@
 class GroundColor < ActiveRecord::Base
 class GroundColor < ActiveRecord::Base
+  include Element
   include ElementInspire
   belongs_to :panel
   belongs_to :color
   include ElementInspire
   belongs_to :panel
   belongs_to :color
@@ -17,6 +18,29 @@ class GroundColor < ActiveRecord::Base
     end
   end
   
     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
   def supply_default
     self.code ||= 0
     if self.panel
index e48b5e9..5042868 100644 (file)
@@ -1,4 +1,5 @@
 class GroundPicture < ActiveRecord::Base
 class GroundPicture < ActiveRecord::Base
+  include Element
   include ElementInspire
   belongs_to :panel
   belongs_to :picture
   include ElementInspire
   belongs_to :panel
   belongs_to :picture
@@ -22,6 +23,32 @@ class GroundPicture < ActiveRecord::Base
     end
   end
   
     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
   def supply_default
     self.x = 0
     self.y = 0
index dd919d0..6751428 100644 (file)
@@ -30,6 +30,27 @@ class Panel < ActiveRecord::Base
     end
   end
   
     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
   def supply_default
     self.border = 2
     self.publish = 0
@@ -138,18 +159,13 @@ class Panel < ActiveRecord::Base
   end
   
   def self.list_opt
   end
   
   def self.list_opt
-    {
-      :panel_pictures => {
-        :picture => {:artist => {}, :license => {}}
-      }, 
-      :speech_balloons => {:balloon => {}, :speech => {}},
-      :ground_pictures => {
-        :picture => {:artist => {}, :license => {}}
-      }, 
-      :ground_colors => {
-      }, 
+    r = {
       :author => {}
     }
       :author => {}
     }
+    self.each_element_classes do |e|
+      r.merge!(e.list_opt_for_panel)
+    end
+    r
   end
   
   def self.show rid, roles
   end
   
   def self.show rid, roles
@@ -169,25 +185,21 @@ class Panel < ActiveRecord::Base
   end
   
   def self.show_opt
   end
   
   def self.show_opt
-    {:include => {
-      :panel_pictures => {
-        :picture => {:artist => {}, :license => {}}
-      }, 
-      :speech_balloons => {:balloon => {}, :speech => {}},
-      :ground_pictures => {
-        :picture => {:artist => {}, :license => {}}
-      }, 
-      :ground_colors => {
-      }, 
+    r = {
       :author => {}
       :author => {}
-    }}
+    }
+    self.each_element_classes do |e|
+      r.merge!(e.show_opt_for_panel)
+    end
+    {:include => r}
   end
   
   def parts_element
   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
   end
   
   def zorderd_elements
@@ -206,26 +218,10 @@ class Panel < ActiveRecord::Base
     res
   end
   
     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
   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
   
     }
   end
   
@@ -259,7 +255,8 @@ class Panel < ActiveRecord::Base
   
   def licensed_pictures
     r = {}
   
   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
       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
   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
     ]
   end
   def validate_child
index 9d11682..64f990c 100644 (file)
@@ -1,4 +1,5 @@
 class PanelPicture < ActiveRecord::Base
 class PanelPicture < ActiveRecord::Base
+  include Element
   include ElementInspire
   belongs_to :panel
   belongs_to :picture
   include ElementInspire
   belongs_to :panel
   belongs_to :picture
@@ -22,6 +23,32 @@ class PanelPicture < ActiveRecord::Base
     end
   end
   
     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)
   def visible? roles
     if MagicNumber['run_mode'] == 0
       return false unless guest_role_check(roles)
index 08f50b8..421127b 100644 (file)
@@ -1,4 +1,5 @@
 class SpeechBalloon < ActiveRecord::Base
 class SpeechBalloon < ActiveRecord::Base
+  include Element
   include ElementInspire
   has_one :balloon, :dependent => :destroy
   has_one :speech, :dependent => :destroy
   include ElementInspire
   has_one :balloon, :dependent => :destroy
   has_one :speech, :dependent => :destroy
@@ -24,6 +25,28 @@ class SpeechBalloon < ActiveRecord::Base
     end
   end
   
     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 
   def supply_default
     if self.panel
       self.t = self.panel.new_t 
index 78e162f..22807c1 100644 (file)
@@ -63,7 +63,7 @@ config.assets.initialize_on_precompile = false
     def self.licenses
       @@licenses || {}
     end
     def self.licenses
       @@licenses || {}
     end
-     
+    
     def self.speech_balloons=(ary)
       @@speech_balloons = ary
     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
     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)
 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'
 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 (file)
index 0000000..46f9173
--- /dev/null
@@ -0,0 +1,4 @@
+  PanelPicture: panel_pictures
+  SpeechBalloon: speech_balloons
+  GroundPicture: ground_pictures
+  GroundColor: ground_colors
index 7bb8189..83559c3 100644 (file)
@@ -11,4 +11,5 @@ require 'pettan_imager'
 require 'ar_helper'
 require 'import_result'
 require 'common'
 require 'ar_helper'
 require 'import_result'
 require 'common'
+require 'element'
 
 
index 3ceac7c..84441a4 100644 (file)
@@ -1,3 +1,4 @@
+require 'element'
 Pettanr::Application.configure do
   # Settings specified here will take precedence over those in config/application.rb
 
 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 (file)
index 0000000..aef0d2b
--- /dev/null
@@ -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
+
index ec82c05..2c7ba5b 100644 (file)
@@ -784,18 +784,6 @@ describe Panel do
       r.include?(@gp).should be_true\r
     end\r
   end\r
       r.include?(@gp).should be_true\r
     end\r
   end\r
-  describe 'コマ部品に於いて' do\r
-    before do\r
-    end\r
-    context 'つつがなく終わるとき' do\r
-      it 'コマ部品集合を利用している' do\r
-        r = @panel.parts\r
-      end\r
-    end\r
-    it 'リストを返している' do\r
-      r = @panel.parts\r
-    end\r
-  end\r
   \r
   describe 'z順コマ部品に於いて' do\r
     before do\r
   \r
   describe 'z順コマ部品に於いて' do\r
     before do\r
@@ -867,8 +855,8 @@ describe Panel do
     end\r
     context 'つつがなく終わるとき' do\r
       it 'コマ部品を利用している' do\r
     end\r
     context 'つつがなく終わるとき' do\r
       it 'コマ部品を利用している' do\r
-        Panel.any_instance.stub(:parts).with(any_args).and_return([])\r
-        Panel.any_instance.should_receive(:parts).with(any_args).exactly(1)\r
+        Panel.any_instance.stub(:parts_element).with(any_args).and_return([])\r
+        Panel.any_instance.should_receive(:parts_element).with(any_args).exactly(1)\r
         r = @panel.panel_elements\r
       end\r
     end\r
         r = @panel.panel_elements\r
       end\r
     end\r
@@ -918,25 +906,25 @@ describe Panel do
     end\r
     context 'つつがなく終わるとき' do\r
       before do\r
     end\r
     context 'つつがなく終わるとき' do\r
       before do\r
-        Panel.stub(:elm_json_opt).with(@pp).and_return({})\r
-        Panel.stub(:elm_json_opt).with(@sb).and_return({})\r
-        Panel.stub(:elm_json_opt).with(@gp).and_return({})\r
-        Panel.stub(:elm_json_opt).with(@gc).and_return({})\r
+        PanelPicture.stub(:json_opt_for_panel).and_return({})\r
+        SpeechBalloon.stub(:json_opt_for_panel).and_return({})\r
+        GroundPicture.stub(:json_opt_for_panel).and_return({})\r
+        GroundColor.stub(:json_opt_for_panel).and_return({})\r
       end\r
       end\r
-      it 'コマ要素のjson出力オプションにコマ絵json出力オプションを依頼している' do\r
-        Panel.should_receive(:elm_json_opt).with(@pp).exactly(1)\r
+      it 'コマ絵にjson出力オプションを依頼している' do\r
+        PanelPicture.should_receive(:json_opt_for_panel).exactly(1)\r
         r = @panel.elements\r
       end\r
         r = @panel.elements\r
       end\r
-      it 'ã\82³ã\83\9eè¦\81ç´ ã\81®jsonå\87ºå\8a\9bã\82ªã\83\97ã\82·ã\83§ã\83³ã\81«ã\83\95ã\82­ã\83\80ã\82·json出力オプションを依頼している' do\r
-        Panel.should_receive(:elm_json_opt).with(@sb).exactly(1)\r
+      it 'ã\83\95ã\82­ã\83\80ã\82·ã\81«json出力オプションを依頼している' do\r
+        SpeechBalloon.should_receive(:json_opt_for_panel).exactly(1)\r
         r = @panel.elements\r
       end\r
         r = @panel.elements\r
       end\r
-      it 'コマ要素のjson出力オプションに絵地json出力オプションを依頼している' do\r
-        Panel.should_receive(:elm_json_opt).with(@gp).exactly(1)\r
+      it '絵地にjson出力オプションを依頼している' do\r
+        GroundPicture.should_receive(:json_opt_for_panel).exactly(1)\r
         r = @panel.elements\r
       end\r
         r = @panel.elements\r
       end\r
-      it 'コマ要素のjson出力オプションに色地json出力オプションを依頼している' do\r
-        Panel.should_receive(:elm_json_opt).with(@gc).exactly(1)\r
+      it '色地にjson出力オプションを依頼している' do\r
+        GroundColor.should_receive(:json_opt_for_panel).exactly(1)\r
         r = @panel.elements\r
       end\r
     end\r
         r = @panel.elements\r
       end\r
     end\r