javy_tool 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in javy_tool.gemspec
4
+ gemspec
data/LICENSE.md ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 qmliu
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # JavyTool
2
+
3
+ Just pack some methods that ofen used to a gem,ex:h2o,truncate etc..
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'javy_tool'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install javy_tool
18
+
19
+ ## Usage
20
+
21
+ JavyTool.h2o(aHash)
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/javy_tool.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'javy_tool/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "javy_tool"
8
+ gem.version = JavyTool::VERSION
9
+ gem.authors = ["javy_liu"]
10
+ gem.email = ["javy_liu@163.com"]
11
+ gem.description = %q{pack some used methods}
12
+ gem.summary = %q{some methods ofen used in my projects}
13
+ gem.homepage = "https://github.com/javyliu/javy_tool"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ #s.add_dependency "rails", "~> 3.2.6"
21
+ end
@@ -0,0 +1,174 @@
1
+ module JavyTool
2
+ module Breadcrumb
3
+ module ClassMethods
4
+
5
+ end
6
+
7
+ module InstanceMethods
8
+ protected
9
+
10
+ def set_breadcrumbs
11
+ #@breadcrumbs = ["#{I18n.t("common.locate_des")}<a href='/wb/'>#{I18n.t("common.forum_home")}</a>".html_safe]
12
+ end
13
+
14
+ def drop_breadcrumb(title=nil, url=nil)
15
+ title ||= @page_title
16
+ #url ||= url_for
17
+ if title
18
+ if url
19
+ @breadcrumbs.push("<a href=\"#{url}\">#{title}</a>".html_safe)
20
+ else
21
+ @breadcrumbs.push("#{title}")
22
+ end
23
+ end
24
+ end
25
+
26
+ def drop_page_title(title)
27
+ @page_title = title
28
+ return @page_title
29
+ end
30
+
31
+ def no_breadcrumbs
32
+ @breadcrumbs = []
33
+ end
34
+ end
35
+
36
+ module Helpers
37
+ def yield_or_default(message, default_message = "")
38
+ message.nil? ? default_message : message
39
+ end
40
+ # set SITE_NAME in enviroment.rb
41
+ # set @page_title in controller respectively
42
+ # add<%= render_page_title %> in head
43
+ def render_page_title
44
+ title = @page_title ? "#{@page_title}_#{SITE_NAME}" : SITE_NAME rescue "SITE_NAME"
45
+ content_tag("title", title, nil, false)
46
+ end
47
+
48
+ def render_body_tag
49
+ class_attribute = ["#{controller_name}-controller","#{action_name}-action"].join(" ")
50
+ id_attribute = (@body_id)? " id=\"#{@body_id}-page\"" : ""
51
+
52
+ raw(%Q|<!--[if lt IE 7 ]>
53
+ <body class="#{class_attribute} ie6"><![endif]-->
54
+ <!--[if gte IE 7 ]>
55
+ <body class="#{class_attribute} ie"><![endif]-->
56
+ <!--[if !IE]>-->
57
+ <body#{id_attribute} class="#{class_attribute}">
58
+ <!--<![endif]-->|)
59
+
60
+ end
61
+
62
+
63
+ # display the flash messages
64
+ def notice_message
65
+ flash_messages = []
66
+ flash.each do |type, message|
67
+ type = :success if type == :notice
68
+ type = :alert if type == :error
69
+ text = content_tag(:div, message, :class => "alert-#{type} alert-box")
70
+ flash_messages << text if message
71
+ end
72
+ flash_messages.join("\n").html_safe
73
+ end
74
+
75
+ def s(html)
76
+ sanitize( html, :tags => %w(table thead tbody tr td th ol ul li div span font img sup sub br hr a pre p h1 h2 h3 h4 h5 h6), :attributes => %w(id class style src href size color) )
77
+ end
78
+
79
+ def render_breadcrumb
80
+ return "" if @breadcrumbs.nil? || @breadcrumbs.size <= 0
81
+ prefix = "".html_safe
82
+ crumb = "".html_safe
83
+
84
+ @breadcrumbs.each_with_index do |c, i|
85
+ breadcrumb_class = []
86
+ breadcrumb_class << "active" if i == (@breadcrumbs.length - 1)
87
+ if i == (@breadcrumbs.length - 1)
88
+ breadcrumb_content = c
89
+ else
90
+ breadcrumb_content = c + content_tag(:span, ">", :class => "divider")
91
+ end
92
+
93
+ crumb += content_tag(:li, breadcrumb_content ,:class => breadcrumb_class )
94
+ end
95
+ return prefix + content_tag(:ul, crumb, :class => "breadcrumb")
96
+ end
97
+
98
+ # add nested object to the parent form by ajax
99
+ # parameters:
100
+ # name: link title
101
+ # f: form_for's f
102
+ # association: association object
103
+ # example:
104
+ # <%=link_to_add_fields("Add Cycle",f,:pipgame_cycles,f.object.pipgame_cycles.new(:item_type => "App",:position => "cycle_img")) %>
105
+ # this method will render _pipgame_cycle_fields.html.erb file like
106
+ #
107
+ # <div class="fields" rel="need_upload">
108
+ # <p>
109
+ # <%= f.label :img_src %><br />
110
+ # <%= f.hidden_field :img_src,:class=>"web_img_src" %>
111
+ # <%= f.hidden_field :item_type %>
112
+ # <%= f.hidden_field :position %>
113
+ # <%= f.hidden_field :_destroy %>
114
+ # <%= link_to_function "remove", "remove_fields(this)" %>
115
+ # </p>
116
+ # </div>
117
+ #
118
+ # and you need add javascript like following:
119
+ #
120
+ # function remove_fields(link) {
121
+ # $(link).prev("input[type=hidden]").val("1");
122
+ # $(link).closest(".fields").hide();
123
+ # }
124
+ # function add_fields(link, association, content) {
125
+ # var new_id = new Date().getTime();
126
+ # var regexp = new RegExp("new_" + association, "g");
127
+ # var con = content.replace(regexp, new_id);
128
+ # //console.log(con);
129
+ # $(link).parent().before(con);
130
+ # }
131
+ #
132
+ # deprecated in rails4 need to fix the link_to_founction
133
+ def link_to_add_fields(name, f, association,new_object=nil)
134
+ new_object ||= f.object.class.reflect_on_association(association).klass.new()
135
+ fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
136
+ render(association.to_s.singularize + "_fields", :f => builder)
137
+ end
138
+ link_to_function(name, "add_fields(this, \"#{association}\", \"#{escape_javascript(fields.html_safe)}\")")
139
+ end
140
+
141
+
142
+ #set head description,keywords etc
143
+ def head(head_identity=nil)
144
+ content_for(:head) do
145
+ case head_identity
146
+ when "description"
147
+ "<meta name=\"description\" content=\"#{yield}\" />\n"
148
+ when "keywords"
149
+ "<meta name=\"keywords\" content=\"#{yield}\" />\n"
150
+ else
151
+ yield
152
+ end.html_safe
153
+ end
154
+ end
155
+ #add javascript to foot
156
+ def foot
157
+ content_for(:foot) do
158
+ yield.html_safe
159
+ end
160
+ end
161
+ #format time
162
+ def format_timestamp(ts,format='%Y-%m-%d %H:%M')
163
+ ts.strftime(format)
164
+ end
165
+ end
166
+
167
+ def self.included(receiver)
168
+ receiver.extend ClassMethods
169
+ receiver.send :include, InstanceMethods
170
+ receiver.send :helper, Helpers
171
+ receiver.send :before_filter, :set_breadcrumbs
172
+ end
173
+ end
174
+ end
@@ -0,0 +1,51 @@
1
+ module JavyTool
2
+ module Utils
3
+ require "ostruct"
4
+ module_function
5
+ # translate a Hash object to a OpenStruct object
6
+ # parameter:
7
+ # ahash => Hash
8
+ # return: a OpenStruct object
9
+ def h2o( ahash )
10
+ return ahash unless Hash === ahash
11
+ OpenStruct.new( Hash[ *ahash.inject( [] ) { |a, (k, v)| a.push(k, h2o(v)) } ] )
12
+ end
13
+
14
+ # truncate string with utf-8 encoding
15
+ def truncate_u(text, length = 30, truncate_string = "...")
16
+ l=0
17
+ char_array=text.unpack("U*")
18
+ char_array.each_with_index do |c,i|
19
+ l = l+ (c<127 ? 0.5 : 1)
20
+ if l>=length
21
+ return char_array[0..i].pack("U*")+(i<char_array.length-1 ? truncate_string : "")
22
+ end
23
+ end
24
+ return text
25
+ end
26
+
27
+ # truncate string and chinese is two chars
28
+ def truncate_o(text,length=16)
29
+ text = Iconv.conv("gb2312","utf8",text)[0,length]
30
+ Iconv.conv("utf8","gb2312",text)
31
+ end
32
+
33
+ # upload file,default to /tmp
34
+ # return filename
35
+ def upload_file(file,path=nil)
36
+ path ||= JavyTool.options[:upload_path]
37
+ unless file.original_filename.empty?
38
+ filename = if block_given?
39
+ yield file.original_filename
40
+ else
41
+ Time.now.strftime("%Y%m%d%H%M%S") + rand(10000).to_s + File.extname(file.original_filename)
42
+ # if File.extname(file.original_filename).downcase == ".apk"
43
+ # file.original_filename.gsub(/[^\w]/,'') #
44
+ # end
45
+ end
46
+ File.open(path+filename, "wb") { |f| f.write(file.read) }
47
+ filename
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,3 @@
1
+ module JavyTool
2
+ VERSION = "0.0.1"
3
+ end
data/lib/javy_tool.rb ADDED
@@ -0,0 +1,18 @@
1
+ require "javy_tool/version"
2
+
3
+ module JavyTool
4
+ autoload :Utils, "javy_tool/utils"
5
+ autoload :Breadcrumb, "javy_tool/breadcrumb"
6
+ mattr_accessor :tool_config
7
+ mattr_accessor :upload_path
8
+ @@upload_path = "/tmp"
9
+
10
+ def self.setup
11
+ yield self
12
+ end
13
+ def self.options
14
+ @@options ||= {
15
+ :upload_path => upload_path
16
+ }
17
+ end
18
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: javy_tool
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - javy_liu
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-12-20 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: pack some used methods
15
+ email:
16
+ - javy_liu@163.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE.md
24
+ - README.md
25
+ - Rakefile
26
+ - javy_tool.gemspec
27
+ - lib/javy_tool.rb
28
+ - lib/javy_tool/breadcrumb.rb
29
+ - lib/javy_tool/utils.rb
30
+ - lib/javy_tool/version.rb
31
+ homepage: https://github.com/javyliu/javy_tool
32
+ licenses: []
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubyforge_project:
51
+ rubygems_version: 1.8.24
52
+ signing_key:
53
+ specification_version: 3
54
+ summary: some methods ofen used in my projects
55
+ test_files: []
56
+ has_rdoc: