duffy 0.2.7 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f500ee475e148d1e0f37aeaead04c15e0f280d03
4
- data.tar.gz: a572c350b1398456699d20ce7eac3d39a4cdd667
3
+ metadata.gz: f8f76d6f0836c26f999f5354fb66878964f731c0
4
+ data.tar.gz: f4e5cd23c6bfcf19621e66beb391b873dd37fc3f
5
5
  SHA512:
6
- metadata.gz: 1b1595e172f2769dfb763a91129997db94be1cd0687d151ceb83d1d66b52543884ec3c9a540e7480bee7b9a18f47f28fdd4b5a8e64ab9d1dd93ee981539446bd
7
- data.tar.gz: be4334f85f1c7fcd3d1395de5acf818633d9b659c7262d1d7e32e0f4d532c71080c97c338d26d886026a3270ebeed93a2be93789252cbdae86217ab7641a8605
6
+ metadata.gz: ef4bcde07bca8d30a069bb6ec57d0f8cda82572ff5b4d61f11568ce2681faa03ab5c95d950a2f33bc18d34ec725ddc508c163bdde1759cf13f6179379b9b42ca
7
+ data.tar.gz: 6270daf98b3bf80d76a1a8078e70e2fb5bbd96c08049b55d596bc51d8343e3cfaf621376d23c109d40156c025d2dcaa2baec983e1210215f9747a9bdb5daebd1
data/.rubocop.yml ADDED
@@ -0,0 +1,38 @@
1
+ # 80 characters, Ruby Style Guide? Really? This isn't DOS. 120 -->
2
+ Metrics/LineLength:
3
+ Max: 120
4
+
5
+ # Ruby console always uses double quotes for results. I'm supposed to find-and-replace all my results? Nope.
6
+ Style/StringLiterals:
7
+ Enabled: false
8
+
9
+ # In general I agree with this but don't find it important enough to waste time on.
10
+ Style/EmptyLinesAroundBlockBody:
11
+ Enabled: false
12
+ Style/EmptyLines:
13
+ Enabled: false
14
+
15
+ # I don't think this needs to be strictly one way or the other.
16
+ Style/EmptyMethod:
17
+ Enabled: false
18
+
19
+ # Another one of those it's good enough for ruby irb, it's good enough for me:
20
+ Style/WordArray:
21
+ Enabled: false
22
+
23
+ # These are going to be ginormous, don't worry about it.
24
+ Metrics/BlockLength:
25
+ ExcludedMethods:
26
+ - describe
27
+ - context
28
+
29
+ # I've literally never seen anyone use %i for this, and it looks terrible.
30
+ Style/SymbolArray:
31
+ EnforcedStyle: brackets
32
+
33
+ # Nobody does this and it looks stupid
34
+ Style/NumericLiterals:
35
+ Enabled: false
36
+
37
+ Style/CaseIndentation:
38
+ Enabled: false
data/README.md CHANGED
@@ -62,7 +62,6 @@ habtm | has_and_belongs_to_many
62
62
 
63
63
 
64
64
  ## Git Access:
65
- This one is namespaced in case you use the 'git' gem. I found it to be overkill for what I wanted.
66
65
 
67
66
  Method | Result
68
67
  ----------|---------
@@ -73,8 +72,8 @@ Duffy::Git.branch | Current git branch.
73
72
 
74
73
 
75
74
  ## CPU Detection:
76
- Linux and Mac only for now, each method returns 1 on unsupported hosts.
77
- Example results for my dual core i5 with hyperthreading.
75
+ * Linux and Mac only for now, each method returns 1 on unsupported hosts.
76
+ * Example results for my dual core i5 with hyperthreading.
78
77
 
79
78
  Method | Result
80
79
  ----------|---------
@@ -84,6 +83,16 @@ Duffy::System.threads | 4
84
83
  Duffy::System.sane_load | 3
85
84
  Duffy::System.cpu_percent| 16%
86
85
 
86
+ ## Memory Statistics:
87
+ * All values returned in Megabytes.
88
+ * Available + Used always exactly adds up to Total.
89
+
90
+ Method | Result
91
+ ----------|---------
92
+ Duffy::System.mem_total | 4096
93
+ Duffy::System.mem_available | 3072
94
+ Duffy::System.mem_used | 1024
95
+
87
96
 
88
97
  ## View Helpers:
89
98
 
data/duffy.gemspec CHANGED
@@ -21,6 +21,6 @@ Gem::Specification.new do |spec|
21
21
  spec.add_dependency 'activesupport' # Required for ActiveSupport::Configurable
22
22
  spec.add_development_dependency "bundler", "~> 1.7"
23
23
  spec.add_development_dependency "rake", "~> 10.0"
24
- spec.add_development_dependency "rails"
24
+ spec.add_development_dependency "rails", '>= 5.0'
25
25
  spec.add_development_dependency "rspec"
26
26
  end
@@ -4,37 +4,8 @@ module DuffyHelper
4
4
  # It's just an <li> with a link inside it.
5
5
  # menu_tag('Help', help_path) => <li><a href="/help">Help</a></li>
6
6
  def menu_tag(title, link, options = {})
7
- content_tag(:li, link_to(title, link), options)
8
- end
9
-
10
- # Create Excel icon in consistent location
11
- # This is intended to display the xlsx version of a view.
12
- # Add a yield(:excel) somewhere in your application layout:
13
- # content_tag( :li, yield(:excel), class: :excel) if content_for?(:excel)
14
- def excel_icon(link = params.merge(format: "xlsx"))
15
7
  ActiveSupport::Deprecation.warn('This method will be removed without replacement in future versions')
16
- content_for :excel do
17
- link_to(image_tag('excel.png'), link)
18
- end
19
- end
20
-
21
- # Create a simple link with the class "icon"
22
- # Generate a uniq and reasonable id based on the link passed (if any)
23
- # Add any classes passed in via options
24
- # Add the "favorite" class if icon is favorited
25
- # - This is application specific and will just not do anything if it runs into a problem
26
- # If any uncaught exceptions are thrown in production you'll just get nothing, otherwise you'll get the error on screen.
27
- def icon(title, link, options = {})
28
- ActiveSupport::Deprecation.warn('This method will be removed without replacement in future versions')
29
- begin
30
- options[:id] = "icon_#{link.parameterize("_")}" unless link.blank?
31
- options[:class] = [:icon] << options[:class]
32
- options[:class] << :favorite if defined?(current_user) and (current_user.person.details.where(key: "favorite_#{options[:id]}").any? rescue false)
33
- options[:class] = options[:class].flatten.compact
34
- link_to(title, link, options)
35
- rescue Exception => e
36
- Rails.env.development? ? content_tag(:p, e) : nil
37
- end
8
+ content_tag(:li, link_to(title, link), options)
38
9
  end
39
10
 
40
11
  end
data/lib/duffy/git.rb CHANGED
@@ -1,38 +1,42 @@
1
1
  require 'active_support/core_ext/object/blank'
2
2
 
3
3
  module Duffy
4
-
5
4
  # I like to have a git log in the admin section of my websites. I use these in my capistrano tasks to
6
5
  # generate what I need and upload along with the deployment.
7
6
  # If for some reason you don't have git installed, each method returns nil.
8
-
9
7
  class Git
10
8
  class << self
11
-
12
9
  # Produce tab separated listing of current git log.
13
10
  # Useful for displaying a development history page.
14
11
  def log
15
- `git log --pretty=format:"%ad%x09%an%x09%s" --date=short`.strip.presence rescue nil
12
+ `git log --pretty=format:"%ad%x09%an%x09%s" --date=short`.strip.presence
13
+ rescue
14
+ nil
16
15
  end
17
16
 
18
17
  # I tend use the commit count / 1000.0 as a version for my applications.
19
18
  # You wouldn't want to do that if you're building a gem used by others.
20
19
  def count
21
- `git rev-list HEAD --count`.presence.to_i rescue nil
20
+ `git rev-list HEAD --count`.presence.to_i
21
+ rescue
22
+ nil
22
23
  end
23
24
 
24
25
  # Read the git committer's email.
25
26
  # Uses local if present, otherwise global (git default procedure)
26
27
  # nil if unset
27
28
  def email
28
- `git config --get user.email`.strip.presence rescue nil
29
+ `git config --get user.email`.strip.presence
30
+ rescue
31
+ nil
29
32
  end
30
33
 
31
34
  # Display the current branch
32
35
  def branch
33
- `git rev-parse --abbrev-ref HEAD`.strip.presence rescue nil
36
+ `git rev-parse --abbrev-ref HEAD`.strip.presence
37
+ rescue
38
+ nil
34
39
  end
35
-
36
40
  end
37
41
  end
38
42
  end
data/lib/duffy/railtie.rb CHANGED
@@ -4,13 +4,14 @@ require File.dirname(__FILE__) + "/duffy_helper"
4
4
  require File.dirname(__FILE__) + "/beast_mode_helper"
5
5
 
6
6
  module Duffy
7
+ # Make our helpers available to those host application
7
8
  class Railtie < Rails::Railtie
8
9
  initializer "duffy_helper" do
9
- ActiveSupport.on_load( :action_view ){ include DuffyHelper }
10
+ ActiveSupport.on_load(:action_view) { include DuffyHelper }
10
11
  end
11
12
 
12
13
  initializer "beast_mode_helper" do
13
- ActiveSupport.on_load( :action_view ){ include BeastModeHelper }
14
+ ActiveSupport.on_load(:action_view) { include BeastModeHelper }
14
15
  end
15
16
  end
16
- end
17
+ end
data/lib/duffy/system.rb CHANGED
@@ -1,22 +1,19 @@
1
1
  module Duffy
2
-
3
2
  # Depending on your hardware, you may want to make decisions in your code.
4
3
  # I use this to figure out how many processes I can launch in parallel.
5
-
6
- # This is linux specific (for now) and will return 1 for everything on other systems.
7
-
8
4
  class System
9
5
  class << self
10
-
11
6
  # How many Physical CPUs do you have.
12
7
  # Linux: Detected by counting unique physical IDs
13
8
  # Mac: hw.packages
14
9
  def cpus
15
10
  case RUBY_PLATFORM
16
- when /linux/ then File.read('/proc/cpuinfo').scan(/^physical id.*/).uniq.count rescue 1
17
- when /darwin/ then `sysctl -n hw.packages`.to_i rescue 1
11
+ when /linux/ then File.read('/proc/cpuinfo').scan(/^physical id.*/).uniq.count
12
+ when /darwin/ then `sysctl -n hw.packages`.to_i
18
13
  else 1
19
14
  end
15
+ rescue
16
+ 1
20
17
  end
21
18
 
22
19
  # How many actual CPU cores do we have not including Hyperthreading
@@ -24,19 +21,23 @@ module Duffy
24
21
  # Mac: hw.physicalcpu
25
22
  def cores
26
23
  case RUBY_PLATFORM
27
- when /linux/ then (File.read('/proc/cpuinfo').scan(/^cpu cores.*/).first.scan(/\d+$/).first.to_i rescue 1) * cpus
28
- when /darwin/ then `sysctl -n hw.physicalcpu`.to_i rescue 1
24
+ when /linux/ then File.read('/proc/cpuinfo').scan(/(cpu cores).*(\d+)/)[0][1].to_i * cpus
25
+ when /darwin/ then `sysctl -n hw.physicalcpu`.to_i
29
26
  else 1
30
27
  end
28
+ rescue
29
+ 1
31
30
  end
32
31
 
33
32
  # How many threads does the system have.
34
33
  def threads
35
34
  case RUBY_PLATFORM
36
- when /linux/ then File.read('/proc/cpuinfo').scan(/^processor\s*:/).size rescue 1
37
- when /darwin/ then `sysctl -n hw.ncpu`.to_i rescue 1
35
+ when /linux/ then File.read('/proc/cpuinfo').scan(/^processor\s*:/).size
36
+ when /darwin/ then `sysctl -n hw.ncpu`.to_i
38
37
  else 1
39
38
  end
39
+ rescue
40
+ 1
40
41
  end
41
42
 
42
43
  # What is a sane number of threads to use for data processing.
@@ -46,23 +47,57 @@ module Duffy
46
47
  end
47
48
 
48
49
  # The system's current CPU utilization.
49
- # Darwin: Get a list of all processes' CPU percentage and add them up. Accurate to a couple percent vs. Activity Monitor.
50
+ # Darwin: Get a list of all processes' CPU percentage and add them up.
50
51
  # Linux: Read /proc/stat twice and take the difference to give cpu time used in that interval.
51
52
  def cpu_percent
52
53
  case RUBY_PLATFORM
53
- when /darwin/ then (`ps -A -o %cpu`.lines.map(&:to_f).inject(:+) rescue 0) / threads
54
- when /linux/ then proc_diff rescue 0
54
+ when /darwin/ then `ps -A -o %cpu`.lines.map(&:to_f).inject(:+) / threads
55
+ when /linux/ then proc_diff
56
+ else 0
57
+ end
58
+ rescue
59
+ 0
60
+ end
61
+
62
+ # Total system memory in Megabytes
63
+ # Darwin: hw.memsize (bytes)
64
+ # Linux: Read /proc/meminfo
65
+ def mem_total
66
+ case RUBY_PLATFORM
67
+ when /darwin/ then `sysctl -n hw.memsize`.to_i / 1024 / 1024
68
+ when /linux/ then File.read("/proc/meminfo").scan(/MemTotal:\s*(\d+)/)[0][0].to_i / 1024
69
+ else 0
70
+ end
71
+ rescue
72
+ 0
73
+ end
74
+
75
+ # Memory available for use in Megabytes
76
+ # Darwin: vm_stat (Pages Free + Pages Inactive)
77
+ # Linux: Read /proc/meminfo
78
+ def mem_available
79
+ case RUBY_PLATFORM
80
+ when /darwin/ then `vm_stat`.lines.grep(/(free:|inactive:)\s*(\d+)/){$2}.map(&:to_i).inject(:+) * 4 / 1024
81
+ when /linux/ then (File.read("/proc/meminfo").lines.grep(/MemAvailable/).first.split[1].to_i / 1024)
55
82
  else 0
56
83
  end
84
+ rescue
85
+ 0
57
86
  end
58
87
 
88
+ # Memory used
89
+ # Subtract mem_available from mem_total.
90
+ # This ignores file cache etc that is not actually used by programs.
91
+ def mem_used
92
+ mem_total - mem_available
93
+ end
59
94
 
60
95
  private
61
96
 
62
97
  # [CPU USE, CPU IDLE]
63
98
  def proc_stat
64
99
  cpu = File.open("/proc/stat", "r").read.lines.first
65
- [cpu.split[1 .. 3].map(&:to_i).inject(:+), cpu.split[1 .. 4].map(&:to_i).inject(:+)]
100
+ [cpu.split[1..3].map(&:to_i).inject(:+), cpu.split[1..4].map(&:to_i).inject(:+)]
66
101
  end
67
102
 
68
103
  # Poll proc_stat twice and find the usage in that time.
@@ -73,7 +108,6 @@ module Duffy
73
108
  f = proc_stat
74
109
  (f[0] - s[0]) * 100.0 / (f[1] - s[1])
75
110
  end
76
-
77
111
  end
78
112
  end
79
113
  end
data/lib/duffy/version.rb CHANGED
@@ -1,8 +1,9 @@
1
1
  module Duffy
2
- VERSION = "0.2.7"
2
+ VERSION = "0.3.0"
3
3
  end
4
4
 
5
5
  # History
6
+ # 0.3.0 - Added system memory methods. Removed deprecated icon methods.
6
7
  # 0.2.7 - Deprecated "icon" and "excel_icon"
7
8
  # 0.2.6 - Added cpu_percent to give the current total CPU usage percentage.
8
9
  # 0.2.5 - Added beast_mode helper to render partials in parallel
data/lib/duffy.rb CHANGED
@@ -24,7 +24,6 @@ require File.dirname(__FILE__) + "/duffy/version"
24
24
  # [application.css]
25
25
  # *= require duffy/reset
26
26
  module Duffy
27
-
28
27
  def self.configuration
29
28
  @configuration ||= Duffy::Configuration.new
30
29
  end
@@ -56,8 +55,4 @@ module Duffy
56
55
  config_accessor(:fiscal_month) {7}
57
56
  config_accessor(:fiscal_day) {1}
58
57
  end
59
-
60
58
  end
61
-
62
-
63
-
data/spec/date_spec.rb CHANGED
@@ -4,23 +4,22 @@ describe Date do
4
4
  describe "fiscal_year (Assuming July 1st Start)" do
5
5
 
6
6
  it "2000-07-01 => 2001" do
7
- expect(Date.new(2000,7,1).fiscal_year).to eq(2001)
7
+ expect(Date.new(2000, 7, 1).fiscal_year).to eq(2001)
8
8
  end
9
9
 
10
10
  it "2000-06-30 => 2000" do
11
- expect(Date.new(2000,6,30).fiscal_year).to eq(2000)
11
+ expect(Date.new(2000, 6, 30).fiscal_year).to eq(2000)
12
12
  end
13
13
 
14
14
  it "2000-01-01 => 2000" do
15
- expect(Date.new(2000,1,1).fiscal_year).to eq(2000)
15
+ expect(Date.new(2000, 1, 1).fiscal_year).to eq(2000)
16
16
  end
17
17
 
18
18
  it "1999-12-31 => 2000" do
19
- expect(Date.new(1999,12,31).fiscal_year).to eq(2000)
19
+ expect(Date.new(1999, 12, 31).fiscal_year).to eq(2000)
20
20
  end
21
21
 
22
22
  end
23
23
 
24
24
 
25
25
  end
26
-
data/spec/string_spec.rb CHANGED
@@ -79,4 +79,4 @@ describe String do
79
79
 
80
80
 
81
81
 
82
- end
82
+ end
data/spec/system_spec.rb CHANGED
@@ -1,23 +1,28 @@
1
1
  require 'spec_helper'
2
2
  describe Duffy::System do
3
3
 
4
- # These methods return per-host settings, so all I can think of to test is that they return integers.
4
+ # These methods return per-host settings. Verify the return object type and value within reason.
5
5
  # This generates the documentation more than anything.
6
6
  describe "cpus" do
7
7
  it "returns the number of physical cpus" do
8
8
  expect(Duffy::System.cpus).to be_an(Integer)
9
+ expect(Duffy::System.cpus).to be >= 1
9
10
  end
10
11
  end
11
12
 
12
13
  describe "cores" do
13
14
  it "returns the total number of CPU cores" do
14
15
  expect(Duffy::System.cores).to be_an(Integer)
16
+ expect(Duffy::System.cores).to be >= 1
17
+
15
18
  end
16
19
  end
17
20
 
18
21
  describe "threads" do
19
22
  it "returns the total number of threads" do
20
23
  expect(Duffy::System.threads).to be_an(Integer)
24
+ expect(Duffy::System.threads).to be >= 1
25
+
21
26
  end
22
27
  end
23
28
 
@@ -27,8 +32,26 @@ describe Duffy::System do
27
32
  end
28
33
  end
29
34
 
35
+ describe 'mem_total' do
36
+ it "returns the total amount of physical memory in Megabytes" do
37
+ expect(Duffy::System.mem_total).to be_an(Integer)
38
+ expect(Duffy::System.mem_total).to be > 0
39
+ end
40
+ end
30
41
 
42
+ describe 'mem_available' do
43
+ it "returns the amount of available memory in Megabytes" do
44
+ expect(Duffy::System.mem_available).to be_an(Integer)
45
+ expect(Duffy::System.mem_available).to be > 0
46
+ end
47
+ end
31
48
 
49
+ describe 'mem_used' do
50
+ it "returns the amount of used memory in Megabytes" do
51
+ expect(Duffy::System.mem_used).to be_an(Integer)
52
+ expect(Duffy::System.mem_used).to be > 0
53
+ end
54
+ end
32
55
 
33
56
 
34
57
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: duffy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.7
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jacob Duffy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-20 00:00:00.000000000 Z
11
+ date: 2017-05-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -58,14 +58,14 @@ dependencies:
58
58
  requirements:
59
59
  - - ">="
60
60
  - !ruby/object:Gem::Version
61
- version: '0'
61
+ version: '5.0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
- version: '0'
68
+ version: '5.0'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rspec
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -89,6 +89,7 @@ extra_rdoc_files: []
89
89
  files:
90
90
  - ".gitignore"
91
91
  - ".rspec"
92
+ - ".rubocop.yml"
92
93
  - ".travis.yml"
93
94
  - Gemfile
94
95
  - LICENSE.txt
@@ -134,7 +135,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
134
135
  version: '0'
135
136
  requirements: []
136
137
  rubyforge_project:
137
- rubygems_version: 2.5.1
138
+ rubygems_version: 2.6.12
138
139
  signing_key:
139
140
  specification_version: 4
140
141
  summary: Library of things