Skip to content

Commit

Permalink
Merge pull request #65 from lucascaton/sort_by-none-by-default
Browse files Browse the repository at this point in the history
Does not sort by default
  • Loading branch information
lucascaton authored Oct 19, 2016
2 parents 94cc567 + 1a57479 commit 923f323
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 35 deletions.
12 changes: 12 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,15 @@ Style/MultilineMethodCallIndentation:
Style/PredicateName:
Exclude:
- 'lib/enumerate_it/class_methods.rb'

Style/GuardClause:
MinBodyLength: 3

RSpec/MultipleExpectations:
Enabled: false

RSpec/NestedGroups:
Enabled: false

RSpec/MessageExpectation:
Enabled: false
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
enumerate_it (1.3.1)
enumerate_it (1.4.0.rc1)
activesupport (>= 3.0.0)

GEM
Expand Down Expand Up @@ -45,7 +45,7 @@ GEM
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.5.0)
rspec-support (3.5.0)
rubocop (0.43.0)
rubocop (0.44.1)
parser (>= 2.3.1.1, < 3.0)
powerpack (~> 0.1)
rainbow (>= 1.99.1, < 3.0)
Expand Down
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,17 +163,17 @@ mode, you can use the `sort_by` class method.
class RelationshipStatus < EnumerateIt::Base
associate_values married: 1, single: 2

sort_by :name
sort_by :translation
end
```

The `sort_by` methods accept one of the following values:

| Value | Behavior |
| :------------- | :------------------------------------------------------------------------- |
| `:translation` | The default behavior, will sort the returned values based on translations |
| `:name` | Will sort the returned values based on the name of each enumeration option |
| `:none` | Will return values in order that was passed to `associate_values` call |
| Value | Behavior |
| :------------- | :------------------------------------------------------------------------------------------- |
| `:none` | The default behavior, will return values in order that was passed to `associate_values` call |
| `:translation` | will sort the returned values based on translations |
| `:name` | Will sort the returned values based on the name of each enumeration option |

## Using enumerations

Expand Down Expand Up @@ -498,11 +498,11 @@ Changes are maintained under [Releases](https://github.com/lucascaton/enumerate_

* Fork the project.
* Make your feature addition or bug fix.
* Add tests for it. This is important so I don't break it in a future version unintentionally.
* Add tests for it. This is important so we don't break it in a future version unintentionally.
* [Optional] Run the tests agaist a specific Gemfile: `$ appraisal rails_5.0 rake spec`.
* Run the tests agaist all supported versions: `$ rake`.
* Commit, do not mess with Rakefile, version, or history. (if you want to have your own version,
that is fine but bump version in a commit by itself I can ignore when I pull)
* Send me a pull request. Bonus points for topic branches.
* Commit, but please do not mess with `Rakefile`, version, or history.
* Send a Pull Request. Bonus points for topic branches.

## Copyright

Expand Down
4 changes: 2 additions & 2 deletions lib/enumerate_it/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def translate(value)
private

def sorted_map
return enumeration if sort_mode == :none
return enumeration if sort_mode.nil? || sort_mode == :none

enumeration.sort_by { |k, v| sort_lambda.call(k, v) }
end
Expand All @@ -103,7 +103,7 @@ def sort_lambda
value: ->(_k, v) { v[0] },
name: ->(k, _v) { k },
translation: ->(_k, v) { translate(v[1]) }
}[sort_mode || :translation]
}[sort_mode]
end

def normalize_enumeration(values_hash)
Expand Down
2 changes: 1 addition & 1 deletion lib/enumerate_it/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module EnumerateIt
VERSION = '1.3.1'.freeze
VERSION = '1.4.0.rc1'.freeze
end
12 changes: 10 additions & 2 deletions spec/enumerate_it/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@

context 'associate values with a list' do
it 'creates constants for each enumeration value' do
expect(TestEnumerationWithList::FIRST).to eq('first')
expect(TestEnumerationWithList::FIRST).to eq('first')
expect(TestEnumerationWithList::SECOND).to eq('second')
end

Expand All @@ -207,7 +207,15 @@
end
end

context 'specifying a default sort mode' do
context 'not specifying a sort mode' do
subject { create_enumeration_class_with_sort_mode(nil).to_a }

it 'does not sort' do
is_expected.to eq([%w(xyz 1), %w(fgh 2), %w(abc 3), %w(jkl 0)])
end
end

context 'specifying a sort mode' do
subject { create_enumeration_class_with_sort_mode(sort_mode).to_a }

context 'by value' do
Expand Down
26 changes: 8 additions & 18 deletions spec/support/test_classes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,12 @@ class TestEnumeration < EnumerateIt::Base
end

class TestEnumerationWithoutArray < EnumerateIt::Base
associate_values(
value_one: '1',
value_two: '2'
)
associate_values value_one: '1', value_two: '2'
end

class TestEnumerationWithExtendedBehaviour < EnumerateIt::Base
associate_values(
first: '1',
second: '2'
)
associate_values first: '1', second: '2'

def self.to_a
super.reverse
end
Expand All @@ -32,21 +27,15 @@ class TestEnumerationWithReservedWords < EnumerateIt::Base
end

class TestEnumerationWithDash < EnumerateIt::Base
associate_values(
'pt-BR'
)
associate_values 'pt-BR'
end

class TestEnumerationWithCamelCase < EnumerateIt::Base
associate_values(
'iPhone'
)
associate_values 'iPhone'
end

class Foobar < EnumerateIt::Base
associate_values(
bar: 'foo'
)
associate_values bar: 'foo'
end

class PolymorphicEnum < EnumerateIt::Base
Expand All @@ -67,12 +56,13 @@ def print(msg)

class BaseClass
extend EnumerateIt

has_enumeration_for :foobar, with: TestEnumeration
end

def create_enumeration_class_with_sort_mode(sort_mode)
Class.new(EnumerateIt::Base) do
sort_by sort_mode
sort_by(sort_mode)

associate_values(
foo: %w(1 xyz),
Expand Down

0 comments on commit 923f323

Please sign in to comment.