ecraft-extensions 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e983d84a091fa978a907730011ec23a79064d6d1
4
+ data.tar.gz: 4959d94f2267daffae1fd29f2d48be6c63621140
5
+ SHA512:
6
+ metadata.gz: 6a8e05e42037785fe1a3bfe3124abead4f8301be8c60dd90760a190e98ce27368cee48bf38f84e03d5668a852be8f25eb039ecc69905b96edd6a334ca6fe841b
7
+ data.tar.gz: 1c0a4f8e3d0ae2582d3682e38ff54dd8cc2a5bc932e913da2f7784c56612fb50926a96bfd90103b62c7db7eb7bc62a14ffda615cf2dcaa18181628b4116f64d2
@@ -0,0 +1,3 @@
1
+ Gemfile.lock
2
+ .yardoc/
3
+ coverage/
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ -rspec_helper
@@ -0,0 +1,35 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.3
3
+ DisplayCopNames: true
4
+
5
+ Lint/EndAlignment:
6
+ EnforcedStyleAlignWith: variable
7
+
8
+ Metrics/BlockLength:
9
+ Exclude:
10
+ - spec/**/*
11
+ Metrics/LineLength:
12
+ Max: 132
13
+ Metrics/MethodLength:
14
+ Severity: warning
15
+
16
+ # Rationale: allow Weirich-style blocks
17
+ Style/BlockDelimiters:
18
+ Enabled: false
19
+
20
+ # Needed since core extensions might get included from `module_eval` contexts.
21
+ Style/ClassAndModuleChildren:
22
+ Enabled: false
23
+
24
+ Style/Documentation:
25
+ Enabled: false
26
+ Style/Encoding:
27
+ Enabled: false
28
+ Style/FileName:
29
+ Enabled: false
30
+ Style/MultilineOperationIndentation:
31
+ EnforcedStyle: indented
32
+ Style/NumericPredicate:
33
+ Enabled: false
34
+ Style/FrozenStringLiteralComment:
35
+ Enabled: false
@@ -0,0 +1,16 @@
1
+ sudo: false
2
+ language: ruby
3
+
4
+ # Dummy version to avoid wasting time on RVM installs.
5
+ rvm: 1.9.3
6
+
7
+ env:
8
+ - "JRUBY_OPTS=--debug"
9
+
10
+ before_install:
11
+ - rm -rf ~/.rvm && if [ ! -d "$HOME/jruby-bin" ]; then mkdir -p ~/jruby-bin && pushd ~/jruby-bin && curl -o- -L https://s3.amazonaws.com/jruby.org/downloads/9.1.8.0/jruby-bin-9.1.8.0.tar.gz | tar xzf - --strip-components=1 && popd ; fi
12
+ - export PATH="$HOME/jruby-bin/bin:$PATH"
13
+ - gem install bundler
14
+ - gem update --system
15
+
16
+ cache: bundler
@@ -0,0 +1 @@
1
+ --markup markdown --no-private **/*.rb
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ecraft-web-dsl-builtins.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,23 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) eCraft Oy Ab and Contributors
4
+
5
+ All rights reserved.
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ of this software and associated documentation files (the "Software"), to deal
9
+ in the Software without restriction, including without limitation the rights
10
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ copies of the Software, and to permit persons to whom the Software is
12
+ furnished to do so, subject to the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be included in all
15
+ copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ SOFTWARE.
@@ -0,0 +1,62 @@
1
+ [![Build Status](https://travis-ci.com/ecraft/ecraft-extensions.svg?token=BUqjqARNkptMDoMbPtFS&branch=master)](https://travis-ci.com/ecraft/ecraft-extensions)
2
+
3
+ # Ecraft::Extensions
4
+
5
+ This code contains some useful extension methods to various Ruby classes.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'ecraft-extensions'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install ecraft-extensions
22
+
23
+ ## Usage
24
+
25
+ Preferred approach:
26
+
27
+ ```ruby
28
+ # To load a specific extension:
29
+ require 'ecraft/extensions/bigdecimal'
30
+ ```
31
+
32
+ Not-so-preferred approach:
33
+
34
+ ```ruby
35
+ # To load _all_ core extensions. USE WITH GREAT CARE, since this could cause conflicts with other libraries.
36
+ require 'ecraft/extensions/all'
37
+ ```
38
+
39
+ ## Development
40
+
41
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
42
+
43
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
44
+
45
+ ## Contributing
46
+
47
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ecraft/extensions. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
48
+
49
+ ### Documentation
50
+
51
+ We strive to write documentation inline in [YARD](http://yardoc.org) format.
52
+
53
+ ### Previewing documentation locally
54
+ ```
55
+ bundle exec yard server -r
56
+ ```
57
+
58
+ Check the locally served documentation at http://localhost:8808/
59
+
60
+ ### License
61
+
62
+ MIT
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: %i[linters spec]
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'ecraft/extensions/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'ecraft-extensions'
9
+ spec.version = Ecraft::Extensions::VERSION
10
+ spec.authors = ['Tre Kronor']
11
+ spec.email = ['team.trekronor@ecraft.com']
12
+
13
+ spec.summary = 'eCraft Extensions'
14
+ spec.description = 'Extension methods for various Ruby classes'
15
+ spec.homepage = 'https://github.com/ecraft/ecraft-extensions'
16
+ spec.licenses = []
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ spec.bindir = 'exe'
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ['lib']
22
+
23
+ spec.add_development_dependency 'bundler'
24
+ spec.add_development_dependency 'coveralls'
25
+ spec.add_development_dependency 'guard-rspec', '~> 4.7'
26
+ spec.add_development_dependency 'rake'
27
+ spec.add_development_dependency 'rspec'
28
+ spec.add_development_dependency 'rubocop', '~> 0.47'
29
+ spec.add_development_dependency 'simplecov'
30
+ spec.add_development_dependency 'yard'
31
+ end
@@ -0,0 +1 @@
1
+ require 'ecraft/extensions/version'
@@ -0,0 +1,7 @@
1
+ # Requiring this file will activate _all_ the "core extensions" defined in ecraft-extensions. This may or may not be what you want.
2
+ # It is usually a better, safer approach to selectively require only the parts you know that you need, since core extensions _can_
3
+ # potentially cause conflicts with other libraries extending core classes (such as ActiveSupport).
4
+ require 'ecraft/extensions/bigdecimal'
5
+ require 'ecraft/extensions/date'
6
+ require 'ecraft/extensions/datetime'
7
+ require 'ecraft/extensions/ostruct'
@@ -0,0 +1,44 @@
1
+ require 'bigdecimal'
2
+
3
+ # This extension assists BigDecimals coming out of Sequel resultsets with displaying as integers.
4
+ #
5
+ # By default, `BigDecimal` objects serialized to JSON or string format have two defects:
6
+ #
7
+ # 1. They get serialized in scientific notation (i.e. `1` gets serialized as `0.1E1`).
8
+ # 2. Even if you use the `to_s('F')` approach, `1` will get converted to `1.0`. This is useful to force the data to be converted
9
+ # to a `Float` number if deserialized, but it causes practical problems since field like the M3 company number is normally
10
+ # stored in a `DECIMAL(3)` field in the database. With libraries like [Sequel](http://sequel.jeremyevans.net), this causes
11
+ # the data coming out from the database to be stored in a `BigDecimal`, even though it is really just a large integer.
12
+ #
13
+ # For our .NET-based applications to be able to parse such fields to an `int` field, we needed to strip away the `.0` part so that
14
+ # `BigDecimal(1).to_s` becomes simply `"1"`.
15
+ #
16
+ # More details and history around this can be found in this discussion: https://github.com/ecraft/ecraft-extensions/pull/1
17
+ class ::BigDecimal
18
+ # The original (non-patched) `to_s` implementation.
19
+ alias to_s_original to_s
20
+
21
+ # Converts this object to JSON form, in the simplest string form possible, either looking like a `Float` or `Fixnum`.
22
+ #
23
+ # Values without any fraction digits will use `Fixnum` form, where values with a fractional part will use `Float` form.
24
+ #
25
+ # @return [String]
26
+ def to_json(*_a)
27
+ to_s
28
+ end
29
+
30
+ # Converts this object to the simplest string form possible, either looking like a `Float` or `Fixnum`.
31
+ #
32
+ # Values without any fraction digits will use `Fixnum` form, where values with a fractional part will use `Float` form.
33
+ #
34
+ # @param s [String] The format that should be used for the conversion. If provided, the original `to_s` implementation will be
35
+ # called using this parameter to provide the same semantics as the original method.
36
+ # @return [String]
37
+ def to_s(s = nil)
38
+ if s.nil?
39
+ to_s_original('F').gsub(/\.0$/, '')
40
+ else
41
+ to_s_original(s)
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,7 @@
1
+ # This is a bit crazy, but we need it because the 'json/add/core' adds a really weird serialization of date fields.
2
+ # It looks like this: {"json_class":"Date","y":2000,"m":1,"d":1,"sg":2299161.0}
3
+ class ::Date
4
+ def to_json(*_args)
5
+ format('"%d-%02d-%02d"', year, month, day)
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ class ::DateTime
2
+ def to_json(*_args)
3
+ '"' + to_s + '"'
4
+ end
5
+ end
@@ -0,0 +1,7 @@
1
+ require 'ostruct'
2
+
3
+ class ::OpenStruct
4
+ def to_json(*_a)
5
+ marshal_dump.to_json
6
+ end
7
+ end
@@ -0,0 +1,44 @@
1
+ class ::String
2
+ # These methods should not be used in new code
3
+ # Instead use https://github.com/ecraft/ecraft.uxfactory.server/blob/master/documentation/upgrading-from-facets.md
4
+
5
+ # require 'active_support'
6
+ # require 'active_support/core_ext/string/inflections'
7
+ #
8
+ # 'active_support_string'.camelize # result: "ActiveSupportString"
9
+ def to_camelcase
10
+ separators = ['_', '\s']
11
+ str = dup
12
+ separators.each do |s|
13
+ str = str.gsub(/(?:#{s}+)([a-z])/) { Regexp.last_match(1).upcase }
14
+ end
15
+ str = str.gsub(/(\A|\s)([a-z])/) { Regexp.last_match(1) + Regexp.last_match(2).upcase }
16
+ str
17
+ end
18
+
19
+ # require 'active_support'
20
+ # require 'active_support/core_ext/string/inflections'
21
+ #
22
+ # 'ActiveSupportString'.underscore # result: "active_support_string"
23
+ def to_snake_case
24
+ gsub(/\B[A-Z]/, '_\&').downcase
25
+ end
26
+
27
+ # require 'active_support'
28
+ # require 'active_support/core_ext/string/inflections'
29
+ # 'Foo::Bar'.constantize # => Foo::Bar
30
+ def to_class
31
+ chain = split '::'
32
+ klass = Kernel
33
+ chain.each do |klass_string|
34
+ klass = klass.const_get klass_string
35
+ end
36
+ klass.is_a?(Class) ? klass : nil
37
+ rescue NameError
38
+ nil
39
+ end
40
+
41
+ def to_bool
42
+ !self !~ /^(true|t|yes|y|1)$/i
43
+ end
44
+ end
@@ -0,0 +1,5 @@
1
+ class ::Symbol
2
+ def to_json(_options = {})
3
+ "\"#{self}\""
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ class ::Time
2
+ def as_ms
3
+ to_i * 1000 + usec / 1000
4
+ end
5
+
6
+ def to_json(*_a)
7
+ iso8601.to_json
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ module Ecraft
2
+ module Extensions
3
+ VERSION = '0.2.1'.freeze
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ desc 'Runs the linters'
2
+ task :linters do
3
+ rubocop_passed = system 'bundle exec rubocop'
4
+ raise 'Error running Rubocop and/or CoffeeLint' unless rubocop_passed
5
+ end
metadata ADDED
@@ -0,0 +1,176 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ecraft-extensions
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.1
5
+ platform: ruby
6
+ authors:
7
+ - Tre Kronor
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-05-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: coveralls
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: guard-rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '4.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '4.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.47'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.47'
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: yard
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: Extension methods for various Ruby classes
126
+ email:
127
+ - team.trekronor@ecraft.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".gitignore"
133
+ - ".rspec"
134
+ - ".rubocop.yml"
135
+ - ".travis.yml"
136
+ - ".yardopts"
137
+ - Gemfile
138
+ - LICENSE
139
+ - README.md
140
+ - Rakefile
141
+ - ecraft-extensions.gemspec
142
+ - lib/ecraft/extensions.rb
143
+ - lib/ecraft/extensions/all.rb
144
+ - lib/ecraft/extensions/bigdecimal.rb
145
+ - lib/ecraft/extensions/date.rb
146
+ - lib/ecraft/extensions/datetime.rb
147
+ - lib/ecraft/extensions/ostruct.rb
148
+ - lib/ecraft/extensions/string.rb
149
+ - lib/ecraft/extensions/symbol.rb
150
+ - lib/ecraft/extensions/time.rb
151
+ - lib/ecraft/extensions/version.rb
152
+ - rakelib/linters.rake
153
+ homepage: https://github.com/ecraft/ecraft-extensions
154
+ licenses: []
155
+ metadata: {}
156
+ post_install_message:
157
+ rdoc_options: []
158
+ require_paths:
159
+ - lib
160
+ required_ruby_version: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - ">="
163
+ - !ruby/object:Gem::Version
164
+ version: '0'
165
+ required_rubygems_version: !ruby/object:Gem::Requirement
166
+ requirements:
167
+ - - ">="
168
+ - !ruby/object:Gem::Version
169
+ version: '0'
170
+ requirements: []
171
+ rubyforge_project:
172
+ rubygems_version: 2.6.11
173
+ signing_key:
174
+ specification_version: 4
175
+ summary: eCraft Extensions
176
+ test_files: []