easy_attributes 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ module EasyAttributes
2
+ VERSION = "0.2.0"
3
+ end
metadata CHANGED
@@ -1,78 +1,101 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: easy_attributes
3
- version: !ruby/object:Gem::Version
4
- hash: 29
5
- prerelease: false
6
- segments:
7
- - 0
8
- - 1
9
- - 3
10
- version: 0.1.3
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
11
5
  platform: ruby
12
- authors:
6
+ authors:
13
7
  - Allen Fair
14
8
  autorequire:
15
- bindir: bin
9
+ bindir: exe
16
10
  cert_chain: []
17
-
18
- date: 2010-08-06 00:00:00 -04:00
19
- default_executable:
20
- dependencies: []
21
-
22
- description: Easy Attributes is a Ruby DSL to give more control to attributes.
23
- email: allen.fair@gmail.com
11
+ date: 2016-04-28 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: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description: Easy Attributes is a Ruby DSL to give more control to attributes. It
56
+ provides a unique attribute enum setup, and conversions to bytes and float-as-integer
57
+ (money, frequencies, Ratings, etc.) / fixed-decimal precision as integer (See the
58
+ easy_money gem).
59
+ email:
60
+ - allen.fair@gmail.com
24
61
  executables: []
25
-
26
62
  extensions: []
27
-
28
- extra_rdoc_files:
29
- - LICENSE
30
- - README.rdoc
31
- files:
32
- - .document
33
- - .gitignore
34
- - LICENSE
35
- - README.rdoc
63
+ extra_rdoc_files: []
64
+ files:
65
+ - ".document"
66
+ - ".gitignore"
67
+ - Gemfile
68
+ - README.md
36
69
  - Rakefile
37
- - VERSION
70
+ - bin/console
71
+ - bin/setup
38
72
  - easy_attributes.gemspec
73
+ - easy_attributes/.gitignore
74
+ - easy_attributes/.travis.yml
39
75
  - lib/easy_attributes.rb
40
- - test/helper.rb
41
- - test/test_easy_attributes.rb
42
- has_rdoc: true
43
- homepage: http://github.com/afair/easy_attributes
76
+ - lib/easy_attributes/version.rb
77
+ homepage: https://github.com/afair/easy_attributes
44
78
  licenses: []
45
-
79
+ metadata: {}
46
80
  post_install_message:
47
- rdoc_options:
48
- - --charset=UTF-8
49
- require_paths:
81
+ rdoc_options: []
82
+ require_paths:
50
83
  - lib
51
- required_ruby_version: !ruby/object:Gem::Requirement
52
- none: false
53
- requirements:
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
54
86
  - - ">="
55
- - !ruby/object:Gem::Version
56
- hash: 3
57
- segments:
58
- - 0
59
- version: "0"
60
- required_rubygems_version: !ruby/object:Gem::Requirement
61
- none: false
62
- requirements:
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
63
91
  - - ">="
64
- - !ruby/object:Gem::Version
65
- hash: 3
66
- segments:
67
- - 0
68
- version: "0"
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
69
94
  requirements: []
70
-
71
95
  rubyforge_project:
72
- rubygems_version: 1.3.7
96
+ rubygems_version: 2.5.1
73
97
  signing_key:
74
- specification_version: 3
75
- summary: Easy Attributes for Ruby
76
- test_files:
77
- - test/helper.rb
78
- - test/test_easy_attributes.rb
98
+ specification_version: 4
99
+ summary: 'Easy Attributes for Ruby: Enum, Bytes, Money, float-as-integer views and
100
+ forms'
101
+ test_files: []
data/LICENSE DELETED
@@ -1,20 +0,0 @@
1
- Copyright (c) 2009 Allen Fair
2
-
3
- Permission is hereby granted, free of charge, to any person obtaining
4
- a copy of this software and associated documentation files (the
5
- "Software"), to deal in the Software without restriction, including
6
- without limitation the rights to use, copy, modify, merge, publish,
7
- distribute, sublicense, and/or sell copies of the Software, and to
8
- permit persons to whom the Software is furnished to do so, subject to
9
- the following conditions:
10
-
11
- The above copyright notice and this permission notice shall be
12
- included in all copies or substantial portions of the Software.
13
-
14
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -1,156 +0,0 @@
1
- = easy_attributes
2
-
3
- Easy Attributes is a Ruby DSL to give more control to attributes. Tested with ruby 1.87 & 1.9.2
4
-
5
- == Usage
6
-
7
- Easy Attributes works in any Ruby class, it does not require Active Record or other ORM.
8
-
9
- First, install this from rubygems.org
10
- gem install easy_attributes
11
-
12
- Next, you need to configure your app to use it, by either:
13
- require 'rubygems' # Ruby application, no framework
14
- require 'easy_attributes' #
15
-
16
- config.gem 'easy_attributes' # Rails 2.x, in your ./config/environment.rb
17
-
18
- gem 'easy_attributes' # Rails 3.x, in your ./Gemfile
19
-
20
- Configuration:
21
- EasyAttributes::Config.orm = :active_model # will generate some validations, etc.
22
- EasyAttributes::Config.load(filename) # Loads a file of column/value/names/descriptions
23
- EasyAttributes::define(attribute_name, {:name=>value, ...})
24
-
25
- Declaration:
26
-
27
- class Record < ObjectRelationalMapperOfChoice
28
- include EasyAttributes
29
-
30
- attr_values :field, :name=>'value', :name=>'value', ... :validate=>:active_record, :required=>true, :like=>'other' ...
31
- attr_sequence :field, :name, :name, ... ,:start=>1, :step=>1
32
- attr_money :amount, :units=>"dollars", :unit=>'$', :negative=>'%.2f CR'
33
- attr_bytes :bandwidth
34
- end
35
-
36
- == attr_values
37
-
38
- will define:
39
- field() # => value ### if orm == :attr, attr_accessor will be installed for the attribute
40
- field=(value) # => value ### for active_model classes
41
- field_sym() # => :name
42
- field_sym=(symbol) # => value
43
- field_values() # => {:name=>value, ...}
44
- field_is?(:name) # true if :name matches current value
45
- field_is?(:name, :name, ...) # true if value matches a given symbol
46
- field_is?(:greater_than, :name) # true if the value is greater than the value of :name
47
- The first argument can be one of:
48
- :greater_than, :gt, :greater_than_or_equal, :ge, :less_than, :lt, :less_than_or_equal, :le
49
- field_is?(:between, :name1, :name2) # true if the value is between the corresponding values
50
-
51
- == attr_sequence
52
-
53
- sets up a attr_values with the sequential numbering, by default starting at 1 incrementing by 1
54
-
55
- == attr_bytes
56
-
57
- Creates helper fields to display and accept byte quantities as KB, MB, GB, TB, etc. Since a kilobyte (KB) is
58
- now defined as 1000 bytes, and a kibibyte (KiB) is the 1024 quantity, you wan to configure which you want to use
59
-
60
- EasyAttributes::Config.kb_size = 1000 # Uses KB units as 1KB = 1000 bytes
61
- EasyAttributes::Config.kb_size = 1024 # Uses KB units as 1KB = 1024 bytes
62
- EasyAttributes::Config.kb_size = :new # Uses KiB units as 1KiB = 1024 bytes (DEFAULT)
63
- EasyAttributes::Config.kb_size = :both # Combines usage of KB (1000) and KiB (1024) units (Watch out!)
64
-
65
- attr_bytes creates the following methods for a :bandwidth attribute
66
-
67
- bandwidth_bytes = "1 KB" # => 1024
68
- bandwidth_bytes # => "1 KB" using whatever unit is best
69
- bandwidth_bytes(:MiB) # => "12345.67 MB" using the specified unit
70
- bandwidth_bytes(:MiB, :precision=>0) # => "123 MB"
71
-
72
- == attr_money
73
-
74
- is the inclusion of my easy_money gem
75
-
76
- Mixin the EasyAttributes module into the (model) class you need, and declare the
77
- attributes (columns) you wish you have the attr_money helpers
78
-
79
- class Ledger
80
- include EasyAttributes
81
- attr_accessor :amount, :euro # Integer value of cents
82
- attr_money :amount # Creates amount_money() and amount_money=() methods
83
- attr_money :amount, :units=>"dollars", :unit=>'$', :negative=>'%.2f CR'
84
- # Creates amount_dollars() and amount_dollars=() methods
85
- end
86
-
87
- ledger = Ledger.new
88
- ledger.amount = 100 # 100 cents = $1.00
89
- ledger.amount_money #=> "1.00"
90
- ledger.amount_money = "-123.45"
91
- ledger.amount #=> -12345 (cents)
92
- ledger.amount_money(:negative=>'%.2f CR', :zero=>'Free') # Uses these formats
93
- ledger.amount_dollars #=> "$123.45 CR"
94
-
95
- # Track the bets of the Gamesters of Triskelion on their drill thrall competitions.
96
- class ProviderWagers < ActiveRecord::Base
97
- include EasyAttributes
98
- attr_money :quatloos, :units=>'quatloos', :precision=>3,
99
- :zero=>'even', :nil=>'no bet', :negative=>'%.3f Loss', :unit=>'Q',
100
- :negative_regex=>/^(-?)(.+\d)\s*Loss/i
101
- # creates amount_quatloos(), amount_quatloos=()
102
- end
103
-
104
- # in your views
105
- <%= f.text_field :amount_quatloos %> # -12000 => "Q12.000 Loss"
106
-
107
- Options for attr_money calls:
108
- * :money_method - Use this as the alternative name to the money-access methods
109
- * :units - Use this as an alternative suffix name to the money methods ('dollars' gives 'xx_dollars')
110
- * :precision - The number of digits implied after the decimal, default is 2
111
- * :separator - The character to use after the integer part, default is '.'
112
- * :delimiter - The character to use between every 3 digits of the integer part, default none
113
- * :positive - The sprintf format to use for positive numbers, default is based on precision
114
- * :negative - The sprintf format to use for negative numbers, default is same as :positive
115
- * :zero - The sprintf format to use for zero, default is same as :positive
116
- * :nil - The sprintf format to use for nil values, default none
117
- * :unit - Prepend this to the front of the money value, say '$', default none
118
- * :blank - Return this value when the money string is empty or has no digits on assignment
119
- * :negative_regex - A Regular Expression used to determine if a number is negative (and without a - sign), defaults to having a "CR" after the number
120
-
121
- === Formatters
122
- You can also call or build your own custom conversions. Ensure that
123
- you can convert between the integer and money forms if you need to.
124
-
125
- The "money" type is a string, suitable for human editing, and will convert back into
126
- integer type. If you override the formatting options, test that your money result
127
- string will convert properly back to the original integer value.
128
-
129
- include EasyAttributes
130
- ...
131
- puts EasyAttributes.money_to_integer( money_string, :option=>value, ... )
132
- puts EasyAttributes.integer_to_money( cents_integer, :option=>value, ... )
133
- puts EasyAttributes.integer_to_float( cents_integer, :option=>value, ... )
134
- puts EasyAttributes.float_to_integer( money_float, :option=>value, ... )
135
-
136
- EasyAttributes.integer_to_float( nil, blank:0 ) #=> 0.0 [Ruby 1.9.1 Syntax]
137
- EasyAttributes.integer_to_float( 12345, :precision=>3 ) #=> 12.345
138
- EasyAttributes.float_to_integer(12.345111, :precision=>3 ) #=> 12345
139
-
140
- The options to these methods are the same as the #attr_money declarations
141
-
142
-
143
- == Note on Patches/Pull Requests
144
-
145
- * Fork the project.
146
- * Make your feature addition or bug fix.
147
- * Add tests for it. This is important so I don't break it in a
148
- future version unintentionally.
149
- * Commit, do not mess with rakefile, version, or history.
150
- (if you want to have your own version, that is fine but
151
- bump version in a commit by itself I can ignore when I pull)
152
- * Send me a pull request. Bonus points for topic branches.
153
-
154
- == Copyright
155
-
156
- Copyright (c) 2010 Allen Fair. See LICENSE for details.
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 0.1.3
@@ -1,19 +0,0 @@
1
- require 'rubygems'
2
- require 'test/unit'
3
-
4
- $LOAD_PATH.unshift(File.dirname(__FILE__))
5
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
- require 'easy_attributes'
7
-
8
- class Test::Unit::TestCase
9
- end
10
- require 'rubygems'
11
- require 'test/unit'
12
-
13
-
14
- class Sample
15
- include EasyAttributes
16
- attr_accessor :price, :balance
17
- attr_money :price
18
- attr_money :balance
19
- end
@@ -1,151 +0,0 @@
1
- require 'helper'
2
- EasyAttributes::Config.orm = :attr
3
-
4
- class TestEasyAttributes < Test::Unit::TestCase
5
- include EasyAttributes
6
- attr_sequence :tas, :n1, :n2
7
- attr_values :tav, :k1=>1, :k2=>2, :k3=>3
8
- attr_values :status, {}, :like=>'TestEasyAttributes#tav'
9
- attr_bytes :bw
10
- attr_money :amount
11
-
12
- def test_attr_sequence
13
- self.tas_sym = :n1
14
- assert_equal self.tas, 1
15
- assert_equal self.tas_sym, :n1
16
- end
17
-
18
- def test_attr_values
19
- self.tav_sym = :k1
20
- assert_equal tav, 1
21
- self.tav_sym = :k2
22
- assert_equal tav, 2
23
- assert_equal tav_sym, :k2
24
- assert_equal tav_is?(:k2), true
25
- assert_equal tav_is?(:k1, :k3), false
26
- #self.tav = :k1
27
- #assert_equal tav, 1
28
- end
29
-
30
- def test_like
31
- self.status_sym = :k1
32
- assert_equal self.status, 1
33
- end
34
-
35
- # Removed for now, not shipping my data file!
36
- def test_load
37
- #EasyAttributes::Config.load "values"
38
- #assert_equal EasyAttributes::Config.attributes['status'][:ok], 8
39
- end
40
-
41
- def test_attr_bytes
42
- self.bw = 1024
43
- assert_equal bw, 1024
44
- assert_equal bw_bytes(:KiB, :precision=>0), "1 KiB"
45
- self.bw = [1, :kb]
46
- end
47
-
48
- def test_format_bytes
49
- EasyAttributes::Config.kb_size = :both
50
- assert_equal EasyAttributes.format_bytes( 900 ), "900 B"
51
- assert_equal EasyAttributes.format_bytes( 1000 ), "1 KB"
52
- assert_equal EasyAttributes.format_bytes( 12345 ), "12 KiB"
53
- assert_equal EasyAttributes.format_bytes( 123456789 ), "117 MiB"
54
- assert_equal EasyAttributes.format_bytes( 9999999999 ), "9.31 GiB"
55
- assert_equal EasyAttributes.format_bytes( 123456789, :KiB ), "120563.271 KiB"
56
- assert_equal EasyAttributes.format_bytes( 123456789, :KiB, 1 ), "120563.3 KiB"
57
- assert_equal EasyAttributes.format_bytes( 123456789, :KiB, 0 ), "120563 KiB"
58
- end
59
-
60
- def test_parse_bytes
61
- EasyAttributes::Config.kb_size=:both
62
- assert_equal EasyAttributes.parse_bytes( "1.5 KiB" ), 1536
63
- assert_equal EasyAttributes.parse_bytes( "1 gb" ), EasyAttributes::GB
64
- assert_equal EasyAttributes.parse_bytes( "1kb", :kb_size=>1000 ), 1000
65
- assert_equal EasyAttributes.parse_bytes( "1kb", :kb_size=>1024 ), 1024
66
- end
67
-
68
- def test_include
69
- sample = Sample.new
70
- flunk "no price_money method" unless sample.respond_to?(:price_money)
71
- flunk "no price_money= method" unless sample.respond_to?(:price_money=)
72
- end
73
-
74
- def test_method_money
75
- s = Sample.new
76
- [ 10000, 123456, 0, -1 -9876 ].each do |p|
77
- s.price = p
78
- m = s.price_money
79
- s.price_money = m
80
- flunk "Assignment error: p=#{p} m=#{m} price=#{s.price}" unless s.price = p
81
- end
82
- end
83
-
84
- def test_method_money=
85
- s = Sample.new
86
- { "0.00"=>0, "12.34"=>1234, "-1.2345"=>-123, "12"=>1200, "4.56CR"=>-456 }.each do |m,p|
87
- s.price_money = m
88
- flunk "Assignment error: p=#{p} m=#{m} price=#{s.price}" unless s.price = p
89
- end
90
- end
91
-
92
- def test_integer_to_money
93
- assert EasyAttributes.integer_to_money(123) == '1.23'
94
- assert EasyAttributes.integer_to_money(-12333) == '-123.33'
95
- assert EasyAttributes.integer_to_money(0) == '0.00'
96
- assert EasyAttributes.integer_to_money(nil, :nil=>'?') == '?'
97
- assert EasyAttributes.integer_to_money(-1, :negative=>'%.2f CR') == '0.01 CR'
98
- assert EasyAttributes.integer_to_money(0, :zero=>'free') == 'free'
99
- assert EasyAttributes.integer_to_money(100, :unit=>'$') == '$1.00'
100
- assert EasyAttributes.integer_to_money(100, :separator=>',') == '1,00'
101
- assert EasyAttributes.integer_to_money(12345678900, :separator=>',', :delimiter=>'.') == '123.456.789,00'
102
- assert EasyAttributes.integer_to_money(333, :precision=>3) == '0.333'
103
- assert EasyAttributes.integer_to_money(111, :precision=>1) == '11.1'
104
- assert EasyAttributes.integer_to_money(111, :precision=>0) == '111'
105
- end
106
-
107
- def test_money_to_integer
108
- assert EasyAttributes.money_to_integer('1.23' ) == 123
109
- assert EasyAttributes.money_to_integer('0.00' ) == 0
110
- assert EasyAttributes.money_to_integer('-1.23' ) == -123
111
- assert EasyAttributes.money_to_integer('1.23 CR' ) == -123
112
- assert EasyAttributes.money_to_integer('$-2.34 CR' ) == 234
113
- assert EasyAttributes.money_to_integer(' 1.234' ) == 123
114
- assert EasyAttributes.money_to_integer('$1' ) == 100
115
- assert EasyAttributes.money_to_integer('1' ) == 100
116
- assert EasyAttributes.money_to_integer('' ) == nil
117
- assert EasyAttributes.money_to_integer('1,00', :separator=>',',:delimiter=>'.') == 100
118
- assert EasyAttributes.money_to_integer('$123.456.789,00 CR', :separator=>',',:delimiter=>'.') == -12345678900
119
- assert EasyAttributes.money_to_integer('4.44', :precision=>4 ) == 44400
120
- assert EasyAttributes.money_to_integer('4.44', :precision=>0 ) == 4
121
- end
122
-
123
- def test_float_to_integer
124
- assert EasyAttributes.float_to_integer(1.00 ) == 100
125
- assert EasyAttributes.float_to_integer(1.001 ) == 100
126
- assert EasyAttributes.float_to_integer(-1.23 ) == -123
127
- assert EasyAttributes.float_to_integer(9.0 ) == 900
128
- assert EasyAttributes.float_to_integer(nil ) == nil
129
- assert EasyAttributes.float_to_integer(0.00 ) == 0
130
- end
131
-
132
- def test_integer_to_float
133
- assert EasyAttributes.integer_to_float(1 ) == 0.01
134
- assert EasyAttributes.integer_to_float(0 ) == 0.0
135
- assert EasyAttributes.integer_to_float(-100 ) == -1.00
136
- assert EasyAttributes.integer_to_float(nil ) == nil
137
- assert EasyAttributes.integer_to_float(9999888, :precision=>3 ) == 9999.888
138
- end
139
-
140
- def test_format_money
141
- assert EasyAttributes.format_money(12345) == '123.45'
142
- assert EasyAttributes.format_money(12345, "%07.2m") == '0000123.45'
143
- assert EasyAttributes.format_money(12345, "%07.3m") == '0000123.450'
144
- assert EasyAttributes.format_money(12345, "%m") == '123'
145
- assert EasyAttributes.format_money(12345, "free") == 'free'
146
- assert EasyAttributes.format_money(-12345) == '-123.45'
147
- assert EasyAttributes.format_money(-12345, "%07.1m") == '-000123.4'
148
- assert EasyAttributes.format_money(-1) == '-0.01'
149
- end
150
-
151
- end