adjustment_bureau 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,21 @@
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
18
+ .ruby-gemset
19
+ .ruby-version
20
+ .irbrc
21
+ .rspec
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in adjustment_bureau.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 bjh
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,71 @@
1
+ # AdjustmentBureau
2
+
3
+ A CSS Property Parser with the simple task of applying a numeric adjustment to property values.
4
+
5
+ **NOTE:** this does not parse CSS, it parses CSS property strings only. i.e. `margin: 1px 2px 3px 4px;`.
6
+
7
+ ## Example
8
+
9
+ ```ruby
10
+ # parse the property directly and adjust it
11
+ property = AdjustmentBureau::Property.parse('margin: 1px 2px 3px 4px;')
12
+ property.adjust(:+, 10)
13
+ expect(property.to_s).to eq 'margin: 11px 12px 13px 14px;' #=> true
14
+ ```
15
+
16
+ ```ruby
17
+ # set up an adjuster and use it on multiple properties
18
+ properties = [
19
+ 'top: 100px;',
20
+ 'left: 100px;',
21
+ 'width: 100px;',
22
+ 'height: 100px;'
23
+ ]
24
+
25
+ adjustable_properties = [:top, :left]
26
+ adjustment_operation = :+
27
+ adjustment_amount = 10
28
+
29
+ adjuster = AdjustmentBureau::Adjuster.new(
30
+ adjustable_properties,
31
+ adjustment_operation,
32
+ adjustment_amount)
33
+
34
+ properties.map! { |p| adjuster.adjust(p).to_s }
35
+
36
+ # expected output
37
+ expected = [
38
+ 'top: 110px;',
39
+ 'left: 110px;',
40
+ 'width: 100px;',
41
+ 'height: 100px;'
42
+ ]
43
+
44
+ expect(properties).to eq expected #=> true
45
+ ```
46
+
47
+ ## Installation
48
+
49
+ Add this line to your application's Gemfile:
50
+
51
+ gem 'adjustment_bureau'
52
+
53
+ And then execute:
54
+
55
+ $ bundle
56
+
57
+ Or install it yourself as:
58
+
59
+ $ gem install adjustment_bureau
60
+
61
+ ## Usage
62
+
63
+ TODO: Write usage instructions here
64
+
65
+ ## Contributing
66
+
67
+ 1. Fork it ( http://github.com/<my-github-username>/adjustment_bureau/fork )
68
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
69
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
70
+ 4. Push to the branch (`git push origin my-new-feature`)
71
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new('spec')
5
+
6
+ task default: :spec
7
+
8
+ desc 'load gem files into IRB'
9
+ task :console do
10
+ exec 'irb -Ilib -radjustment_bureau'
11
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'adjustment_bureau/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "adjustment_bureau"
8
+ spec.version = AdjustmentBureau::VERSION
9
+ spec.authors = ["bjh"]
10
+ spec.email = ["fake@fake.com"]
11
+ spec.summary = %q{A CSS property adjuster}
12
+ spec.description = %q{Something I needed at work so I gemmed it up...}
13
+ spec.homepage = "https://github.com/bjh/adjustment_bureau"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "guard-rspec"
25
+ end
@@ -0,0 +1,4 @@
1
+ require "adjustment_bureau/version"
2
+ require "adjustment_bureau/value"
3
+ require "adjustment_bureau/property"
4
+ require "adjustment_bureau/adjuster"
@@ -0,0 +1,25 @@
1
+ require_relative 'parser'
2
+
3
+ module AdjustmentBureau
4
+ class Adjuster
5
+ attr_accessor :properties, :type, :amount
6
+
7
+ def initialize(adjustable_properties_list, type, amount)
8
+ @properties = adjustable_properties_list.map &:to_s
9
+ @type = type
10
+ @amount = amount
11
+ end
12
+
13
+ def adjust(property_string)
14
+ property = Property.parse(property_string)
15
+ property.adjust(type, amount) if adjustable? property
16
+ property
17
+ end
18
+
19
+ private
20
+
21
+ def adjustable?(property)
22
+ @properties.include? property.name
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,17 @@
1
+
2
+ module AdjustmentBureau
3
+ module Helpers
4
+ def self.numeric?(value)
5
+ Float(value) != nil rescue false
6
+ end
7
+
8
+ def self.numerize(value)
9
+ if Helpers.numeric? value
10
+ value = Float(value)
11
+ value = (value == value.floor) ? value.to_i : value.round(2)
12
+ end
13
+
14
+ value
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,38 @@
1
+ require_relative 'value'
2
+ require_relative 'helpers'
3
+
4
+ module AdjustmentBureau
5
+ class Parser
6
+
7
+ def parse(property_string)
8
+ name = parse_name(property_string)
9
+ values = parse_values(property_string)
10
+ return [name, values]
11
+ end
12
+
13
+ private
14
+
15
+ def parse_name(property_string)
16
+ property_string.split(':')[0].strip
17
+ end
18
+
19
+ def parse_values(property_string)
20
+ values = property_string.split(':')[1].strip.gsub(/\;/, '')
21
+
22
+ values.split(/\s+/m).collect do |value|
23
+ Value.new(*parse_value(value))
24
+ end
25
+ end
26
+
27
+ def parse_value(value)
28
+ n = (value = value.to_s.strip)
29
+ value = n.gsub(/[^0-9\.]/, '')
30
+ unit = /[^0-9\.]*$/.match(n)[0]
31
+
32
+ return [
33
+ Helpers.numerize(value),
34
+ unit
35
+ ]
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,24 @@
1
+ require_relative 'parser'
2
+
3
+ module AdjustmentBureau
4
+ Property = Struct.new(:name, :values) do
5
+ def self.parse(property_string)
6
+ Property.new(*Parser.new.parse(property_string))
7
+ end
8
+
9
+ def adjust(type, amount)
10
+ values.each do |value|
11
+ value.adjust(type, amount)
12
+ end
13
+ end
14
+
15
+ def to_s(padding=0)
16
+ output = []
17
+ output << (' ' * padding)
18
+ output << "#{name}: "
19
+ output << values.map(&:to_s).join(' ')
20
+ output << ';'
21
+ output.join('')
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,18 @@
1
+ require_relative 'helpers'
2
+
3
+ module AdjustmentBureau
4
+ Value = Struct.new(:value, :unit) do
5
+
6
+ def adjust(type, amount)
7
+ if not value.respond_to?(type)
8
+ raise ArgumentError.new("the value: #{value} cannot have function #{type} applied to it.")
9
+ end
10
+
11
+ self.value = Helpers.numerize(value.method(type).call(amount))
12
+ end
13
+
14
+ def to_s
15
+ "#{value}#{unit}"
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ module AdjustmentBureau
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+ require 'adjustment_bureau.rb'
3
+
4
+ describe AdjustmentBureau do
5
+ describe '.' do
6
+ context 'when ...' do
7
+ it 'does stuff' do
8
+ properties = [
9
+ 'top: 100px;',
10
+ 'left: 100px;',
11
+ 'width: 100px;',
12
+ 'height: 100px;'
13
+ ]
14
+
15
+ adjustable_properties = [:top, :left]
16
+ adjustment_operation = :+
17
+ adjustment_amount = 10
18
+
19
+ adjuster = AdjustmentBureau::Adjuster.new(
20
+ adjustable_properties,
21
+ adjustment_operation,
22
+ adjustment_amount)
23
+
24
+ properties.map! { |p| adjuster.adjust(p).to_s }
25
+
26
+ # expected output
27
+ expected = [
28
+ 'top: 110px;',
29
+ 'left: 110px;',
30
+ 'width: 100px;',
31
+ 'height: 100px;'
32
+ ]
33
+
34
+ expect(properties).to eq expected
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+ require 'adjustment_bureau/adjuster'
3
+
4
+ describe AdjustmentBureau::Adjuster do
5
+ let(:adjuster) {
6
+ AdjustmentBureau::Adjuster.new([:top, :left], :+, 10)
7
+ }
8
+
9
+ describe '#adjust' do
10
+ context 'when a property is adjustable' do
11
+ it 'returns a Property with adjusted values' do
12
+ property = adjuster.adjust('top: 100px;')
13
+ expect(property.to_s).to eq 'top: 110px;'
14
+ end
15
+
16
+ it 'handles floating point values' do
17
+ property = adjuster.adjust('top: 10.12px;')
18
+ expect(property.to_s).to eq 'top: 20.12px;'
19
+ end
20
+ end
21
+
22
+ context 'when a property is NOT adjustable' do
23
+ it 'returns a Property with un-adjusted values' do
24
+ property = adjuster.adjust('height: 100px;')
25
+ expect(property.to_s).to eq 'height: 100px;'
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,61 @@
1
+ require 'spec_helper'
2
+ require 'adjustment_bureau/helpers'
3
+
4
+ describe AdjustmentBureau::Helpers do
5
+ # let(:helpers) { Object.new.extend(AdjustmentBureau::Helpers) }
6
+ let(:helpers) { AdjustmentBureau::Helpers }
7
+
8
+ describe '#numeric?' do
9
+ it 'returns true for an integer' do
10
+ expect(helpers.numeric?(1)).to be_true
11
+ end
12
+
13
+ it 'returns true for a decimal' do
14
+ expect(helpers.numeric?(1.3)).to be_true
15
+ end
16
+
17
+ it 'returns true for a decimal with a leading zero' do
18
+ expect(helpers.numeric?(0.3)).to be_true
19
+ end
20
+
21
+ it 'returns true for a nmumeric string' do
22
+ expect(helpers.numeric?('0.3')).to be_true
23
+ end
24
+
25
+ it 'returns false for anything not a number' do
26
+ expect(helpers.numeric?('shazam')).to be_false
27
+ end
28
+ end
29
+
30
+ describe '#numerize' do
31
+ context 'when given an Integer' do
32
+ it 'returns an Integer' do
33
+ expect(helpers.numerize(1)).to eq 1
34
+ end
35
+ end
36
+
37
+ context 'when given an numeric String' do
38
+ it 'returns an Integer' do
39
+ expect(helpers.numerize('1.12')).to eq 1.12
40
+ end
41
+ end
42
+
43
+ context 'when given a decimal with no fractional part' do
44
+ it 'returns an Integer' do
45
+ expect(helpers.numerize('1.00')).to eq 1
46
+ end
47
+ end
48
+
49
+ context 'when given a decimal with a fractional part' do
50
+ it 'returns a Float' do
51
+ expect(helpers.numerize('1.23')).to eq 1.23
52
+ end
53
+ end
54
+
55
+ context 'value with manu decimal places' do
56
+ it 'rounds to decimal places' do
57
+ expect(helpers.numerize('1.23456789')).to eq 1.23
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+ require 'adjustment_bureau/parser'
3
+
4
+ describe AdjustmentBureau::Parser do
5
+ let(:parser) { AdjustmentBureau::Parser.new }
6
+
7
+ describe '.parse' do
8
+ it 'converts the parsed value into an Array' do
9
+ name, values = parser.parse('font-size: 10pt;')
10
+ expect(values).to be_a Array
11
+ end
12
+
13
+ it 'handles multiple values' do
14
+ name, values = parser.parse('margin: 1px 2px 3px 4px;')
15
+ expect(values).to be_a Array
16
+ end
17
+
18
+ it 'returns the name, value and unit in an array' do
19
+ name, values = parser.parse('font-size: 10pt;')
20
+ expect(name).to eq 'font-size'
21
+ expect(values.first.value).to eq 10
22
+ # expect(unit).to eq 'pt'
23
+ end
24
+
25
+ it 'handles floatin point numbers for values' do
26
+ name, values = parser.parse('font-size: 10.4pt;')
27
+ expect(values.first.value).to eq 10.4
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+ require 'adjustment_bureau/property'
3
+
4
+ describe AdjustmentBureau::Property do
5
+ let(:property) { AdjustmentBureau::Property.parse('font-size: 10pt;') }
6
+
7
+ describe '.parse' do
8
+ it 'parses the property string and sets the fields' do
9
+ expect(property.to_s).to eq 'font-size: 10pt;'
10
+ end
11
+ end
12
+
13
+ describe '#adjust' do
14
+ it 'adjusts multiple values' do
15
+ property = AdjustmentBureau::Property.parse('margin: 1px 2px 3px 4px;')
16
+ property.adjust(:+, 10)
17
+ expect(property.to_s).to eq 'margin: 11px 12px 13px 14px;'
18
+ end
19
+
20
+ it 'adjusts a single value' do
21
+ property.adjust(:*, 10)
22
+ expect(property.to_s).to eq 'font-size: 100pt;'
23
+ end
24
+ end
25
+
26
+ describe '#to_s' do
27
+ it 'returns a valid CSS property string' do
28
+ expect(property.to_s).to eq 'font-size: 10pt;'
29
+ end
30
+
31
+ it 'returns a valid CSS property string with multiple values' do
32
+ property = AdjustmentBureau::Property.parse('margin: 1px 2px 3px 4px;')
33
+ expect(property.to_s).to eq 'margin: 1px 2px 3px 4px;'
34
+ end
35
+
36
+ it 'can pad the beggining of the property string' do
37
+ expect(property.to_s(4)).to eq ' font-size: 10pt;'
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+ require 'adjustment_bureau/value'
3
+
4
+ describe AdjustmentBureau::Value do
5
+ let(:value) { AdjustmentBureau::Value.new(12, 'px') }
6
+
7
+ describe '#adjust' do
8
+ it 'applies the given operator to the value and amount' do
9
+ value.adjust(:+, 12)
10
+ expect(value.to_s).to eq '24px'
11
+ end
12
+
13
+ it 'handles floating point values' do
14
+ value.adjust(:+, 12.12)
15
+ expect(value.to_s).to eq '24.12px'
16
+ end
17
+
18
+ it 'rounds floating point numbers to two places' do
19
+ value.adjust(:+, 12.78888)
20
+ expect(value.to_s).to eq '24.79px'
21
+ end
22
+ end
23
+
24
+ describe '#to_s' do
25
+ it 'returns the value and unit as a string' do
26
+ expect(value.to_s).to eq '12px'
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,17 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+
12
+ # Run specs in random order to surface order dependencies. If you find an
13
+ # order dependency and want to debug it, you can fix the order by providing
14
+ # the seed, which is printed after each run.
15
+ # --seed 1234
16
+ config.order = 'random'
17
+ end
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: adjustment_bureau
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - bjh
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-04-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.5'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.5'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: guard-rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: Something I needed at work so I gemmed it up...
79
+ email:
80
+ - fake@fake.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - Gemfile
87
+ - LICENSE.txt
88
+ - README.md
89
+ - Rakefile
90
+ - adjustment_bureau.gemspec
91
+ - lib/adjustment_bureau.rb
92
+ - lib/adjustment_bureau/adjuster.rb
93
+ - lib/adjustment_bureau/helpers.rb
94
+ - lib/adjustment_bureau/parser.rb
95
+ - lib/adjustment_bureau/property.rb
96
+ - lib/adjustment_bureau/value.rb
97
+ - lib/adjustment_bureau/version.rb
98
+ - spec/adjustment_bureau_spec.rb
99
+ - spec/lib/adjuster_spec.rb
100
+ - spec/lib/helpers_spec.rb
101
+ - spec/lib/parser_spec.rb
102
+ - spec/lib/property_spec.rb
103
+ - spec/lib/value_spec.rb
104
+ - spec/spec_helper.rb
105
+ homepage: https://github.com/bjh/adjustment_bureau
106
+ licenses:
107
+ - MIT
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ segments:
119
+ - 0
120
+ hash: 792453711919969155
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ none: false
123
+ requirements:
124
+ - - ! '>='
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ segments:
128
+ - 0
129
+ hash: 792453711919969155
130
+ requirements: []
131
+ rubyforge_project:
132
+ rubygems_version: 1.8.23
133
+ signing_key:
134
+ specification_version: 3
135
+ summary: A CSS property adjuster
136
+ test_files:
137
+ - spec/adjustment_bureau_spec.rb
138
+ - spec/lib/adjuster_spec.rb
139
+ - spec/lib/helpers_spec.rb
140
+ - spec/lib/parser_spec.rb
141
+ - spec/lib/property_spec.rb
142
+ - spec/lib/value_spec.rb
143
+ - spec/spec_helper.rb