1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 | var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
|
6 |
|
7 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
8 |
|
9 | var sift = require("sift"),
|
10 | isThere = require("is-there"),
|
11 | abs = require("abs"),
|
12 | rJson = require("r-json"),
|
13 | wJson = require("w-json");
|
14 |
|
15 |
|
16 | var DEFAULT_PATH = abs("~/.ideas.json");
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | var Idea = function () {
|
29 | function Idea(path, callback) {
|
30 | var _this = this;
|
31 |
|
32 | _classCallCheck(this, Idea);
|
33 |
|
34 | if (typeof path === "function") {
|
35 | callback = path;
|
36 | path = null;
|
37 | }
|
38 |
|
39 |
|
40 | this.path = path = path || DEFAULT_PATH;
|
41 | callback = callback || function (err) {
|
42 | if (err) throw err;
|
43 | };
|
44 |
|
45 |
|
46 | if (!isThere(this.path)) {
|
47 | this.ideas = [];
|
48 | this.save(callback);
|
49 | } else {
|
50 | this.list(function (err, ideas) {
|
51 | if (err) {
|
52 | return callback(err);
|
53 | }
|
54 | _this.ideas = ideas;
|
55 | callback(null, ideas, _this);
|
56 | });
|
57 | }
|
58 | }
|
59 | |
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | _createClass(Idea, [{
|
71 | key: "list",
|
72 | value: function list(callback) {
|
73 | rJson(this.path, function (err, content) {
|
74 | if (err) {
|
75 | return callback(err);
|
76 | }
|
77 | callback(err, content);
|
78 | });
|
79 | return this;
|
80 | }
|
81 | |
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 | }, {
|
93 | key: "filter",
|
94 | value: function filter(filters, callback) {
|
95 | callback(null, sift(filters, this.ideas));
|
96 | return this;
|
97 | }
|
98 | |
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 | }, {
|
109 | key: "create",
|
110 | value: function create(idea, callback) {
|
111 | if (!idea) {
|
112 | return callback(new Error("Idea cannot be empty."));
|
113 | }
|
114 | this.ideas.push({
|
115 | id: this.ideas.length,
|
116 | idea: idea,
|
117 | date: new Date(),
|
118 | state: "OPEN"
|
119 | });
|
120 | return this;
|
121 | }
|
122 | |
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 | }, {
|
134 | key: "solve",
|
135 | value: function solve(id, callback) {
|
136 | if (!this.ideas[id - 1]) {
|
137 | return callback(new Error("Cannot find any idea with this id."));
|
138 | }
|
139 | this.ideas[id - 1].state = "SOLVED";
|
140 | return this;
|
141 | }
|
142 | |
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 | }, {
|
153 | key: "save",
|
154 | value: function save(callback) {
|
155 | wJson(this.path, this.ideas, callback);
|
156 | return this;
|
157 | }
|
158 | }]);
|
159 |
|
160 | return Idea;
|
161 | }();
|
162 |
|
163 | module.exports = Idea; |
\ | No newline at end of file |