Skip to content

Commit

Permalink
don't throw translating unknown strings to/from IB
Browse files Browse the repository at this point in the history
  • Loading branch information
cbilson committed Dec 31, 2012
1 parent 52a8363 commit 59860e2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
40 changes: 26 additions & 14 deletions src/ib_re_actor/translation.clj
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,37 @@ to check if if a given value is valid (known)."
(contains? to-table# val#))

(defmethod translate [:to-ib ~table-name] [_# _# val#]
(when val#
(if (valid? :to-ib ~table-name val#)
(to-table# val#)
(throw (ex-info (str "Can't translate to IB " ~table-name " " val#)
{:value val#
:table ~table-name
:valid-values (keys to-table#)})))))
(when val#
(cond
(valid? :to-ib ~table-name val#)
(to-table# val#)

(string? val#)
val#

:otherwise
(throw (ex-info (str "Can't translate to IB " ~table-name " " val#)
{:value val#
:table ~table-name
:valid-values (keys to-table#)})))))

(defmethod valid? [:from-ib ~table-name] [_# _# val#]
(contains? from-table# val#))

(defmethod translate [:from-ib ~(keyword name)] [_# _# val#]
(when val#
(if (valid? :from-ib ~table-name val#)
(from-table# val#)
(throw (ex-info (str "Can't translate from IB " ~table-name " " val#)
{:value val#
:table ~table-name
:valid-values (vals to-table#)}))))))))
(when val#
(cond
(valid? :from-ib ~table-name val#)
(from-table# val#)

(string? val#)
val#

:otherwise
(throw (ex-info (str "Can't translate from IB " ~table-name " " val#)
{:value val#
:table ~table-name
:valid-values (vals to-table#)}))))))))

(translation-table duration-unit
{:second "S"
Expand Down
12 changes: 12 additions & 0 deletions test/ib_re_actor/test/translation.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@
[clj-time.core :only [date-time]]
[midje.sweet]))

(fact "unknown string codes just translate into themselves"
(fact "coming from IB"
(translate :from-ib :security-type "some weird value")
=> "some weird value")
(fact "going out to IB"
(translate :to-ib :security-type "some weird value")
=> "some weird value"))

(fact "unknown keyword codes throw"
(translate :to-ib :security-type :I-misspelled-something)
=> (throws #"^Can't translate to IB"))

(tabular
(fact "it can translate to IB durations"
(translate :to-ib :duration [?value ?unit]) => ?expected)
Expand Down

0 comments on commit 59860e2

Please sign in to comment.