class Agave::Local::Item
Attributes
entity[R]
Public Class Methods
new(entity, items_repo)
click to toggle source
# File lib/agave/local/item.rb, line 19 def initialize(entity, items_repo) @entity = entity @items_repo = items_repo end
Public Instance Methods
==(other)
click to toggle source
# File lib/agave/local/item.rb, line 24 def ==(other) other.is_a?(Item) && other.id == id end
attributes()
click to toggle source
# File lib/agave/local/item.rb, line 45 def attributes fields.each_with_object( ActiveSupport::HashWithIndifferentAccess.new ) do |field, acc| acc[field.api_key.to_sym] = send(field.api_key) end end
children()
click to toggle source
# File lib/agave/local/item.rb, line 61 def children @items_repo.children_of(id).sort_by(&:position) if item_type.tree end
created_at()
click to toggle source
# File lib/agave/local/item.rb, line 69 def created_at # @TODO ? # Time.parse(entity.created_at).utc end
fields()
click to toggle source
# File lib/agave/local/item.rb, line 41 def fields @fields ||= item_type.fields.sort_by(&:position) end
item_type()
click to toggle source
# File lib/agave/local/item.rb, line 37 def item_type @item_type ||= entity.item_type end
parent()
click to toggle source
# File lib/agave/local/item.rb, line 57 def parent @items_repo.find(entity.parent_id) if item_type.tree && entity.parent_id end
position()
click to toggle source
# File lib/agave/local/item.rb, line 53 def position entity.position end
singleton?()
click to toggle source
# File lib/agave/local/item.rb, line 32 def singleton? item_type.singleton end
Also aliased as: single_instance?
to_hash(max_depth = 3, current_depth = 0)
click to toggle source
# File lib/agave/local/item.rb, line 80 def to_hash(max_depth = 3, current_depth = 0) return id if current_depth >= max_depth base = { id: id, item_type: item_type.api_key, updated_at: updated_at, created_at: created_at } base[:position] = position if item_type.sortable if item_type.tree base[:position] = position base[:children] = children.map do |child| child.to_hash( max_depth, current_depth + 1 ) end end fields.each_with_object(base) do |field, result| value = send(field.api_key) result[field.api_key.to_sym] = if value.respond_to?(:to_hash) m = value.method(:to_hash) if m.arity == 2 value.to_hash(max_depth, current_depth + 1) else value.to_hash end else value end end end
to_s()
click to toggle source
# File lib/agave/local/item.rb, line 74 def to_s api_key = item_type.api_key "#<Item id=#{id} item_type=#{api_key} attributes=#{attributes}>" end
Also aliased as: inspect
updated_at()
click to toggle source
# File lib/agave/local/item.rb, line 65 def updated_at Time.parse(entity.updated_at).utc end
Private Instance Methods
method_missing(method, *arguments, &block)
click to toggle source
Calls superclass method
# File lib/agave/local/item.rb, line 148 def method_missing(method, *arguments, &block) field = fields.find { |f| f.api_key.to_sym == method } if field && arguments.empty? read_attribute(method, field) else super end rescue NoMethodError => e if e.name === method message = [] message << "Undefined method `#{method}`" message << "Available fields for a `#{item_type.api_key}` item:" message += fields.map do |f| "* .#{f.api_key}" end raise NoMethodError, message.join("\n") else raise e end end
read_attribute(method, field)
click to toggle source
# File lib/agave/local/item.rb, line 121 def read_attribute(method, field) field_type = field.field_type type_klass_name = "::Agave::Local::FieldType::#{field_type.camelize}" type_klass = type_klass_name.safe_constantize value = if field.localized obj = entity.send(method) || {} Utils::LocaleValue.find(obj) else entity.send(method) end if type_klass type_klass.parse(value, @items_repo) else warning = [ "Warning: unrecognized field of type `#{field_type}`", "for item `#{item_type.api_key}` and", "field `#{method}`: returning a simple Hash instead.", 'Please upgrade to the latest version of the `agave` gem!' ] puts warning.join(' ') value end end
respond_to_missing?(method, include_private = false)
click to toggle source
Calls superclass method
# File lib/agave/local/item.rb, line 169 def respond_to_missing?(method, include_private = false) field = fields.find { |f| f.api_key.to_sym == method } if field true else super end end