cards_lib 0.0.9 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 10716ea47a94f0d478b4503e7151f99e82f41dbb
4
- data.tar.gz: 541e1726e3db0891a5de3c4bb581a2a097c706fe
3
+ metadata.gz: 9f94c306a1f03188d1d020e3e24a79222b46defe
4
+ data.tar.gz: e78c436d453c9f0a550c33545eafbe6abb598438
5
5
  SHA512:
6
- metadata.gz: d32136123ac2e81a3b2a86398782ab4d0214d2fb2d953eaa779ac5af82151846a024f33bcb98921cd83b96d1852cbb6254467d4c6b5d141a901587757fbc8dd2
7
- data.tar.gz: cac6d824f9502c16c5c82843c018396813ae5f99a2e64e44815e6bfe2550b2172834e564b6c8487b74b96b316c69e1232b66ccefd4028ff1bb8b482677dc00a6
6
+ metadata.gz: d82a1aabe22e731af5d690ded716a8e186e335b8716bde7999a870096b7137f4f41f3644946d113a6f692b8ab774b96e950c7e7fe99aab2c7a78ebf36b173c52
7
+ data.tar.gz: 71d6a2c5bdb8f1b8c678c8f30e6c6910f5769539bd4bd70cdf91d5cc0b3b7d6e83ff471c8a81fbfd425fe02a69bbb710f9db7e0ecc0fc28d41d39ebe3106d7bd
data/.gitignore CHANGED
@@ -11,3 +11,4 @@
11
11
  /pkg/
12
12
  /spec/reports/
13
13
  /tmp/
14
+ travis.log
data/README.md CHANGED
@@ -2,4 +2,53 @@
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/cards_lib.svg)](http://badge.fury.io/rb/cards_lib)[![Build Status](https://travis-ci.org/danielpclark/CardsLib.svg?branch=master)](https://travis-ci.org/danielpclark/CardsLib)[![Code Climate](https://codeclimate.com/github/danielpclark/CardsLib/badges/gpa.svg)](https://codeclimate.com/github/danielpclark/CardsLib)[![Test Coverage](https://codeclimate.com/github/danielpclark/CardsLib/badges/coverage.svg)](https://codeclimate.com/github/danielpclark/CardsLib/coverage)
4
4
 
5
- gem 'cards_lib'
5
+ ##Install
6
+
7
+ ```
8
+ gem install 'cards_lib'
9
+ ```
10
+ Requires Ruby 2 or greater.
11
+
12
+ ##Usage
13
+
14
+ Create a Card with Card.new(face)
15
+
16
+ ```ruby
17
+ CardsLib::Card.new("As")
18
+ ```
19
+
20
+ Use a Macro to create a list of Card instances from card faces.
21
+
22
+ ```ruby
23
+ CardsLib::Cards["Ah","2h","3h","4h","5h"]
24
+ ```
25
+
26
+ Create a Deck with Deck.new(cards)
27
+ A default deck is generated if no parameters are given.
28
+
29
+ ```ruby
30
+ CardsLib::Deck.new
31
+ ```
32
+
33
+ Pick what rules you'd like to use in determining a set of cards.
34
+
35
+ ```ruby
36
+ # BOOLEAN RESULT
37
+ CardsLib::IsSet.verify(
38
+ card_instances_array,
39
+ rules = [:unique, :ordered, :paired, :suited],
40
+ specifications = {min: 3, max: Float::INFINITY}
41
+ )
42
+ ```
43
+
44
+ ##License
45
+
46
+ The MIT License (MIT)
47
+
48
+ Copyright (c) 2015 by Daniel P. Clark
49
+
50
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
51
+
52
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
53
+
54
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -2,23 +2,12 @@ module CardsLib
2
2
  class Card
3
3
  include Comparable
4
4
  def initialize(face, ranker = Ranker)
5
- raise InvalidCardFace, "face cannot be blank" if face.to_s.empty?
6
- @suit = nil
7
- @rank = nil
8
- @ranker = ranker
9
- if face.is_a? Hash
10
- @suit = face.fetch(:suit) { nil }
11
- @rank = face.fetch(:rank) { nil }
12
- if @rank && @suit
13
- str = ""
14
- if @rank.length.>(1) && @suit.length.>(1)
15
- str = " of "
16
- end
17
- @face = [@rank, str, @suit].join
18
- end
19
- else
20
- @face = face
21
- end
5
+ raise InvalidCardFace, "Parameter face cannot be blank!" if face.to_s.empty?
6
+ @suit = if_hash_then_fetch(face, :suit)
7
+ @rank = if_hash_then_fetch(face, :rank)
8
+ @face = face_from_rank_and_suit(@rank, @suit) if @rank && @suit
9
+
10
+ @face ||= face
22
11
  @ranker = ranker.new(rank)
23
12
  end
24
13
 
@@ -46,33 +35,39 @@ module CardsLib
46
35
  @ranker.<=>(other)
47
36
  end
48
37
 
49
- def sequential(other)
50
- @ranker.sequential(other)
38
+ def sequential?(other)
39
+ @ranker.sequential?(other)
51
40
  end
52
41
 
53
42
  # return other if true
54
43
  def paired?(other)
55
- (self.rank == other.rank) ? other : NilCard.new
44
+ (self.rank == other.rank) ? other : nil
56
45
  end
57
46
 
58
47
  # return other if true
59
48
  def suited?(other)
60
- (self.suit == other.suit) ? other : NilCard.new
49
+ (self.suit == other.suit) ? other : nil
61
50
  end
62
51
 
63
52
  # returns other if true
64
53
  def ordered?(other)
65
- self.sequential(other) ? other : NilCard.new
54
+ self.sequential?(other) ? other : nil
66
55
  end
67
56
  end
68
57
 
69
- class InvalidCardFace < Exception
58
+ class InvalidCardFace < Exception; end
59
+ class InvalidRankAndSuitProvided < Exception; end
70
60
 
61
+ private
62
+ def face_from_rank_and_suit(rank, suit)
63
+ if rank && suit
64
+ [rank, ((rank.length.>(1) && suit.length.>(1)) ? " of " : ""), suit].join
65
+ else
66
+ raise InvalidRankAndSuitProvided, "Suit and Rank provided in Hash are invalid!"
67
+ end
71
68
  end
72
69
 
73
- class NilCard
74
- def method_missing(m,*a,&b)
75
- self
76
- end
70
+ def if_hash_then_fetch(item, target)
71
+ item.is_a?(Hash) ? item.fetch(target) { nil } : nil
77
72
  end
78
73
  end
@@ -2,9 +2,9 @@ module CardsLib
2
2
  module IsSet
3
3
  class << self
4
4
  def verify(cards, rules = [:unique, :paired], specs = {})
5
- @max = specs.fetch(:max) {Float::INFINITY}
6
- @min = specs.fetch(:min) { 3 }
7
- rules.all? {|r| send(r, cards) } && (@min..@max).include?(cards.count)
5
+ max = specs.fetch(:max) {Float::INFINITY}
6
+ min = specs.fetch(:min) { 3 }
7
+ rules.all? {|r| send(r, cards) } && (min..max).include?(cards.count)
8
8
  end
9
9
 
10
10
  private
@@ -12,16 +12,18 @@ module CardsLib
12
12
  cards.combination(2).all? {|a,b| a != b }
13
13
  end
14
14
 
15
+ using Refinements::InjectWhile
16
+
15
17
  def paired(cards)
16
- !(cards.inject(:paired?).is_a? NilCard)
18
+ cards.inject_while?(:paired?)
17
19
  end
18
20
 
19
21
  def suited(cards)
20
- !(cards.inject(:suited?).is_a? NilCard)
22
+ cards.inject_while?(:suited?)
21
23
  end
22
24
 
23
25
  def ordered(cards)
24
- !(cards.sort.inject(:ordered?).is_a? NilCard)
26
+ cards.sort.inject_while?(:ordered?)
25
27
  end
26
28
  end
27
29
  end
@@ -2,26 +2,19 @@ module CardsLib
2
2
  class Ranker
3
3
  include Comparable
4
4
  attr :rank, :ranks, :rank_lookup
5
- def initialize(
6
- rank = nil,
7
- ranks: "A23456789TJQK".split(''),
8
- rank_lookup: ->rank_face{
9
- @ranks.index(rank_face).to_i + 1
10
- })
11
- @rank = rank
12
- @ranks = ranks
13
- @rank_lookup = rank_lookup
5
+ def initialize rank = nil,
6
+ ranks: "A23456789TJQK".chars,
7
+ rank_lookup: nil
8
+
9
+ @rank, @ranks, @rank_lookup = rank, ranks, rank_lookup ||
10
+ ->rank_face{ @ranks.index(rank_face).to_i + 1 }
14
11
  end
15
12
 
16
13
  def <=>(item)
17
14
  @rank_lookup[self.rank] <=> @rank_lookup[item.rank]
18
15
  end
19
16
 
20
- def inspect
21
- @rank
22
- end
23
-
24
- def sequential(item)
17
+ def sequential?(item)
25
18
  (@rank_lookup[self.rank] - @rank_lookup[item.rank]).abs == 1
26
19
  end
27
20
  end
@@ -0,0 +1,18 @@
1
+ module CardsLib::Refinements
2
+ module InjectWhile
3
+ refine Array do
4
+ def inject_while?(m)
5
+ result, *array = self.dup
6
+ loop do
7
+ break if array.empty?
8
+ other = array.shift
9
+
10
+ return false if result.class != other.class
11
+
12
+ result = result.send(m, other)
13
+ end
14
+ true
15
+ end
16
+ end
17
+ end
18
+ end
@@ -1,3 +1,3 @@
1
1
  module CardsLib
2
- VERSION = "0.0.9"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/cards_lib.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "cards_lib/version"
2
+ require "cards_lib/refinements" # REQUIRE BEFORE USE
2
3
  require "cards_lib/card"
3
4
  require "cards_lib/deck"
4
5
  require "cards_lib/standard"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cards_lib
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel P. Clark
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-11-27 00:00:00.000000000 Z
11
+ date: 2015-11-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -103,9 +103,9 @@ files:
103
103
  - lib/cards_lib/deck.rb
104
104
  - lib/cards_lib/is_set.rb
105
105
  - lib/cards_lib/ranker.rb
106
+ - lib/cards_lib/refinements.rb
106
107
  - lib/cards_lib/standard.rb
107
108
  - lib/cards_lib/version.rb
108
- - travis.log
109
109
  homepage: http://github.com/danielpclark/CardsLib
110
110
  licenses:
111
111
  - MIT
data/travis.log DELETED
@@ -1,87 +0,0 @@
1
- Command line:
2
- "travis encrypt CODECLIMATE_REPO_TOKEN=ecf8c246bcc2fdad42590f78c7b214bc19abf212eee0979427477e9c6a510320 bundle exec rake"
3
-
4
-
5
- ________ __
6
- / | / |
7
- ########/ ______ ______ __ __ ##/ _______
8
- ## | / \ / \ / \ / | / | / |
9
- ## | /###### | ###### | ## \ /##/ ## | /#######/
10
- ## | ## | ##/ / ## | ## /##/ ## | ## \
11
- ## | ## | /####### | ## ##/ ## | ###### |
12
- ## | ## | ## ## | ###/ ## | / ##/
13
- ##/ ##/ #######/ #/ ##/ #######/
14
-
15
- TRajectory Analyzer and VISualizer - Open-source freeware under GNU GPL v3
16
-
17
- Copyright (c) Martin Brehm (2009-2014)
18
- Martin Thomas (2012-2014)
19
- Barbara Kirchner (2009-2014)
20
- University of Leipzig / University of Bonn.
21
-
22
- http://www.travis-analyzer.de
23
-
24
- Please cite:
25
- M. Brehm and B. Kirchner, J. Chem. Inf. Model. 2011, 51 (8), pp 2007-2023.
26
-
27
- There is absolutely no warranty on any results obtained from TRAVIS.
28
-
29
- # Running on allyourdev at Thu Jun 18 13:40:20 2015 (PID 16448).
30
- # Running in /home/danielpclark/dev/CardsLib
31
- # Source code version: Jan 17 2014.
32
- # Compiled at Jan 19 2014 05:12:11.
33
- # Compiler version: 4.8.2
34
- # Target platform: Linux
35
- # Compile flags: DEBUG_ARRAYS
36
- # Machine: int=4b, long=8b, addr=8b, 0xA0B0C0D0=D0,C0,B0,A0.
37
- # User home: /home/danielpclark
38
- # Exe path: /usr/bin/travis
39
- # Input from terminal, Output to terminal
40
-
41
- >>> Please use a color scheme with dark background or specify "-nocolor"! <<<
42
-
43
- No configuration file found.
44
- Writing default configuration to /home/danielpclark/.travis.conf ...
45
-
46
- Unknown parameter: "encrypt".
47
-
48
- List of supported command line options:
49
-
50
- -p <file> Loads position data from the specified trajectory file.
51
- The file format may be *.xyz, *.pdb, *.lmp (Lammps) or HISTORY (DLPOLY).
52
- -i <file> Reads input from the specified text file.
53
-
54
- -config <file> Load the specified configuration file.
55
- -stream Treats input trajectory as a stream (e.g. named pipe): No fseek, etc.
56
- -showconf Shows a tree structure of the configuration file.
57
- -writeconf Writes the default configuration file, including all defines values.
58
-
59
- -verbose Show detailed information about what's going on.
60
- -nocolor Executes TRAVIS in monochrome mode.
61
- -dimcolor Uses dim instead of bright colors.
62
-
63
- -credits Display a list of persons which contributed to TRAVIS.
64
- -help, -? Shows this help.
65
-
66
- If only one argument is specified, it is assumed to be the name of a trajectory file.
67
- If argument is specified at all, TRAVIS asks for the trajectory file to open.
68
-
69
-
70
- Note: To show a list of all persons which contributed to TRAVIS,
71
- please add "-credits" to your command line arguments, or set the
72
- variable "SHOWCREDITS" to "TRUE" in your travis.conf file.
73
-
74
- Source code from other projects used in TRAVIS:
75
- - lmfit from Joachim Wuttke
76
- - kiss_fft from Mark Borgerding
77
- - voro++ from Chris Rycroft
78
-
79
- http://www.travis-analyzer.de
80
-
81
- Please cite:
82
-
83
- * "TRAVIS - A Free Analyzer and Visualizer for Monte Carlo and Molecular Dynamics Trajectories",
84
- M. Brehm, B. Kirchner; J. Chem. Inf. Model. 2011, 51 (8), pp 2007-2023.
85
-
86
- *** The End ***
87
-