Class: Hitsuji

Inherits:
Object
  • Object
show all
Defined in:
lib/hitsuji.rb

Overview

The Hitsuji class is the interface to this module, and it contains all the functions you need. Examples using this class can be found in the descriptions of the class and instance methods below.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHitsuji

Creates a new system where all operations are performed.

Example

my_system = Hitsuji.new


13
14
15
# File 'lib/hitsuji.rb', line 13

def initialize
  @struct = []
end

Class Method Details

.item(name, value) ⇒ Object

Creates a new item, the equivalent of a variable in the system.

Attributes

  • name - the name of the new item, which is written as a symbol.

  • value - the contents of the new item, whether that be a string, variable or any other object.

Example

my_system = Hitsuji.new                        # a new system
my_item = Hitsuji.item(:foo, 'bar')            # a new item


29
30
31
# File 'lib/hitsuji.rb', line 29

def self.item(name, value)
  Item.new(name, value)
end

.linker(name, objs) ⇒ Object

Creates a new linker, which is simply put a grouping of items and other linkers.

Attributes

  • name - the name of the new linker, which is written as a symbol.

  • value - the contents of the new linker, whether that be an item or another linker.

Example

my_system = Hitsuji.new                        # a new system
my_item = Hitsuji.item(:foo, 'bar')            # a new item
my_item2 = Hitsuji.item(:qux, 'quux')          # a second item
items = [:foo, :qux]
my_linker = Hitsuji.linker(:baz, items)        # a new linker


49
50
51
# File 'lib/hitsuji.rb', line 49

def self.linker(name, objs)
  Linker.new(name, objs)
end

.operation(name, input, block) ⇒ Object

Creates a new operation, which is an equation performed on multiple items contained within a linker. This linker can contain more linkers from which more items will be progressively taken. An operation can then be used as part of a linker, and the result can be used in another operation.

Attributes

  • name - the name of the new linker, which is written as a symbol.

  • input - the contents of the new linker, whether that be an item or another linker.

  • block - a block as a string to perform the operation

Example

my_system = Hitsuji.new                        # a new system
my_item = Hitsuji.item(:foo, 1)                # a new item
my_item2 = Hitsuji.item(:qux, 2)               # a second item
items = [:foo, :qux]
my_linker = Hitsuji.linker(:baz, items)        # a new linker
my_op = Hitsuji.operation(:op, my_linker, %{
  |arg1, arg2| arg1 + arg2
}) # => :foo + :qux => 1 + 2 => 3              # a new operation


75
76
77
# File 'lib/hitsuji.rb', line 75

def self.operation(name, input, block)
  Operation.new(name, input, block)
end

Instance Method Details

#bind(*obj) ⇒ Object

‘Binds’ the inputted items to the system, allowing for the continous updating of the values within a system. This continuous updating forms the main principle of Hitsuji. Once bound, an object can only be edited using the Hitsuji.find, Hitsuji.edit and Hitsuji.remove methods. It can never share a name with any other bound object. Once bound, the name of an object becomes uneditable, but the value still keeps its read-write capabilites.

Attributes

  • obj - the items, linkers and operation you want to bind

Example

my_system = Hitsuji.new                        # a new system
my_item = Hitsuji.item(:foo, 1)                # a new item
my_item2 = Hitsuji.item(:qux, 2)               # a second item
items = [:foo, :qux]
my_linker = Hitsuji.linker(:baz, items)        # a new linker
my_system.bind(my_item, my_item2, my_linker)   # binds items + linker


98
99
100
101
# File 'lib/hitsuji.rb', line 98

def bind(*obj)
  @struct.concat obj
  update @struct
end

#edit(query, value) ⇒ Object

Finds a bound object in the system by name, edits the object if it exists, and then returns the original object.

Attributes

  • query - the name of the object to edit

  • value - the new value to assign to this object

Example

my_system = Hitsuji.new                        # a new system
my_system.import('oldfile.txt')                # imports 'oldfile.txt'
my_item = my_system.edit(:foo, 'bar')          # changes an item


168
169
170
# File 'lib/hitsuji.rb', line 168

def edit(query, value)
  get(query, @struct, value, false)
end

#export(directory) ⇒ Object

Exports the current state of the system to a file. This process *does not* export unbound items, linkers or operations! Creating new items doesn’t automatically bind them to the system, so therefore the exported file only contains objects bound with Hitsuji.bind.

Attributes

  • directory - the path of the file to export the system (the extension of the file doesn’t matter)

Example

my_system = Hitsuji.new                        # a new system
my_item = Hitsuji.item(:foo, 1)                # a new item
my_system.bind(my_item)                        # binds item
my_system.export('newfile.txt')                # exports to 'newfile.txt'


119
120
121
# File 'lib/hitsuji.rb', line 119

def export(directory)
  Transfer.export(directory, @struct)
end

#find(query) ⇒ Object

Finds a bound object in the system by name, and returns the object if it exists.

Attributes

  • query - the name of the object to search for

Example

my_system = Hitsuji.new                        # a new system
my_system.import('oldfile.txt')                # imports 'oldfile.txt'
my_item = my_system.find(:foo)                 # finds an item


151
152
153
# File 'lib/hitsuji.rb', line 151

def find(query)
  get(query, @struct, nil, false)
end

#import(directory) ⇒ Object

Imports a file into a system, *overwriting anything already bound to the system*.

Attributes

  • directory - the path of the file you want to import from

Example

my_system = Hitsuji.new                        # a new system
my_system.import('oldfile.txt')                # imports 'oldfile.txt'


134
135
136
137
# File 'lib/hitsuji.rb', line 134

def import(directory)
  @struct = Transfer.import(directory)
  update @struct
end

#remove(query) ⇒ Object

Finds a bound object in the system by name, removes it if it exists, and then returns the original object.

Attributes

  • query - the name of the object to remove

Example

my_system = Hitsuji.new                        # a new system
my_system.import('oldfile.txt')                # imports 'oldfile.txt'
my_item = my_system.remove(:foo)               # removes an item


184
185
186
# File 'lib/hitsuji.rb', line 184

def remove(query)
  get(query, @struct, nil, true)
end