ezframe 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -12,22 +12,29 @@ module Ezframe
12
12
 
13
13
  tag = ht_h[:tag]
14
14
  case tag
15
- when "input"
16
- input(ht_h)
15
+ when "textarea"
16
+ textarea(ht_h)
17
17
  when "select"
18
18
  return select(ht_h) if ht_h[:item]
19
19
  when "icon"
20
20
  tag = "i"
21
21
  end
22
22
  tag = ht_h[:tag]
23
- opt_s, child_s = join_attributes(ht_h)
24
- if !child_s.strip.empty? || %w[div span table tr td th textarea].include?(tag)
25
- return "<#{ht_h[:tag]} #{opt_s}>\n#{child_s}\n</#{ht_h[:tag]}>\n"
23
+ error_box = ""
24
+ if %w[input select textarea].include?(tag)
25
+ error_box = "<div id=\"error-box-#{ht_h[:name]}\" class=\"error-box hide\"></div>"
26
26
  end
27
- "<#{ht_h[:tag]} #{opt_s} />"
27
+ opt_s, child_s = join_attribute(ht_h)
28
+ if !child_s.strip.empty? || !%w[img input hr br meta].include?(tag)
29
+ start_tag = [ht_h[:tag], opt_s].compact.join(" ").strip
30
+ return "<#{start_tag}>#{child_s}</#{ht_h[:tag]}>"+error_box
31
+ end
32
+ tag_content = [ ht_h[:tag], opt_s ].compact.join(" ")
33
+ return "<#{tag_content}/>"+error_box
28
34
  end
29
35
 
30
- def join_attributes(attrs)
36
+ # attributeの連結文字列化
37
+ def join_attribute(attrs)
31
38
  child_s = ""
32
39
  opt_a = attrs.map do |k, v|
33
40
  case k
@@ -52,16 +59,10 @@ module Ezframe
52
59
  [opt_a.compact.join(" "), child_s]
53
60
  end
54
61
 
55
- def input(ht_h)
56
- size = ht_h[:size]
57
- # puts "input: size=#{size.inspect}"
58
- if size && (size.index("x") || size.index("*"))
59
- if /(\d+)\s*[x\*]\s*(\d+)/ =~ size
60
- ht_h[:cols], ht_h[:rows] = $1, $2
61
- ht_h.delete(:size)
62
- end
63
- ht_h[:tag] = "textarea"
64
- ht_h[:child] = ht_h[:value]
62
+ def textarea(ht_h)
63
+ value = ht_h[:value]
64
+ if value
65
+ ht_h[:child] = value
65
66
  ht_h.delete(:value)
66
67
  end
67
68
  end
@@ -78,7 +79,7 @@ module Ezframe
78
79
  h[:selected] = "selected" if selected
79
80
  end
80
81
  h[:child] = v
81
- # mylog "select: hash: k=#{k}, v=#{v}, value=#{ht_h[:value]}"
82
+ # Logger.info "select: hash: k=#{k}, v=#{v}, value=#{ht_h[:value]}"
82
83
  if ht_h[:value] && ht_h[:value].to_s == k.to_s
83
84
  h[:selected] = "selected"
84
85
  end
@@ -90,7 +91,7 @@ module Ezframe
90
91
  if %w[selected default].include?(v[2])
91
92
  h[:selected] = "selected"
92
93
  end
93
- # mylog "select: array: v=#{v}, value=#{ht_h[:value]}"
94
+ # Logger.info "select: array: v=#{v}, value=#{ht_h[:value]}"
94
95
  if ht_h[:value] && ht_h[:value].to_s == v[0].to_s
95
96
  h[:selected] = "selected"
96
97
  end
@@ -41,5 +41,15 @@ class Japanese
41
41
  return nil unless wday
42
42
  return %w(日 月 火 水 木 金 土)[wday]
43
43
  end
44
+
45
+ def to_datetime_str(tm)
46
+ return nil unless tm
47
+ return "%d年%2d月%2d日 %2d:%02d:%02d" %[ tm.year, tm.mon, tm.mday, tm.hour, tm.min, tm.sec ]
48
+ end
49
+
50
+ def to_date_str(tm)
51
+ return nil unless tm
52
+ return "%d年%2d月%2d日" %[ tm.year, tm.mon, tm.mday]
53
+ end
44
54
  end
45
55
  end
@@ -0,0 +1,29 @@
1
+ module Ezframe
2
+ class Jquery
3
+ class << self
4
+ def into_html_header
5
+ css_a = Config[:extra_css_list].map {|file| "<link href=\"#{file}\" rel=\"stylesheet\">\n" }
6
+ js_a = Config[:extra_js_list].map {|file| "<script src=\"#{file}\"></script>\n" }
7
+
8
+ css_files = Dir["./asset/css/*.css"]||[]
9
+ css_a += css_files.map do |file|
10
+ file.gsub!("./asset", "")
11
+ "<link href=\"#{file}\" rel=\"stylesheet\">\n"
12
+ end
13
+ js_files = Dir["./asset/js/*.js"]||[]
14
+ js_a += js_files.map do |file|
15
+ file.gsub!("./asset", "")
16
+ "<script src=\"#{file}\"></script>\n"
17
+ end
18
+ (css_a+js_a).join
19
+ end
20
+
21
+ def into_bottom_of_body
22
+ ""
23
+ end
24
+
25
+ def convert(ht_h)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -1,11 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- if File.exist?("pages/common.rb")
4
- require "#{Dir.pwd}/pages/common.rb"
3
+ if File.exist?("page/common.rb")
4
+ require "#{Dir.pwd}/page/common.rb"
5
5
  end
6
- Dir["models/*.rb"].sort.each do |file|
6
+ Dir["model/*.rb"].sort.each do |file|
7
7
  require "#{Dir.pwd}/#{file}"
8
8
  end
9
- Dir["pages/*.rb"].sort.each do |file|
9
+ Dir["page/*.rb"].sort.each do |file|
10
10
  require "#{Dir.pwd}/#{file}"
11
11
  end
@@ -0,0 +1,39 @@
1
+ module Ezframe
2
+ class Logger
3
+ class << self
4
+ @instance = nil
5
+
6
+ def writer(level="", msg)
7
+ unless @instance
8
+ @instance = File.open("log/#{ENV['RACK_ENV']||'development'}.log", "a+")
9
+ @instance.sync = true
10
+ end
11
+ @instance.puts "#{Time.now.to_s}:#{level.upcase}:#{msg}"
12
+ end
13
+
14
+ def level=(lv)
15
+ @level = lv
16
+ end
17
+
18
+ def info(msg)
19
+ writer("info", msg)
20
+ end
21
+
22
+ def debug(msg)
23
+ writer("debug", msg)
24
+ end
25
+
26
+ def warn(msg)
27
+ writer("warn", msg)
28
+ end
29
+
30
+ def error(msg)
31
+ writer("debug", msg)
32
+ end
33
+
34
+ def <<(msg)
35
+ writer("", msg)
36
+ end
37
+ end
38
+ end
39
+ end
@@ -8,12 +8,12 @@ module Ezframe
8
8
  js_a = Config[:extra_js_list].map {|file| "<script src=\"#{file}\"></script>\n" }
9
9
 
10
10
  css_files = Dir["./asset/css/*.css"]||[]
11
- css_a += css_files.map do |file|
11
+ css_a += css_files.sort.map do |file|
12
12
  file.gsub!("./asset", "")
13
13
  "<link href=\"#{file}\" rel=\"stylesheet\">\n"
14
14
  end
15
15
  js_files = Dir["./asset/js/*.js"]||[]
16
- js_a += js_files.map do |file|
16
+ js_a += js_files.sort.map do |file|
17
17
  file.gsub!("./asset", "")
18
18
  "<script src=\"#{file}\"></script>\n"
19
19
  end
@@ -32,13 +32,10 @@ module Ezframe
32
32
  new_h = ht_h.map { |v| convert(v) }
33
33
  elsif ht_h.is_a?(Hash)
34
34
  unless ht_h[:tag]
35
- mylog("convert: no tag: #{ht_h.inspect}")
35
+ Logger.info("convert: no tag: #{ht_h.inspect}")
36
36
  return nil
37
37
  end
38
38
  case ht_h[:tag].to_sym
39
- # when :input, :select
40
- # new_h = input(ht_h) if "hidden" != ht_h[:type]
41
- # return new_h
42
39
  when :checkbox
43
40
  return checkbox(ht_h)
44
41
  when :radio
@@ -57,8 +54,8 @@ module Ezframe
57
54
 
58
55
  def icon(ht_h)
59
56
  new_h = ht_h.clone
60
- mylog "[warn] no name attribute for icon ht_h: #{ht_h.inspect}" unless new_h[:name]
61
- new_h.add_class("material-icons")
57
+ Logger.info "[warn] no name attribute for icon ht_h: #{ht_h.inspect}" unless new_h[:name]
58
+ new_h.add_class(%w[material-icons align-icon])
62
59
  new_h.update({ tag: "i", child: ht_h[:name] })
63
60
  new_h.delete(:name)
64
61
  return new_h
@@ -18,7 +18,7 @@ module Ezframe
18
18
  begin
19
19
  yaml = YAML.load_file(file)
20
20
  rescue
21
- mylog("YAML load error: #{file}")
21
+ Logger.info("YAML load error: #{file}")
22
22
  return
23
23
  end
24
24
  if /([a-z]{2})\.yml$/ =~ file
@@ -6,7 +6,11 @@ require_relative "util"
6
6
 
7
7
  module Ezframe
8
8
  class PageBase
9
- attr_accessor :auth, :request
9
+ # class << self
10
+ # attr_accessor :auth
11
+ # end
12
+
13
+ attr_accessor :request
10
14
 
11
15
  def initialize(request = nil)
12
16
  @class_snake = class_to_snake(self.class)
@@ -21,20 +25,16 @@ module Ezframe
21
25
  # Rackのrequestを代入し、関連するインスタンス変数を定義
22
26
  def set_request(request)
23
27
  @request = request
24
- @model = request.env["model"]
25
- mylog "[WARN] model is not defined" unless @model
26
- @column_set = @model.column_sets[@class_snake.to_sym]
27
- # mylog "[WARN] column_set is not defined: #{@class_snake}" unless @column_set
28
- if @column_set
29
- @dataset = @column_set.dataset
30
- end
28
+ @column_set = ColumnSets.get(@class_snake)
29
+ @dataset = DB.dataset(@class_snake)
30
+ Logger.debug "column_set is not defined: #{@class_snake}" unless @column_set
31
31
  @params = parse_query_string(request.env["QUERY_STRING"])
32
32
  @params.update(request.params)
33
- mylog "params=#{@params.inspect}" if @params.length > 0
33
+ # Logger.info "set_request: params=#{@params.inspect}" if @params.length > 0
34
34
  # @id, @key = @params[:id], @params[:key]
35
35
  @env = @request.env
36
36
  @session = @env["rack.session"]
37
- # mylog "session = #{@session.inspect}"
37
+
38
38
  if %w[POST PUT].include?(request.request_method)
39
39
  body = @request.body.read
40
40
  if request.content_type.index("json")
@@ -42,25 +42,30 @@ module Ezframe
42
42
  else
43
43
  @parsed_body = parse_query_string(body)
44
44
  end
45
- # mylog "parsed_body=#{@parsed_body.inspect}"
45
+ # Logger.info "parsed_body=#{@parsed_body.inspect}"
46
46
  @event = @parsed_body[:event] || {}
47
+ @form = @event[:form]
48
+
49
+ # Logger.info "event=#{@event}"
47
50
  end
48
51
  end
49
52
 
50
53
  # routeから基本URLを生成
51
54
  def make_base_url(id = nil)
52
55
  path = Route::get_path(@class_snake)
53
- params = @request.env["url_params"]
56
+ params = @request.env["url_params"] || {}
57
+ # Logger.info "make_base_url: params=#{params}"
54
58
  # params[@class_snake.to_sym] = id
55
59
  path_s = path.map do |pa|
56
60
  if pa == @class_snake.to_sym && id
57
61
  "#{pa}/#{id}"
58
- elsif params[p]
59
- "#{pa}/#{params[p]}"
62
+ elsif params[pa.to_sym]
63
+ "#{pa}/#{params[pa.to_sym]}"
60
64
  else
61
65
  pa
62
66
  end
63
67
  end.join("/")
68
+ # Logger.info "path_s=#{path_s}"
64
69
  return "/#{path_s}"
65
70
  end
66
71
 
@@ -71,7 +76,7 @@ module Ezframe
71
76
  into_html_header: Materialize.into_html_header,
72
77
  into_bottom_of_body: Materialize.into_bottom_of_body,
73
78
  }
74
- Template.fill("template/base.html", args)
79
+ Template.fill_from_file("template/base.html", args)
75
80
  end
76
81
 
77
82
  def parse_json_body(body)
@@ -79,15 +84,19 @@ module Ezframe
79
84
  begin
80
85
  json = JSON.parse(body)
81
86
  rescue => e
82
- mylog "ERROR: #{e.class}:#{e.message}\n#{e.backtrace}"
87
+ Logger.info "ERROR: #{e.class}:#{e.message}\n#{e.backtrace}"
83
88
  return nil
84
89
  end
85
90
  json = json.recursively_symbolize_keys if json.is_a?(Hash) || json.is_a?(Array)
86
91
  return json
87
92
  end
88
93
 
94
+ def session
95
+ return @request.env['rack.session']
96
+ end
97
+
89
98
  def warden
90
- @request.env["warden"]
99
+ return @request.env["warden"]
91
100
  end
92
101
 
93
102
  def login?
@@ -95,7 +104,7 @@ module Ezframe
95
104
  end
96
105
 
97
106
  def user
98
- warden.user
107
+ return warden.user
99
108
  end
100
109
  end
101
110
  end
@@ -6,6 +6,7 @@ module Ezframe
6
6
  route_h ||= Config[:route].deep_dup
7
7
  # puts "config=#{Config[:route]}, route_h=#{route_h}"
8
8
  args = {}
9
+ opts = {}
9
10
  class_a = []
10
11
  # p path_parts
11
12
  if path_parts.empty?
@@ -21,28 +22,34 @@ module Ezframe
21
22
  while path_parts.length > 0
22
23
  part = path_parts.shift
23
24
  # break if part.empty?
24
- # mylog "part=#{part}, route_h=#{route_h.inspect}"
25
+ # Logger.info "part=#{part}, route_h=#{route_h.inspect}"
25
26
  if route_h.has_key?(part.to_sym)
26
- # mylog "has_route: #{part}"
27
+ # Logger.info "has_route: #{part}"
27
28
  class_a.push(part)
28
29
  if path_parts[0].to_i > 0
29
30
  args[part.to_sym] = val = path_parts.shift
30
- # mylog "value: part=#{part}, val=#{val}"
31
+ # Logger.info "value: part=#{part}, val=#{val}"
31
32
  end
32
33
  route_h = route_h[part.to_sym]
33
34
  break if route_h.nil?
34
- # mylog "route_h changed: #{route_h}"
35
+ opts = {}
36
+ route_h.keys.compact.each do |rkey|
37
+ if rkey =~ /option_(\w+)/
38
+ opt_key = $1
39
+ opts[opt_key.to_sym] = route_h[rkey]
40
+ end
41
+ end
35
42
  else
36
43
  # routeに無ければ、メソッドを探す
37
- # mylog "no_route: #{part}"
44
+ # Logger.info "no_route: #{part}"
38
45
  klass = get_class(class_a[-1])
39
46
  return [ 404 ] unless klass
40
47
  instance = klass.new
41
48
  method_name = make_method_name(part, request.request_method)
42
49
  if instance.respond_to?(method_name)
43
- return [instance, method_name, args]
50
+ return [instance, method_name, args, opts]
44
51
  else
45
- mylog "undefined method: #{klass}.#{method_name}: full path=#{request.path_info}"
52
+ Logger.info "undefined method: #{klass}.#{method_name}: full path=#{request.path_info}"
46
53
  end
47
54
  end
48
55
  end
@@ -56,10 +63,10 @@ module Ezframe
56
63
  part = "default"
57
64
  end
58
65
  method_name = make_method_name(part, request.request_method)
59
- #mylog "method_name=#{method_name}"
66
+ #Logger.info "method_name=#{method_name}"
60
67
  instance = klass.new
61
68
  if instance.respond_to?(method_name)
62
- return [instance, method_name, args]
69
+ return [instance, method_name, args, opts]
63
70
  end
64
71
  return [ 404 ]
65
72
  end
@@ -67,21 +74,23 @@ module Ezframe
67
74
  # ページクラスの階層を辿る
68
75
  def get_path(class_snake, route_h = nil)
69
76
  route_h = Config[:route] unless route_h
77
+ # Logger.info "get_path: route_h=#{route_h}"
70
78
  @get_path_found_it = nil
71
- return scan_route(class_snake, route_h.deep_dup).reverse
79
+ route =_scan_route(class_snake, route_h.deep_dup)
80
+ return route.reverse if route
81
+ return nil
72
82
  end
73
83
 
74
84
  # targetに対応する名称のクラスまでの経路を返す
75
- def scan_route(target, route_h)
76
- # puts "scan_route: target=#{target}, route_h=#{route_h}"
85
+ def _scan_route(target, route_h)
77
86
  if route_h.keys.include?(target.to_sym)
78
87
  @get_path_found_it = true
79
88
  return [ target ]
80
89
  else
81
90
  route_h.each do |k, v|
82
- next if k == :class
91
+ next if k.to_s =~ /^option_/
83
92
  if v.is_a?(Hash)
84
- a = scan_route(target, v)
93
+ a = _scan_route(target, v)
85
94
  if @get_path_found_it
86
95
  a.push(k)
87
96
  return a
@@ -97,11 +106,11 @@ module Ezframe
97
106
  end
98
107
 
99
108
  def get_class(keys)
100
- mylog "get_class: #{keys.inspect}"
109
+ # Logger.info "get_class: #{keys.inspect}"
101
110
  return nil unless keys
102
111
  keys = [ keys ] if keys.is_a?(String)
103
- klass = (%w[Ezframe] + keys.map { |k| k.to_camel }).join("::")
104
- # mylog "get_class: #{klass}"
112
+ klass = (%w[Ezframe] + keys.map { |k| k.to_s.to_camel }).join("::")
113
+ # Logger.info "get_class: #{klass}"
105
114
  if Object.const_defined?(klass)
106
115
  return Object.const_get(klass)
107
116
  else
@@ -109,17 +118,6 @@ module Ezframe
109
118
  end
110
119
  return nil
111
120
  end
112
-
113
- def find_file(target)
114
- Find.find("./asset").each do |file|
115
- path_a = file.split("/")
116
- if path_a[-1] == target
117
- suffix = "." + target.split(".")[-1]
118
- return [200, { "Content-Type" => Rack::Mime.mime_type(suffix) }, [File.open(file, &:read)]]
119
- end
120
- end
121
- return [ 404 ]
122
- end
123
121
  end
124
122
  end
125
123
  end