summaryrefslogtreecommitdiff
path: root/src/item.cc
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2009-02-14 05:37:53 -0400
committerJohn Wiegley <johnw@newartisans.com>2009-02-14 05:37:53 -0400
commitee5e0600aaa63859b9452d981ea9dd29b581b216 (patch)
tree91f087a8c701f6dca8ddf2e35a4bba107866bb3e /src/item.cc
parent67d63366cb1c7e1bc528f625c28437ca19ab927e (diff)
downloadfork-ledger-ee5e0600aaa63859b9452d981ea9dd29b581b216.tar.gz
fork-ledger-ee5e0600aaa63859b9452d981ea9dd29b581b216.tar.bz2
fork-ledger-ee5e0600aaa63859b9452d981ea9dd29b581b216.zip
xact metadata searches get passed up to the entry
That is, if a metadata tag cannot be found in a transaction, look in the parent entry to see if it was set there. Transactions "inherit" notational details from their entries.
Diffstat (limited to 'src/item.cc')
-rw-r--r--src/item.cc94
1 files changed, 52 insertions, 42 deletions
diff --git a/src/item.cc b/src/item.cc
index 48694727..e997d13e 100644
--- a/src/item.cc
+++ b/src/item.cc
@@ -37,18 +37,61 @@ bool item_t::use_effective_date = false;
bool item_t::has_tag(const string& tag) const
{
- if (! metadata)
+ DEBUG("item.meta", "Checking if item has tag: " << tag);
+ if (! metadata) {
+ DEBUG("item.meta", "Item has no metadata at all");
return false;
+ }
string_map::const_iterator i = metadata->find(tag);
+ if (SHOW_DEBUG("item.meta")) {
+ if (i == metadata->end())
+ DEBUG("item.meta", "Item does not have this tag");
+ else
+ DEBUG("item.meta", "Item has the tag!");
+ }
return i != metadata->end();
}
+bool item_t::has_tag(const mask_t& tag_mask,
+ const optional<mask_t>& value_mask) const
+{
+ if (metadata) {
+ foreach (const string_map::value_type& data, *metadata) {
+ if (tag_mask.match(data.first)) {
+ if (! value_mask)
+ return true;
+ else if (data.second)
+ return value_mask->match(*data.second);
+ }
+ }
+ }
+ return false;
+}
+
optional<string> item_t::get_tag(const string& tag) const
{
+ DEBUG("item.meta", "Getting item tag: " << tag);
if (metadata) {
+ DEBUG("item.meta", "Item has metadata");
string_map::const_iterator i = metadata->find(tag);
- if (i != metadata->end())
+ if (i != metadata->end()) {
+ DEBUG("item.meta", "Found the item!");
return (*i).second;
+ }
+ }
+ return none;
+}
+
+optional<string> item_t::get_tag(const mask_t& tag_mask,
+ const optional<mask_t>& value_mask) const
+{
+ if (metadata) {
+ foreach (const string_map::value_type& data, *metadata) {
+ if (tag_mask.match(data.first) &&
+ (! value_mask ||
+ (data.second && value_mask->match(*data.second))))
+ return data.second;
+ }
}
return none;
}
@@ -146,48 +189,15 @@ namespace {
value_t has_tag(call_scope_t& args) {
item_t& item(find_scope<item_t>(args));
- if (! item.metadata)
- return false;
-
- IF_DEBUG("item.meta") {
- foreach (const item_t::string_map::value_type& data, *item.metadata) {
- *_log_stream << " Tag: " << data.first << "\n";
- *_log_stream << "Value: ";
- if (data.second)
- *_log_stream << *data.second << "\n";
- else
- *_log_stream << "<none>\n";
- }
- }
-
- value_t& arg(args[0]);
- if (arg.is_string()) {
- if (args.size() == 1) {
+ if (args.size() == 1) {
+ if (args[0].is_string())
return item.has_tag(args[0].as_string());
- }
- else if (optional<string> tag = item.get_tag(args[0].as_string())) {
- if (args[1].is_string()) {
- return args[1].as_string() == *tag;
- }
- else if (args[1].is_mask()) {
- return args[1].as_mask().match(*tag);
- }
- }
- }
- else if (arg.is_mask()) {
- foreach (const item_t::string_map::value_type& data, *item.metadata) {
- if (arg.as_mask().match(data.first)) {
- if (args.size() == 1)
- return true;
- else if (data.second) {
- if (args[1].is_string())
- return args[1].as_string() == *data.second;
- else if (args[1].is_mask())
- return args[1].as_mask().match(*data.second);
- }
- }
- }
+ else if (args[0].is_mask())
+ return item.has_tag(args[0].as_mask());
+ } else {
+ return item.has_tag(args[0].to_mask(),
+ args[1].to_mask());
}
return false;
}