Module: Sumaki::Model

Defined in:
lib/sumaki/model.rb,
lib/sumaki/model/enum.rb,
lib/sumaki/model/fields.rb,
lib/sumaki/model/fields/type.rb,
lib/sumaki/model/associations.rb,
lib/sumaki/model/fields/type/date.rb,
lib/sumaki/model/fields/reflection.rb,
lib/sumaki/model/fields/type/float.rb,
lib/sumaki/model/fields/type/value.rb,
lib/sumaki/model/fields/type/string.rb,
lib/sumaki/model/fields/type/boolean.rb,
lib/sumaki/model/fields/type/integer.rb,
lib/sumaki/model/fields/type/date_time.rb,
lib/sumaki/model/associations/collection.rb,
lib/sumaki/model/associations/reflection.rb,
lib/sumaki/model/associations/association.rb

Overview

Sumaki

Sumaki is a wrapper for structured data like JSON.

class AnimeList
  include Sumaki::Model
  repeated :anime
  field :name

 class Anime
    include Sumaki::Model
    singular :studio
    field :title

    class Studio
      include Sumaki::Model
      field :name
    end
  end
end

data = {
  name: 'Winter 2023',
  anime: [
    {
      title: 'The Vampire Dies in No Time',
      studio: {
        name: 'MADHOUSE Inc.'
      }
    },
    {
      title: '“Ippon” again!',
      studio: {
        name: 'BAKKEN RECORD'
      }
    }
  ]
}

anime_list = AnimeList.new(data)
anime_list.name #=> 'Winter 2023'
anime_list.anime[0].title #=> 'The Vampire Dies in No Time'
anime_list.anime[0].studio.name #=>  'MADHOUSE Inc.'
anime_list.anime[0].object #=> { title: 'The Vampire Dies in No Time', studio: { name: 'MADHOUSE Inc.' } }

Access to fields

By declaring ‘field`, you can access the field.

class Anime
  include Sumaki::Model
  field :title
  field :url
end

anime = Anime.new({ title: "The Vampire Dies in No Time", url: "https://sugushinu-anime.jp/" })
anime.title #=> "The Vampire Dies in No Time"

Access to sub objects

By declaring ‘singular`, you can access the sub object.

class Book
  include Sumaki::Model
  singular :company
  field :title

  class Company
    include Sumaki::Model
    field :name
  end
end

data = {
  title: "The Ronaldo Chronicles",
  company: {
    name: 'Autumn Books',
  }
}

comic = Book.new(data)
comic.company.name #=> 'Autumn Books'

By declaring ‘repeated`, you can access the repeated sub objects as an Array.

class Company
  include Sumaki::Model
  repeated :member
  field :name

  class Member
    include Sumaki::Model
    field :name
  end
end

data = {
  name: 'The Ronaldo Vampire Hunter Agency',
  member: [
    { name: 'Ronaldo' },
    { name: 'Draluc' },
    { name: 'John' }
  ]
}

company = Company.new(data)
company.member.size #=> 3
company.member[2].name #=> 'John'

Access to the parent object

Parent object can be referenced from sub object by ‘#parent` method.

class Character
  include Sumaki::Model
  singular :child
  field :name

  class Child
    include Sumaki::Model
    field :name
  end
end

data = {
  name: 'Draus',
  child: {
    name: 'Draluc'
  }
}

character = Character.new(data)
character.child.name #=> 'Draluc'
character.child.parent.name #=> 'Draus'

Enumerations

By declaring ‘enum`, You can map a field to a specified value.

class Character
  include Sumaki::Model
  field :name
  enum :type, vampire: 1, vampire_hunter: 2, familier: 3, editor: 4
end

data = {
  name: 'John',
  type: 3
}

character = Character.new(data)
character.type #=> :familier

Defined Under Namespace

Modules: Associations, ClassMethods, Enum, Fields, InstanceMethods Classes: ObjectAccessor

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



162
163
164
165
166
167
168
169
# File 'lib/sumaki/model.rb', line 162

def self.included(base)
  base.extend ClassMethods
  base.include InstanceMethods

  base.include Fields
  base.include Associations
  base.include Enum
end