Class: SelectableAttr::Enum::Entry

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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, options = nil, &block)
  @enum = enum
  @id = id
  @key = key
  @name = name
  @options = options
  self.instance_eval(&block) if block
end

Instance Attribute Details

#defined_in_codeObject (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

#idObject (readonly)

Returns the value of attribute id.



148
149
150
# File 'lib/selectable_attr/enum.rb', line 148

def id
  @id
end

#keyObject (readonly)

Returns the value of attribute key.



148
149
150
# File 'lib/selectable_attr/enum.rb', line 148

def key
  @key
end

#optionsObject (readonly)

Returns the value of attribute options.



148
149
150
# File 'lib/selectable_attr/enum.rb', line 148

def options
  @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

#inspectObject



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

Returns:

  • (Boolean)


170
171
172
# File 'lib/selectable_attr/enum.rb', line 170

def match?(options)
  @options === options
end

#nameObject



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

Returns:

  • (Boolean)


192
193
194
# File 'lib/selectable_attr/enum.rb', line 192

def null?
  false
end

#null_object?Boolean

Returns:

  • (Boolean)


196
197
198
# File 'lib/selectable_attr/enum.rb', line 196

def null_object?
  self.null?
end

#to_hashObject



200
201
202
# File 'lib/selectable_attr/enum.rb', line 200

def to_hash
  (@options || {}).merge(:id => @id, :key => @key, :name => name)
end