Navigation Menu

Skip to content

Commit

Permalink
Make data attributes as an object
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Feb 17, 2014
1 parent 53af4a3 commit 62f12d9
Show file tree
Hide file tree
Showing 4 changed files with 234 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/chupa-text.rb
Expand Up @@ -41,6 +41,7 @@
require "chupa-text/file-content"
require "chupa-text/virtual-content"

require "chupa-text/attributes"
require "chupa-text/data"
require "chupa-text/input-data"
require "chupa-text/virtual-file-data"
Expand Down
126 changes: 126 additions & 0 deletions lib/chupa-text/attributes.rb
@@ -0,0 +1,126 @@
# Copyright (C) 2014 Kouhei Sutou <kou@clear-code.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

module ChupaText
# Attributes of data.
class Attributes < Struct.new(:title,
:author,
:encoding,
:created_time,
:modified_time)
include Enumerable

def initialize
super
@members = members
@extra_data = {}
end

def to_h
super.merge(@extra_data)
end

def inspect
super.gsub(/>\z/) do
" @extra_data=#{@extra_data.inspect}>"
end
end

# @yield [name, value] Gives attribute name and value to the block.
# @yieldparam [Symbol] name The attribute name.
# @yieldparam [Object] value The attribute value.
def each(&block)
if block.nil?
Enumerator.new(self, :each)
else
each_pair(&block)
@extra_data.each_pair(&block)
end
end

# Gets the value of attribute named `name`.
#
# @param [Symbol, String] name The attribute name.
# @return [Object] The attribute value.
def [](name)
name = normalize_name(name)
if @members.key?(name)
super
else
@extra_data[name]
end
end

# Sets `value` as the value of attribute named `name`.
#
# @param [Symbol, String] name The attribute name.
# @param [Object] value The attribute value.
def []=(name, value)
name = normalize_name(name)
if @members.key?(name)
send("#{name}=", value)
else
@extra_data[name] = value
end
end

# Sets `encoding` as the `encoding` attribute value.
#
# @param [String, Encoding, nil] encoding The encoding.
def encoding=(encoding)
super(normalize_encoding(encoding))
end

# Sets `time` as the `created_time` attribute value.
#
# @param [String, Integer, Time, nil] time The created time.
# If `time` is `Integer`, it is used as UNIX time.
def created_time=(time)
super(normalize_time(time))
end

# Sets `time` as the `modified_time` attribute value.
#
# @param [String, Integer, Time, nil] time The modified time.
# If `time` is `Integer`, it is used as UNIX time.
def modified_time=(time)
super(normalize_time(time))
end

private
def normalize_name(name)
name.to_sym
end

def normalize_encoding(encoding)
if encoding.is_a?(String)
encoding = Encoding.find(encoding)
end
encoding
end

def normalize_time(time)
case time
when String
Time.parse(time)
when Integer
Time.at(time).utc
else
time
end
end
end
end
5 changes: 3 additions & 2 deletions lib/chupa-text/data.rb
Expand Up @@ -43,7 +43,8 @@ class Data
# text and meta-data.
attr_accessor :path

attr_accessor :attributes
# @return [Attributes] The attributes of the data.
attr_reader :attributes

# @return [Data, nil] The source of the data. For example, text
# data (`hello.txt`) in archive data (`hello.tar`) have the
Expand All @@ -56,7 +57,7 @@ def initialize
@size = nil
@path = nil
@mime_type = nil
@attributes = {}
@attributes = Attributes.new
@source = nil
end

Expand Down
104 changes: 104 additions & 0 deletions test/test-attributes.rb
@@ -0,0 +1,104 @@
# Copyright (C) 2014 Kouhei Sutou <kou@clear-code.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

class TestAttributes < Test::Unit::TestCase
def setup
@attributes = ChupaText::Attributes.new
end

sub_test_case("title") do
def test_accessor
assert_nil(@attributes.title)
@attributes.title = "Title"
assert_equal("Title", @attributes.title)
end
end

sub_test_case("author") do
def test_accessor
assert_nil(@attributes.author)
@attributes.author = "Alice"
assert_equal("Alice", @attributes.author)
end
end

sub_test_case("encoding") do
def test_string
@attributes.encoding = "UTF-8"
assert_equal(Encoding::UTF_8, @attributes.encoding)
end

def test_encoding
@attributes.encoding = Encoding::UTF_8
assert_equal(Encoding::UTF_8, @attributes.encoding)
end

def test_nil
@attributes.encoding = nil
assert_nil(@attributes.encoding)
end
end

sub_test_case("created_time") do
def test_string
@attributes.created_time = "2014-02-17T23:14:30+09:00"
assert_equal(Time.parse("2014-02-17T23:14:30+09:00"),
@attributes.created_time)
end

def test_integer
@attributes.created_time = 1392646470
assert_equal(Time.parse("2014-02-17T23:14:30+09:00"),
@attributes.created_time)
end

def test_time
@attributes.created_time = Time.parse("2014-02-17T23:14:30+09:00")
assert_equal(Time.parse("2014-02-17T23:14:30+09:00"),
@attributes.created_time)
end

def test_nil
@attributes.created_time = nil
assert_nil(@attributes.created_time)
end
end

sub_test_case("modified_time") do
def test_string
@attributes.modified_time = "2014-02-17T23:14:30+09:00"
assert_equal(Time.parse("2014-02-17T23:14:30+09:00"),
@attributes.modified_time)
end

def test_integer
@attributes.modified_time = 1392646470
assert_equal(Time.parse("2014-02-17T23:14:30+09:00"),
@attributes.modified_time)
end

def test_time
@attributes.modified_time = Time.parse("2014-02-17T23:14:30+09:00")
assert_equal(Time.parse("2014-02-17T23:14:30+09:00"),
@attributes.modified_time)
end

def test_nil
@attributes.modified_time = nil
assert_nil(@attributes.modified_time)
end
end
end

0 comments on commit 62f12d9

Please sign in to comment.