moved gateway code here
This commit is contained in:
50
app/models/merged/image.rb
Normal file
50
app/models/merged/image.rb
Normal file
@ -0,0 +1,50 @@
|
||||
module Merged
|
||||
class Image
|
||||
include ActiveModel::API
|
||||
include ActiveModel::Conversion
|
||||
extend ActiveModel::Naming
|
||||
|
||||
@@images = {}
|
||||
|
||||
attr_reader :name , :type , :size , :created_at , :updated_at
|
||||
|
||||
def initialize(filename)
|
||||
puts "New Image #{filename}"
|
||||
@name , @type = filename.split(".")
|
||||
file = File.new(Rails.root.join(Image.root,filename))
|
||||
@created_at = file.birthtime
|
||||
@updated_at = file.ctime
|
||||
@size = (file.size/1024).to_i
|
||||
end
|
||||
|
||||
def self.all
|
||||
return @@images unless @@images.empty?
|
||||
Dir[Rails.root.join Image.root + "*.*"].each do |f|
|
||||
file = f.split("/").last
|
||||
self.add( file )
|
||||
end
|
||||
@@images
|
||||
end
|
||||
|
||||
#save an io with given name (without ending, that is taken from io)
|
||||
def self.create_new(filename , io)
|
||||
original , ending = io.original_filename.split("/").last.split(".")
|
||||
filename = original if( filename.blank? )
|
||||
full_filename = filename + "." + ending
|
||||
File.open(Rails.root.join(Image.root, full_filename), "wb") do |f|
|
||||
f.write( io.read )
|
||||
end
|
||||
self.add( full_filename )
|
||||
end
|
||||
|
||||
def self.root
|
||||
"app/assets/images/merge/"
|
||||
end
|
||||
|
||||
private
|
||||
def self.add(filename)
|
||||
key = filename.split(".").first
|
||||
@@images[key] = Image.new(filename)
|
||||
end
|
||||
end
|
||||
end
|
63
app/models/merged/page.rb
Normal file
63
app/models/merged/page.rb
Normal file
@ -0,0 +1,63 @@
|
||||
module Merged
|
||||
class Page
|
||||
include ActiveModel::API
|
||||
include ActiveModel::Conversion
|
||||
extend ActiveModel::Naming
|
||||
|
||||
@@files = Set.new Dir.new(Rails.root.join("merge")).children
|
||||
|
||||
attr_reader :name , :content
|
||||
|
||||
alias :id :name
|
||||
|
||||
def persisted?
|
||||
false
|
||||
end
|
||||
|
||||
def initialize file_name
|
||||
@name = file_name.split(".").first
|
||||
@content = YAML.load_file(Rails.root.join("merge" , file_name))
|
||||
end
|
||||
|
||||
def sections
|
||||
sections = []
|
||||
@content.each_with_index do |section_data, index|
|
||||
sections << Section.new(self , index, section_data)
|
||||
end
|
||||
sections
|
||||
end
|
||||
|
||||
def find_section(section_id)
|
||||
@content.each_with_index do |section , index|
|
||||
next unless section["id"] == section_id
|
||||
return Section.new(self , index , section)
|
||||
end
|
||||
raise "Page #{name} as no section #{section_id}"
|
||||
end
|
||||
|
||||
def first_template
|
||||
@content[0]["template"]
|
||||
end
|
||||
|
||||
def new_section
|
||||
section = Hash.new
|
||||
section['id'] = SecureRandom.hex(10)
|
||||
@content << section
|
||||
Section.new(self , 0 , section)
|
||||
end
|
||||
|
||||
def save
|
||||
file_name = Rails.root.join("merge" , name + ".yaml")
|
||||
File.write( file_name , @content.to_yaml)
|
||||
end
|
||||
|
||||
def self.all
|
||||
@@files.collect{ |file| Page.new(file) }
|
||||
end
|
||||
|
||||
def self.find(name)
|
||||
Page.new(name + ".yaml")
|
||||
end
|
||||
|
||||
end
|
||||
end
|
53
app/models/merged/section.rb
Normal file
53
app/models/merged/section.rb
Normal file
@ -0,0 +1,53 @@
|
||||
module Merged
|
||||
class Section
|
||||
include ActiveModel::API
|
||||
include ActiveModel::Conversion
|
||||
extend ActiveModel::Naming
|
||||
|
||||
attr_reader :name , :content , :page , :index
|
||||
|
||||
def persisted?
|
||||
false
|
||||
end
|
||||
|
||||
def initialize(page , index , section_data)
|
||||
@page = page
|
||||
raise "No number #{index}" unless index.is_a?(Integer)
|
||||
raise "No has #{section_data}" unless section_data.is_a?(Hash)
|
||||
@index = index
|
||||
@content = section_data
|
||||
end
|
||||
|
||||
def update(key , value)
|
||||
return if key == "id" #not updating that
|
||||
if(! @content[key].nil? )
|
||||
if( @content[key].class != value.class )
|
||||
raise "Type mismatch #{key} #{key.class}!=#{value.class}"
|
||||
end
|
||||
end
|
||||
@content[key] = value
|
||||
end
|
||||
|
||||
def template
|
||||
@content["template"]
|
||||
end
|
||||
|
||||
def id
|
||||
@content["id"]
|
||||
end
|
||||
|
||||
def save
|
||||
raise "Called"
|
||||
end
|
||||
|
||||
def self.all
|
||||
@page.sections
|
||||
end
|
||||
|
||||
def self.find(page_name , section_id)
|
||||
raise "buggy"
|
||||
Page.new(name + ".yaml")
|
||||
end
|
||||
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user