Class: SelectableAttr::Enum::Entry
- Inherits:
-
Object
- Object
- SelectableAttr::Enum::Entry
- Defined in:
- lib/selectable_attr/enum.rb
Constant Summary collapse
- BASE_ATTRS =
[:id, :key, :name]
- NULL =
new(nil, nil, nil, nil) do def null?; true; end def name; nil; end end
Instance Attribute Summary collapse
-
#defined_in_code ⇒ Object
readonly
Returns the value of attribute defined_in_code.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #===(other) ⇒ Object
- #[](option_key) ⇒ Object
-
#initialize(enum, id, key, name, options = nil, &block) ⇒ Entry
constructor
A new instance of Entry.
- #inspect ⇒ Object
- #match?(options) ⇒ Boolean
- #name ⇒ Object
- #null? ⇒ Boolean
- #null_object? ⇒ Boolean
- #to_hash ⇒ Object
Constructor Details
#initialize(enum, id, key, name, options = nil, &block) ⇒ Entry
Returns a new instance of Entry.
150 151 152 153 154 155 156 157 |
# File 'lib/selectable_attr/enum.rb', line 150 def initialize(enum, id, key, name, = nil, &block) @enum = enum @id = id @key = key @name = name @options = self.instance_eval(&block) if block end |
Instance Attribute Details
#defined_in_code ⇒ Object (readonly)
Returns the value of attribute defined_in_code.
149 150 151 |
# File 'lib/selectable_attr/enum.rb', line 149 def defined_in_code @defined_in_code end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
148 149 150 |
# File 'lib/selectable_attr/enum.rb', line 148 def id @id end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
148 149 150 |
# File 'lib/selectable_attr/enum.rb', line 148 def key @key end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
148 149 150 |
# File 'lib/selectable_attr/enum.rb', line 148 def @options end |
Instance Method Details
#==(other) ⇒ Object
174 175 176 177 178 179 180 181 |
# File 'lib/selectable_attr/enum.rb', line 174 def ==(other) case other when SelectableAttr::Enum::Entry self.id == other.id else false end end |
#===(other) ⇒ Object
183 184 185 186 187 188 189 190 |
# File 'lib/selectable_attr/enum.rb', line 183 def ===(other) case other when SelectableAttr::Enum::Entry (self.id == other.id) && (self.key == other.key) else false end end |
#[](option_key) ⇒ Object
165 166 167 168 |
# File 'lib/selectable_attr/enum.rb', line 165 def [](option_key) BASE_ATTRS.include?(option_key) ? send(option_key) : @options ? @options[option_key] : nil end |
#inspect ⇒ Object
204 205 206 207 208 |
# File 'lib/selectable_attr/enum.rb', line 204 def inspect # object_idを2倍にしているのは通常のinspectと合わせるためです。 '#<%s:%x @id=%s, @key=%s, @name=%s, @options=%s>' % [ self.class.name, object_id * 2, id.inspect, key.inspect, name.inspect, @options.inspect] end |
#match?(options) ⇒ Boolean
170 171 172 |
# File 'lib/selectable_attr/enum.rb', line 170 def match?() @options === end |
#name ⇒ Object
159 160 161 162 163 |
# File 'lib/selectable_attr/enum.rb', line 159 def name I18n.locale.nil? ? @name : @enum.i18n_scope.nil? ? @name : I18n.translate(key, :scope => @enum.i18n_scope, :default => @name) end |
#null? ⇒ Boolean
192 193 194 |
# File 'lib/selectable_attr/enum.rb', line 192 def null? false end |
#null_object? ⇒ Boolean
196 197 198 |
# File 'lib/selectable_attr/enum.rb', line 196 def null_object? self.null? end |
#to_hash ⇒ Object
200 201 202 |
# File 'lib/selectable_attr/enum.rb', line 200 def to_hash (@options || {}).merge(:id => @id, :key => @key, :name => name) end |