Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support mapping
items
names to values
Ex row.items = [["name", value], ["name2", value2]]
- Loading branch information
1 parent
0fcfd03
commit bec0b40
Showing
6 changed files
with
97 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,43 @@ | |||
module Formotion | |||
module RowType | |||
module ItemsMapper | |||
def items | |||
if !row.items | |||
[] | |||
elsif row.items[0].is_a?(Enumerable) | |||
row.items | |||
else | |||
row.items.map {|i| [i, i]} | |||
end | |||
end | |||
|
|||
def item_names | |||
self.items.map { |name, value| name } | |||
end | |||
|
|||
def item_names_hash | |||
hash = {} | |||
self.items.each do |name, value| | |||
hash[name] = value | |||
end | |||
hash | |||
end | |||
|
|||
def name_index_of_value(value) | |||
item_names.index(item_names_hash.invert[value]) | |||
end | |||
|
|||
def value_for_name_index(index) | |||
item_names_hash[item_names[index]] | |||
end | |||
|
|||
def value_for_name(name) | |||
item_names_hash[name] | |||
end | |||
|
|||
def name_for_value(value) | |||
item_names_hash.invert[value].to_s | |||
end | |||
end | |||
end | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters