Class: SelectableAttr::Enum
- Inherits:
-
Object
- Object
- SelectableAttr::Enum
- Includes:
- Enumerable
- Defined in:
- lib/selectable_attr/enum.rb
Defined Under Namespace
Classes: Entry
Instance Attribute Summary collapse
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #===(other) ⇒ Object
- #[](arg) ⇒ Object
- #define(id, key, name, options = nil, &block) ⇒ Object (also: #entry)
- #each(&block) ⇒ Object
- #entries ⇒ Object
- #entry_by(value, *attrs) ⇒ Object
- #entry_by_hash(attrs) ⇒ Object
- #entry_by_id(id) ⇒ Object
- #entry_by_id_or_key(id_or_key) ⇒ Object
- #entry_by_key(key) ⇒ Object
- #find(options = nil, &block) ⇒ Object
- #i18n_scope(*path) ⇒ Object
- #id_by_key(key) ⇒ Object
- #ids(*ids_or_keys) ⇒ Object
-
#initialize(&block) ⇒ Enum
constructor
A new instance of Enum.
- #key_by_id(id) ⇒ Object
- #keys(*ids_or_keys) ⇒ Object
- #length ⇒ Object (also: #size)
- #map_attrs(attrs, *ids_or_keys) ⇒ Object
- #match_entry(entry, value, *attrs) ⇒ Object
- #name_by_id(id) ⇒ Object
- #name_by_key(key) ⇒ Object
- #names(*ids_or_keys) ⇒ Object
- #options(*ids_or_keys) ⇒ Object
- #to_hash_array ⇒ Object
- #values(*args) ⇒ Object
Constructor Details
#initialize(&block) ⇒ Enum
Returns a new instance of Enum.
13 14 15 16 17 |
# File 'lib/selectable_attr/enum.rb', line 13 def initialize(&block) @entries = [] instance_eval(&block) if block_given? SelectableAttr::Enum.instances << self end |
Instance Attribute Details
#name(val = nil) ⇒ Object
20 21 22 23 |
# File 'lib/selectable_attr/enum.rb', line 20 def name(val = nil) @name = val if val @name end |
Class Method Details
.instances ⇒ Object
8 9 10 |
# File 'lib/selectable_attr/enum.rb', line 8 def instances @@instances ||= [] end |
Instance Method Details
#==(other) ⇒ Object
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/selectable_attr/enum.rb', line 33 def ==(other) return false unless length == other.length other_entries = other.entries # ruby-1.8系ではEnumeratir#with_indexが使えないので1.8でも使用可能な書き方に変更しました。 # entries.map.with_index{|e, i| e == other_entries[i]}.all? # for 1.9 entries.each_with_index do |e, i| return false unless e == other_entries[i] end true end |
#===(other) ⇒ Object
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/selectable_attr/enum.rb', line 44 def ===(other) return false unless length == other.length other_entries = other.entries # ruby-1.8系ではEnumeratir#with_indexが使えないので1.8でも使用可能な書き方に変更しました。 # entries.map.with_index{|e, i| e === other_entries[i]}.all? entries.each_with_index do |e, i| return false unless e === other_entries[i] end true end |
#[](arg) ⇒ Object
91 92 93 |
# File 'lib/selectable_attr/enum.rb', line 91 def [](arg) arg.is_a?(Hash) ? entry_by_hash(arg) : entry_by_id_or_key(arg) end |
#define(id, key, name, options = nil, &block) ⇒ Object Also known as: entry
55 56 57 58 59 |
# File 'lib/selectable_attr/enum.rb', line 55 def define(id, key, name, = nil, &block) entry = Entry.new(self, id, key, name, , &block) entry.instance_variable_set(:@defined_in_code, true) @entries << entry end |
#each(&block) ⇒ Object
29 30 31 |
# File 'lib/selectable_attr/enum.rb', line 29 def each(&block) entries.each(&block) end |
#entries ⇒ Object
25 26 27 |
# File 'lib/selectable_attr/enum.rb', line 25 def entries @entries end |
#entry_by(value, *attrs) ⇒ Object
71 72 73 |
# File 'lib/selectable_attr/enum.rb', line 71 def entry_by(value, *attrs) entries.detect{|entry| match_entry(entry, value, *attrs)} || Entry::NULL end |
#entry_by_hash(attrs) ⇒ Object
87 88 89 |
# File 'lib/selectable_attr/enum.rb', line 87 def entry_by_hash(attrs) entries.detect{|entry| attrs.all?{|(attr, value)| entry[attr].to_s == value.to_s }} || Entry::NULL end |
#entry_by_id(id) ⇒ Object
75 76 77 |
# File 'lib/selectable_attr/enum.rb', line 75 def entry_by_id(id) entry_by(id, :id) end |
#entry_by_id_or_key(id_or_key) ⇒ Object
83 84 85 |
# File 'lib/selectable_attr/enum.rb', line 83 def entry_by_id_or_key(id_or_key) entry_by(id_or_key, :id, :key) end |
#entry_by_key(key) ⇒ Object
79 80 81 |
# File 'lib/selectable_attr/enum.rb', line 79 def entry_by_key(key) entry_by(key, :key) end |
#find(options = nil, &block) ⇒ Object
127 128 129 130 131 |
# File 'lib/selectable_attr/enum.rb', line 127 def find( = nil, &block) entries.detect{|entry| block_given? ? yield(entry) : entry.match?() } || Entry::NULL end |
#i18n_scope(*path) ⇒ Object
62 63 64 65 |
# File 'lib/selectable_attr/enum.rb', line 62 def i18n_scope(*path) @i18n_scope = path unless path.empty? @i18n_scope end |
#id_by_key(key) ⇒ Object
123 |
# File 'lib/selectable_attr/enum.rb', line 123 def id_by_key(key); entry_by_key(key).id; end |
#ids(*ids_or_keys) ⇒ Object
117 |
# File 'lib/selectable_attr/enum.rb', line 117 def ids(*ids_or_keys); map_attrs(:id, *ids_or_keys); end |
#key_by_id(id) ⇒ Object
122 |
# File 'lib/selectable_attr/enum.rb', line 122 def key_by_id(id); entry_by_id(id).key; end |
#keys(*ids_or_keys) ⇒ Object
118 |
# File 'lib/selectable_attr/enum.rb', line 118 def keys(*ids_or_keys); map_attrs(:key, *ids_or_keys); end |
#length ⇒ Object Also known as: size
141 142 143 |
# File 'lib/selectable_attr/enum.rb', line 141 def length entries.length end |
#map_attrs(attrs, *ids_or_keys) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/selectable_attr/enum.rb', line 101 def map_attrs(attrs, *ids_or_keys) if attrs.is_a?(Array) ids_or_keys.empty? ? entries.map{|entry| attrs.map{|attr|entry.send(attr)}} : ids_or_keys.map do |id_or_key| entry = entry_by_id_or_key(id_or_key) attrs.map{|attr|entry.send(attr)} end else attr = attrs ids_or_keys.empty? ? entries.map(&attr.to_sym) : ids_or_keys.map{|id_or_key|entry_by_id_or_key(id_or_key).send(attr)} end end |
#match_entry(entry, value, *attrs) ⇒ Object
67 68 69 |
# File 'lib/selectable_attr/enum.rb', line 67 def match_entry(entry, value, *attrs) attrs.any?{|attr| entry[attr].to_s == value.to_s} end |
#name_by_id(id) ⇒ Object
124 |
# File 'lib/selectable_attr/enum.rb', line 124 def name_by_id(id); entry_by_id(id).name; end |
#name_by_key(key) ⇒ Object
125 |
# File 'lib/selectable_attr/enum.rb', line 125 def name_by_key(key); entry_by_key(key).name; end |
#names(*ids_or_keys) ⇒ Object
119 |
# File 'lib/selectable_attr/enum.rb', line 119 def names(*ids_or_keys); map_attrs(:name, *ids_or_keys); end |
#options(*ids_or_keys) ⇒ Object
120 |
# File 'lib/selectable_attr/enum.rb', line 120 def (*ids_or_keys); map_attrs([:name, :id], *ids_or_keys); end |
#to_hash_array ⇒ Object
133 134 135 136 137 138 139 |
# File 'lib/selectable_attr/enum.rb', line 133 def to_hash_array entries.map do |entry| result = entry.to_hash yield(result) if block_given? result end end |
#values(*args) ⇒ Object
95 96 97 98 99 |
# File 'lib/selectable_attr/enum.rb', line 95 def values(*args) args = args.empty? ? [:name, :id] : args result = entries.collect{|entry| args.collect{|arg| entry.send(arg) }} (args.length == 1) ? result.flatten : result end |